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 - #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.                                      ...

Python tutorial - #3 (Docstrings and Data-Type)

Docstrings = If we write string inside triple single quote (‘’’----‘’’) or triple double quote (“””----“””) and if these strings are written as first statements in a module, function, class or a method. Then these strings are called Documentation strings or simply Docstrings. These strings are useful to create an A.P.I documentation file from python program, An A.P.I (Application Programming Interface) documentation file is a text file or HTML file that contains the description of all features of software or product. When software is created it is the duty of programmer to create docstrings file in which they explain each and every class, Module, function etc. properly. It helps the user of the product (software) to use this smoothly. Representing Binary, Octal and Hexadecimal number. ➠In python, Binary number are represented by adding 0 (zero) B or 0 (zero) b as prefix to each binary number. Example: - 0B11001, 0b00101 etc.       ...

C tutorial #lecture 3

Here you will be introduced to the comments in C and some basic arithmetic operations on variables and you will learn various shorthand operators. Let's get started- Comments in C : Comments in C/C++ and in any other programming languages serve the purpose of improvement of readability of the code.  Comments are enclosed within /*-----------*/. This  symbol is used for multiline comments. For single line comments we use  // symbol  ( two backslashes) The rules of programming language aren't applicable within comments. The text doesn't appear in the output of the code but it appears in the source code. It just makes the program readable for the coder. This is a C code to illustrate comments.  Add caption       Output.  Nesting of comments : Comments written with /*----/*---*/--------*/  can't be nested and are invalid but a comment with  /*------//------*/ can be nested.  The f...