Conditional and Looping Construct
Conditional and Looping Construct
Conditional Statements
if statements
Syntax:
if(condition):
statement
[statements]
e.g.1
Eg.2.
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 ")
Example
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 ")
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
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
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.
if (marks>=33):
pass
else:
print(“Sorry! You are Fail “)