Chapter - 3 Conditional and Iterative Statements
Chapter - 3 Conditional and Iterative Statements
Constructs
- Nirali Purohit
Control statements are used to control the flow of
execution depending upon the specified
condition/logic
Output :-
condition matching the criteria
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.
2. if-else Statements
Syntax:
if condition:
statements
else:
statements
e.g.
a=10
if a < 100:
print(‘less than 100')
else:
print(‘more than equal 100')
OUTPUT
less than 100
1. While Loop
2. For Loop
▶ while (condition):
▶ statement
[statement]
▶ e.g.
x=1 Output
while (x <= 4): 1
2
print(x) 3
x=x+1 4
while loop with else
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
while Loop: while Loop:
Infinite while Loop Infinite while Loop
e.g. e.g.
x=5 x=5
while x == 5: while True:
print(‘inside loop') print(‘inside loop')
Output Output
Inside loop Inside loop
Inside loop Inside loop
… …
… …
2. For Loop
Syntax
for val in range(<start>,end,<step>):
statements
e.g.
for i in range(3):
print (i, end=“ ”)
Output: 0 1 2
for i in range(1,11,2):
for i in range(3,5): print(I,end=“ ”)
print(i) Output: 1 3 5 7 9
Output
3
4
2. For Loop continue
for i in range(5,3,-1):
print(i)
Output
5
4
OR
Output
1
2
3
No Break
2. For Loop continue
Nested For 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
2. For Loop continue
Factorial of a number
factorial = int(input(‘enter a number’))
for i in range(years):
n=n+((n*rate)/100)
print(n)
3. Jump Statements
print("The end")
Output
s
t
r
The end
2.continue
It is used to skip all the remaining statements
in the loop and move controls back to the top of
the loop.
e.g.
for val in "init":
if val == "i":
continue
print(val)
print("The end")
Output
n
t
The end
3. pass Statement
This statement does nothing. It can be used when a
statement is required syntactically but the program
requires no action.
Use in loop
while True:
pass # Busy-wait for keyboard interrupt (Ctrl+C)
In function
It makes a controller to pass by without executing any code.
e.g.
def myfun():
pass #if we don’t use pass here then error message will be shown
print(‘my program')
OUTPUT
My program
3. pass Statement
e.g.
for i in 'initial':
if i == 'i':
pass
else:
print(i)
OUTPUT
n
t
a
L
NOTE : continue forces the loop to start at the next iteration
while pass means "there is no code to execute here" and
will continue through the remainder or the loop body.
Find if the given number is prime no
(Hint: Modulo the number from 2 to the number-1, if
divisible it not prime)
Find if the given number is perfect no or not
(Perfect number is number which is equal to the sum of
its positive factors excluding the number itself )
Find factors of a number
(Hint: Modulo the number from 1 to the number+1, if
divisible print it)
Find largest of 3 numbers
Fibonacci up to 10 numbers using range function:
Other ways to print Fibonacci series
(Hint: Using while loop to give end value of series and
for loop for number of elements in the series)
Find factorial of a number
Find if the number is Armstrong number
(Hint: Modulo the number by 10, exponent the result by length of
the number, add to a variable, remove last digit of the number)
Find lcm of two numbers
(Hint: Find the bigger of two numbers, keep dividing both the
numbers with greater+1 till we reach a number that gets divided
successfully by both the numbers)
Find hcf of two numbers
(Hint: Find the smaller of two numbers, keep dividing both the
numbers from 2 till smaller+1 till we reach a number that divides
both the numbers successfully)
Do the following series:
Answer 1:
Answer 2:
Answer 3:
Answer 4:
Create a pattern as follows:
*
**
***
****
*****
Create a pattern as follows:
12345
1234
123
12
1
Create a pattern as follows:
A
AB
ABC
ABCD
ABCDE