conditional and looping constructs
conditional and looping constructs
syllabus
2020-21
Chapter 10
Conditional
&
Looping Constructs
Computer Science
Class XI ( As per CBSE Board)
Sandeep Singh ( PGT Computer Science )
Control Statements
1. if statements
An if statement is a programming conditional
statement that, if proved true, performs a
function or displays information.
Output :-
condition matcing the criteria
-----------------------------------------------------------
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.
OUTPUT
less than 100
if (year % 4) == 0:
if (year % 100) == 0:
if (year % 400) == 0:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
else:
print("{0} is a leap year".format(year))
else:
print("{0} is not a leap year".format(year))
1. While Loop
2. For Loop
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
*Write a program in python to find out the factorial of a given number
Output
Inside loop
Inside loop
…
…
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.g.
for i in range(3,5):
print(i)
Output
3
4
Sandeep Singh ( PGT Computer Science )
Iteration Statements (Loops)
Output
5
4
range() Function Parameters
start: Starting number of the sequence.
stop: Generate numbers up to, but not including this number.
step(Optional): Determines the increment between each numbers in
the sequence.
Output
1
2
3
No Break
Output
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
for i in range(years):
n=n+((n*rate)/100)
print(n)
print("The end")
Output
s
t
r
The end
Sandeep Singh ( PGT Computer Science )
Iteration Statements (Loops)
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
OUTPUT
My program
Sandeep Singh ( PGT Computer Science )
Iteration Statements (Loops)
3. pass Statement continue
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.