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

Control structures in c #part 1 ( if statement)

The control strucures are the statements that are used to alter the flow or sequence of excecution of program.  There are basically two types of control strucures:                   1. Decision making statements a) if statement          b) if -else statement          c) switch statement 2. Loop constructs: a) for loop     b) while loop           c) do-while loop We will also study about break, continue and go to in subsequent lectures Here we will cover if statement.  Firatly u need to know the symbols for various conditions they are called relational operators.  1. ==  means here "equal to " 2. <= means here "less than Or equal to" 3. >= means here "greater than Or equal to" 4.  ! = means " Not equal to" Now u are euipped  enough to learn loops.  Let's be...

#lecture 2 coding of your First C program

Structure of a C program: #include<header file> Int main()  { ----; ---; ---; } The # sign tells the compiler to include the header file(generally iostream) Header file is included as it is the standard library of all readymade C commands that are fed to the compiler. " Iostream " stands for input output library and has various commands like cin>>, cout<<, basic arithmetic operations.  The main() is very crucial in C. The Main() is a C function and just like a calculator returns a value, the main function returns integer value. So we write int before main. The compilers like turbo C/C++ even allow us yo return no value from main function i.e. void So we sometimes write void main().  The sequence of commands that the compiler has to follow are written between the curly brackets.  The statement in C are ended with ' ; ' Before writing your first C program u need to equip with these things first!   Data t...