Skip to main content

Python tutorial - #6 (Control statements)

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

↷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

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


↷Loop 

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

↷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


↷Nested loop

 Loop inside another loop is called nested loop.

➣We can understand this loop by the help of given program.
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.


↷Else suit

Syntax:-
In While loop
   While condition:
           statement1
   Else:
          statement2
Program - #7

In For loop
   For var in sequence name:
          statement1
   Else:
         statement2
Program - #8

Here, in both cases after the execution of loop, else suit must be executes.

***--------------------------------------------***


Comments

Popular posts from this blog

Python tutorial - #4 (Operators in Python)

OPERATORS IN PYTHON The general purpose of a program is to accept data and perform some operations on the data. The data in the program is stored in variables. The next question is how to perform operations on the data. For example, when a programmer wants to add two numbers, he simply type '+' symbol. This symbol performs addition operation. Such symbols are called operators. ⇨ Operator = An operator is a symbol that performs an operation.  When an operator acts on some variables then it is called operands . Example :- a + b in the given equation '+' operator acts on two operands 'a' and 'b'. If an operator acts on single operands then it is called unary operator . And when an operator acts on two operands then it is called binary operator . And when an operator acts on more than two operands (variable) It is called Tertiary operator . ➠Classifications of Operator -- a) Arthimatic operators b) Assignment operators c) Unary minu...

Python tutorial - #5 (Operators in Python)

Membership Operators This operator helps us to check an element is found in a sequence or not. It gives there result in Boolean value (True or False). ➥There are two types of Membership operator :- a) in b) not in ➢in membership operator This membership operator returns True if an element is found in a sequence and it returns  False When an element is not found in a sequence. Program - #1 ➢not in membership operator This membership operator returns True if an element is not found in a sequence and it returns False if an element is found in a sequence. Program = #2 Identity Operators This operator helps us to check whether two element are same or not. Actually, this operator compares the memory location of two object, if the memory locations are same then objects are also same otherwise the objects are not same.                                      ...