0% found this document useful (0 votes)
25 views5 pages

Cs CHP 4 - Unlocked 1

Hello

Uploaded by

amirbanda42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views5 pages

Cs CHP 4 - Unlocked 1

Hello

Uploaded by

amirbanda42
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 5

Conditional and Iterative Statement

Types of Statements in Python


Statements are the instructions given to the computer to perform any kind of action, be it data
movements, and be it making decisions or be it repeating actions. Statements form the smallest
executable unit within a Python program. Python statements can belong to one of the following three
types :
* Empty Statement * Simple Statement (Single statement) * Compound Statement

1. Empty Statement
The simplest statement is the empty statement i.e., a statement which does nothing. In Python empty
statement is pass statement. It takes the following form :
Pass

2. Simple Statement
Any single executable statement is a simple statement in Python. For Example, following is a simple
statement in Python :
name = input(" your name")

3. Compound Statement
A compound statement represents a group of statements executed as a unit. The compound state
ments of Python are written in a specific pattern as shown below :
<compound statement header> :
<indented body containing multiple simple and/or compound statements>
That is, a compound statement has :
* a header line which begins with a keyword and ends with a colon
* a body consisting of one or more Python statements, each indented inside the header line.
All statements in the body are at the same level of indentation.

Statement Flow Control


In a program, statements may be executed sequentially, selectively or iteratively. Every programming
language provides constructs to support sequence, selection or iteration.

Sequence
The sequence construct means the statements are being executed sequentially. This represents the
default flow of statement

Selection
The selection construct means the execution of statement (s) depending upon a condition-test. If a
condition evaluates to True, a course-of-action (a set of statements) is followed otherwise another
course-of-action (a different set of statements) is followed. This construct (selection construct) is
also called decision construct because it helps in making decision about which set-of-statements is
to be executed.
Iteration
The integration constructs mean repetition of a set-of-statement upon a condition-test. Till the time a
condition is True (or False depending upon the loop), a set-of-statement are repeated again and
again. As soon as the condition becomes False (or True), the repetition stops. The iteration con
struct is also called looping construct. The set-of-statements that are repeated again and again is
called the body the loop. Then on which the execution or exit of the loop depends is called the exit
condition or test-condition.

Program Logic Development Tools


An algorithm is a step-of-step procedure (well-defined instructions) to solve a given problem.
For instance, the algorithm to find the remainder of given two numbers is :
1. Input first number.
2. Input second number
3. Divide first number with second number and store the remainder as third number.
4. Display the result (the third number)

Flowcharts
A flow chat is a graphical representation of an algorithm. Aflowchart shows different subtasks with
different symboles.

Process Decision

Subprocess Start/End

Document Data

* Use Data symbole for Input/output (I/O) operation (taking input and showing output).
* Use Process symbol for any type of computation and internal operations like initialization,
calculation etc.
* Use Subprocess symbol to invoke a procedure written already.

Pseudocode
Pseudo code is an informal language that helps programmers describe steps of a program’s solution
without using any programming language syntax. Pseudo code is an informal way of describing the
steps of a program’s solution without using any strict programming language syntax or
underlying technology considerations.

Decision Trees
Decision trees are a way of presenting rules in a hierarchical and sequential structure, where based
on the hierarchy of rules, certain outcomes are predicted.

Marks Division
 60 First
50 − 60 Second
40 − 50 Third
 40 Failed
The if statements of Python
The if statements are the conditional statements in Python and these implement selection constructs
(decision constructs). An if statement tests a particular condition; if the condition evaluates to true, a
course-of-action is followed i.e., a statement or set-of-statements is executed. Otherwise (if the
condition evaluates to false), the course-of action is ignored.

The if -else Statement


This form of if statement tests a condition and if the condition evaluates to true, it carries out
statements indented below if and in case condition evaluates to false, it carries out statements
indented below else.

The if elif statement


Sometimes, you need to check another condition in case the test-condition of if evaluates to false.
That is, you want to check a condition when control reaches else part, i.e., condition test in the form
of else if.

The nested if statement


A nested if is an if that has another if in its if’s body or in elif’s body or in its else’s body.

The range ( ) Function


The range ( ) function of Python, which is used with the Python for loop. The range ( ) function of
Python generates a list which is a special sequence type. A sequence in Python is a succession of
values bound together by a single name. Some Python sequence types are : strings, lists, tuples.

Operators in and not in


Let us also take about in operator, which is used with range ( ) in for loops. To check whether
a value is contained inside a list you can use in operator, e.g., This expression will test value 3 is
contained in the given sequence

3 in [1,2,3,4] will return True as value 3 is contained in sequence [1,2,3,4]


5 in [1,2,3,4] will return False as value 5 is not contained in sequence [1,2,3,4]. But
5 not in [1,2,3,4] will return True as this fact is true that as value 5 is not contained in
sequence [1,2,3,4].The operator not in does opposite of in
operator.

You can see that in and not in are the operaors that check for membership of a value inside a
sequence. Thus, in and not in are also called membership operators. These operators work with all
sequence types i.e., strings, tuples and lists etc.

Iteration/Looping statements
The iteration statements or repetition statements allow a set of instructions to be performed repeat
edly until a certain condition is fulfilled. The interation statements are also called loops or looping
Statements. Python provides two kinds of loops : for loop and while loop to represent two
categories of loops, which are :
* counting loops the loops that repeat a certain number of times ; Python’s for loops is a
counting loop.

* conditional loops the loops that repeat until a certain thing happens i.e., they keep repeating
as long as some condition is true ; Python’s while loop is a conditional loop.

The for Loop


The for loop of Python is designed to process the items of any sequence, such as a list or a string,
one by one . The general form of for loop is as given below : for<variable>in

<sequence>

statements_to_repeat

for a in [1,4,7] : print (a) print (a*a)

The while loop


A while loop is a conditional loop that will repeat the instructions within itself as long as a conditional
remains true.
The general form of Python while loop is :
while<logicalexpression>
loop-body
Where the loop-body may contain a single statement or multiple statements or an emptystatement
(i.e., pass statement). The loop iterates while the logical expression evaluates to true. When the
expression becomes false, the program control passes to the line after the loop-body.

Loop else statement


Both loops of Python (i.e., for loop and while loop) have an else clause, which is different from else
of if-else statements. The else of a loop executes only when the loop ends normally (i.e., only when
the loop is ending because the while loop’s test conditon has resulted in false or the for loop has
executed for the last value in sequence.)
In order to fully understand the working of loop-else clause, it will be useful to know the working of
break and continue statements.The else clause of a Python loop executes when the loop terminates
normally, not when the loop is terminating because of a break statement.

Jump statements -break and continue


Python offers two jump statements to be used within loops to jump out of loop-interations.
These are break and continue statements.
The break statement
The break statement enables a program to skip over a part of the code. A break statement termi
nates the very loop it lies within. Execution resumes at the statement immediately following the body
of the terminated statement.

Infinite loops and break statement


Sometimes, programmers create infinite loops purposely by specifying an expression which always
remain true, but for such purposely created infinite loops, they incorporate some condition inside the
loop-body on which they can break out of the loop using break statement.
while True : This will always remain True, hence infinite loop ; must be

a=2
print(a) terminated through a break statement
a* = 2
if a  100 :
break

The continue statement


The continue statement is another jump statement like break statement as both the statements skip
over a part of the code. But the continue statement is somewhat different from break. Instead of
forcing termination, the continue statement forces the next iteration of the loop to take place, skip
ping any code in between.

Loop else statement


The else clause of a Python loop executes when the loop terminates normally, i.e., when test-
condition results in to false for a while loop or for loop has executed for the last value in the se
quence ; not when the break statement terminates the loop.

Complete syntax of Python loops along with else clause is as given below :

for  variable  in  seqence  : while  test condition 


statement1 statement1
statement2 statement2
: :

else : else :
statement(s) statement(s)

Nested Loops

A loop may contain another loop in its body. This form of a loop is called nested loop. But in a
nested loop, the inner loop must terminate before the outer loop.

The break statement in a Nested loop


If the statement appears in a nested loop, then it will terminate the very loop it is in. That is, if the
break statement is inside the inner loop then it wil terminate the inner loop only and the outer loop
will continue as it as. If the break statement is in outer loop, then outer loop gets terminated. Since
inner loop is part of outer loop’s body it will also not execute just like other statement of outer
loop’s body.

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy