Conditional Statements
There are situations in real life when we need to do some specific task and based on some specific conditions, we decide what we should do next. Similarly, there comes a situation in programming where a specific task is to be performed if a specific condition is True. In such cases, conditional statements can be used. The following are the conditional statements provided by python-
- if
- if-else
- Nested-if
- if-elif
1) if Statement
The ‘if’ statement is written using “if” keyword, followed by a condition.If
the condition is true the statement-1 will be executed. Otherwise, the control will
be transferred to the first statement after the if block that is statement-2 will be executed.
- if statement must be followed by colon (:)
- Indentation (white space) must be used after if statement as shown below example
Syntax:
statement-1
statement-2
Example-
if 10>5:
print("10 is largest number")
print("Program end")
Note - If you have only one statement in if statement then you can write it in one line as below
if 10>5: print("10 is largest")
2) if else Statement
If the condition is true the statement-1 will be executed. Otherwise, the else block that is statement-2 will be executed.
- if and else statement must be followed by colon (:)
- Indentation (white space) must be used after if and else statement as shown below example
Syntax:
if <condition>:
statement-1
else:
statement-2
Example-
print("10 is largest number")
else:
print("5 is largest number")
3) Nested if Statement
If we use if statement inside another if statement or else statement then it is known as nested if statement.
- if and else statement must be followed by colon (:)
- Indentation (white space) must be used after if and else statement as shown below example
Syntax:
if <condition-1>:
statement-1
else:
if<condition-2>:
statement-2
else:
if<condition...N>:
statement...N
else
statement..M
Example-
ch='b'
if ch=='a':
print("Input character is a")
else:
if ch=='b':
print("Input character is b")
else:
if ch=='c':
print("Input character is c")
else:
print("Input character is not a or b or c")
4) if-elif Statement
if-elif statement is an updated form of nested if statement. The elif keyword means "else if "followed by any condition is used when previous if condtiton is not true
- if and else statement must be followed by colon (:)
- Indentation (white space) must be used after if and else statement as shown below example
Syntax:
if <condition-1>:
statement-1
elif<condition-2>:
statement-2
elif<condition...N>:
statement...N
else
statement..M
Example-
if ch=='d':
print("Input character is a")
elif ch=='b':
print("Input character is b")
elif ch=='c':
print("Input character is c")
else:
print("Input character is not a or b or c")
Iterative Statements or Loop Statements
Sometimes certain section of the code (block) may need to be repeated again
and again as long as certain condition remains true. In order to achieve this,
the iterative statements or loop statements are used.The number of times the block needs to be
repeated is controlled by the test condition used in that statement. This type
of statement is also called as the “Looping Statement”.
There are two types of loop in python-
- for loop
- while loop
1) for loop
The for loop is one of the powerful and efficient statements in
python which is used very often. It specifies how many times the body of
the loops needs to be executed. For this reason it uses control variables
which keep tracks,the count of execution. The general syntax of a ‘for’ loop
looks as below-
Syntax:
for <variable> in range (A,B):
body of the loop
Where A is the starting value and B is the end value + 1
Example
total = 0
n = int (input ("Enter a Positive Number"))
print("\n values are :",end=" ")
for i in range(1,n+1):
print(i,end=" ")
total = total + i
print ("\n The Sum is ", total)
range( ) Function
The range() function can be called in three different ways based on the
number of parameters. All parameter values must be integers.
i) range(end) - This is begins at 0.
Increments by 1. end just
before the value of end
parameter.
Example
for i in range(5):
print(i)
Output :
0,1,2,3,4
ii) range(beg, end) - Starts at begin, End before
end value, Increment by 1
Example
for i in range(2, 5):
print(i)
Output : 2,3,4
iii) range(beg, end,step) - Starts at begin, End before
end value, increment by
step value
Example
for i in range(2, 7, 2):
print(i)
Output : 2,4,6
2) while loop
The while loop allows the program to repeat the body of a loop, any
number of times, when some condition is true.The drawback of while loop is that, if the condition not proper it may lead to infinite looping. So the
user has to carefully choose the condition in such a way that it will
terminate at a particular stage.
Syntax:
while <condition>:
body of the loop
Example
total = 0
n = int (input ("Enter a Positive Number"))
i=1
while i<n:
total = total + i
i=i+1
print ("\n The Sum is ", total)
Nested loop
A nested loop is a loop inside a loop.The "inner loop" will be executed one time for each iteration of the "outer loop".
Example-
for i in range(1,6):
for j in range(1,6):
print(j,end='')
print()
Break Statement
A break statement is used inside both the
while and for loops. It terminates the loop immediately and transfers execution to the new statement after the loop.For example
count = 0 while count <= 100:
print (count)
count += 1
if count == 3:
break
Output
0
1
2
Continue Statement
he continue statement causes the loop to skip its current execution at some point and move on to the next iteration. Instead of terminating the loop like a break statement, it moves on to the subsequent execution.
For example
for i in range(0, 5):
if i == 3:
continue
print(i)
Output
0
1
2
4
0 Comments