Control Statements
When we write a simple program, generally it executes line by line and this execution are called sequential execution. This type of execution is suitable only for developing simple programs. It is not suitable for developing complex programs. To develop a program which contains complex logic. Then we need to execute statements according to our requirement for this we uses Control statements in which we can control the statements according to our need.
➠There are types of control
statements in
Python:-
a) If... statements
b) If... else.. Statements
c) If...elif....else.. Statements
d) While loop
e) For loop
f) Nested loop
g) Else suit
h) Continue statements
i) Break statements
j) Pass statements
k) Assert statements
l) Return statements
↷If... statements
Syntax:- if condition:
Statements
Here, at first the condition is
tested if the condition is True then the statements are executes. Otherwise
statements are not executes and it start to execute next objects.
In the given syntax observe colon (:). It helps to separate condition and statement body.
Program - #1
Program - #1
↷If...else... statements
Syntax:- if condition:
statements1
Else:
statements2
Here, at first the condition is
tested if the condition is True then the statements1 are executes otherwise else statements are executes.
Program - #2
Program - #2
Indentation
Indentation is very important in Python. It refers to the spaces that are used in the beginning of a statement. The statements with same indentation belongs in the same group called suit.↷If...elif...else… statements
Syntax:- if condition1:
statement1
elif:
condition2:
statements2
elif:
condition3:
statements3
............................
............................
Else:
Statement
Here, At first the condition is
tested if the condition is True then the statement1 are executes, If the
condition is false then it test the condition2, If the condition2 is True then
the statement2 is executes otherwise it tests the next condition and it
is continue as same, till the end. If all the condition are False then it executes
the else statements. Using else in this control statements are optional but it
is better to use this in programs.
Program - #3
Program - #3
When we need to executes statements
more than a time, then we needs to use loop control statements.
a) While loop
Syntax:- while condition:
Statements
Here, at first the condition is
tested, If the condition is True then the statements are executes and again the
condition is tested, if the condition is True then again statements are
executes and it happens again and again till the condition becomes False. When
the condition becomes False then next objects starts to execute.
Program - #4
Program - #4
↷For loop
Syntax:- for var in sequence
name:
Statements
Here, at first single element is
taken from sequence and it stored in the var name and then statements are
executes. After execution of statements again another element is taken from
sequence and executes the statements and it happens again and again till all
the elements are taken from sequence.
Program - #5
Program - #5
↷Nested loop
Loop inside another loop is called
nested loop.
➣We can understand this loop by the
help of given program.
Program - #6
Program - #6
Here, execution starts from the first loop. So, i contains '0' first, then it executes second loop completely (give output j =0,1,2,3,4,5) and again it executes the first loop and this time i value will be 1 and again it executes the second completely and it happens till the first loop executes completely.
Syntax:-
➭In While loop
While condition:
statement1
Else:
statement2
Program - #7
Program - #7
➭In For loop
For var in sequence name:
statement1
Else:
statement2
Program - #8
Program - #8
Here, in both cases after the
execution of loop, else suit must be
executes.
***--------------------------------------------***
Comments
Post a Comment