Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
64 views

Conditional and Looping Construct

The document discusses different types of statements in Python including empty, simple, and compound statements. It then covers various types of flow control statements like conditional statements (if, if-else), looping statements (while and for loops), and jump statements (break, continue, pass). Specific examples are provided for each type of statement to illustrate their syntax and usage. Control flow statements allow executing code conditionally or repeatedly based on test expressions to control program logic and flow.

Uploaded by

Satyam Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
64 views

Conditional and Looping Construct

The document discusses different types of statements in Python including empty, simple, and compound statements. It then covers various types of flow control statements like conditional statements (if, if-else), looping statements (while and for loops), and jump statements (break, continue, pass). Specific examples are provided for each type of statement to illustrate their syntax and usage. Control flow statements allow executing code conditionally or repeatedly based on test expressions to control program logic and flow.

Uploaded by

Satyam Prakash
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

CONDITIONAL & LOOPING CONSTRUCT

Type of statement in Python:-


Statements are the instructions given to the computer to perform any
kind of action.
Type of statements:-
(1) Empty statements (ii) Simple statement (iii) Compound
statement
Empty statement :-
The statement which does nothing . In python pass statement is an
empty statement. Whenever python encounter a pass statement , it
does nothing and move to next statement.
Simple statement:-Any single executable statement is a simple
statement. E.g.
Name=input(“Enter any name “) or
print(Name);
Compound statement:-A compound statement represent a group of
statement executed as a unit. E.g.
<Compound Statement header>:
<indented body containing multiple lines>
Header line: It begin with a keywords and ends with a colon.
A body:It contain one or more lines indented inside the header at
same level.
STATEMENT FLOW OF CONTROL:-
(a) Sequence:- The sequence construct means the statements are
being executed sequentially(one by one).
(b) Selection:- The selection construct means execution of
statement depends on condition test. If condition is True one
course of action is followed otherwise another course of
action is followed.
(c) Iteration(Loop):- The iteration construct means repetition
of statement depending upon condition test. Till the time a
condition is True a set of statement is repeated again and
again . as soon as condition become false the repetition
stops.

Conditional Statements

Control statements are used to control the flow of execution


depending upon the specified condition/logic.

There are three types of control statements.

1. Decision Making Statements


2. Iteration Statements (Loops)
3. Jump Statements (break, continue, pass)

Decision making statement used to control the flow of execution of


program depending upon condition.

There are three types of decision making statement.


1. if statements
2. if-else statements
3. Nested if-else statement
4. if…elif.. statements
1. if statements
An if statement is a programming conditional statement that, if proved
true, performs a function or displays information.

if statements
Syntax:
if(condition):
statement
[statements]
e.g.1

marks=int(input(“Enter Marks “))


if marks>=33:
print(“Congratulation”)
print(“You are Pass”)
print(“End of Program”)

Eg.2.

Using logical operator in if statement


x=1 y=2
if(x==1 and y==2):
print(‘condition matching the criteria')

Output :-
condition matching the criteria

eg.3.

a=100
if not(a == 20):
print('a is not equal to 20')

Output :-
a is not equal to 20

2. if-else Statements
If-else statement executes some code if the test expression is true
(nonzero) and some other code if the test expression is false.

Syntax:
if(condition):
statements
else:
statements

e.g.
marks= int(input("Enter Marks "))
if marks>=33:
print("You are Pass ")
else:
print("You are Fail ")

print("End of Program ")

3. Nested if-else statement


If there is an if statement or if…else statement inside if body or else
body then it is known as nested if statement. It allows you to check for
multiple test expressions and execute different codes for more than two
conditions.

Example

# Nested if Example : Largest of Three numbers


a=int(input("Enter a "))
b=int(input("Enter b "))
c=int(input("Enter c "))

if(a>b):
if(a>c):
print("A ia largest ")
else:
print("C ia largest ")

else:
if(b>c):
print("B ia largest ")
else:
print("C ia largest ")

3. if-elif.. else statement


This statement will check a condition, if condition is TRUE one block is
executed and if condition is false then it will again check a condition
and execute a block of code as soon as one of the conditions evaluates to
TRUE. Here else block is added at the end which is executed if none of
the above if-elif statement is true.

Syntax
If (condition):
statements
elif (condition):
statements
else:
statements

E.G.
num = float(input("Enter a number: "))
if num == 0:
print("No is Zero")
elif num>0
print("Positive number")
else:
print("Negative number")

OUTPUT
Enter a number: 5
Positive number

Iteration statements(loop)
Iteration statements(loop) are used to execute a block of statements as
long as the condition is true. Loops statements are used when we need to
run same code again and again.
Loop Control elements:
(a) Initialization:- Before entering in a loop variable must be
initialized It is the first value from where we start a loop.
(b) Test Expression-: It is the condition test whose truth value
decides whether the loop body will be executed or not.
(c) Update Expression:- The update expression change the value of
a loop variable.

Counting loop:- The loop which repeat certain no of time eg for Loop
Conditioning loop:- The loop which repeat until certain condition is
true. Eg while loop

Python Iteration (Loops) statements are of three type :-

1. While Loop
2. For Loop
3. Nested Loops

1. While Loop
It is used to execute a block of statement as long as a given condition
is true. And when the condition become false, the control will come
out of the loop. The condition is checked
Syntax
while (condition):
statements

x = 1
while (x <= 5):
print(x)
x = x + 1
print(“Loop Over”)
Output
1
2
3
4
5
Loop Over
While Loop With else clause
Both the loop in python have else clause which is different from else of
if ..else statement . The else of loop execute only when loop ends
normally.
e.g.
x = 1
while (x < 3):
print('inside while loop value of x is ',x)
x = x + 1
else:
print('inside else value of x is ', x)
Output
inside while loop value of x is 1
inside while loop value of x is 2
inside else value of x is 3
range() Function Parameters
The range function is used in Loop. It is used to generate a list of
value which is special sequence type.
Parameters:-
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number. If stop
value is n then it run upto n-1
step(Optional): Determines the increment between each numbers in the
sequence.
Eg.
Range(10) = 0,1,2,3,4,5,6,7,8,9,
Range(12,18)= 12,13,14,15,16,17
Range(1,11,2)= 1,3,5,7,9

Operator in and not in


The in operator is use with range function:-
To check whether a value is contained inside the list
3 in [1,2,3,4] = true
5 not in [1,2,3,4] = True

‘a’ in “trade” = True


“ash’ in “trash” = True

2. For Loop continue


2. For Loop
It is used to iterate over items of any sequence, such as a list or a
string.
Syntax
for val in sequence:
statements

e.g1.
for i in range(1,5,1):
print(i)
Output
1
2
3
4

Eg.2.
for i in range(5,3,-1):
print(i)
Output
5
4

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

1
16
49

Eg
for ch in "kvk":
print(ch)
k
v
k
For Loop With Else
e.g.
for i in range(1, 4):
print(i)
else:
print("End of loop")
Output
1
2
3
End of loop

Nested Loop: When we use a loop inside the body of another loop, then it
is known as nested loop.

e.g.
for i in range(1,3):
for j in range(1,11):
k=i*j
print (k, end=' ')
print()

Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20

e.g.
for i in range(1,5):
for j in range(1,i):
print (“*”, end=' ')
print()

Output
*
**
***
****
3. Jump Statements
Jump statements are used to transfer the control from one location to
another.
There are three types of jump statements used in python.
1.break
2.continue
3.pass

1.break
It is used to terminate the loop. It enable a program to skip over a
part of the code.

for i in range(1,10,1):
if i == 5:
break
print(i)
print("The end")
Output
1
2
3
4
The end

2.continue
It is used to skip all the remaining statements in the loop
and move controls back to the next iteration (repetition) of the loop.
e.g.
for i in range(1,10,1):
if i == 5:
continue
print(i)

Output
It will print 1 to 10 except 5.

3. pass Statement
This statement does nothing. It can be used when a statement is
required syntactically but the program requires no action.

It is useful when language require presence of statement but logic of


the program does not require any statement.

if (marks>=33):
pass
else:
print(“Sorry! You are Fail “)

You might also like