Control Statement & Decision Making Statement In Python

Control Statements In Python:


In order to control the flow of program,we use conditional statements  and looping.Python provide various tools for flow control. Some of them are if , if .. elif .. else, if..else,while ,for , switch, pass, range, break, else, continue, function etc.

●While loop
A while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.
Syntax-
while expression:
statement(s)

Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. When the condition becomes false, program control passes to the line immediately following the loop.


●for loop
The for loop in Python is used to iterate over a sequence or other iterable objects.
Syntax-
for value in sequence:
  Body of for

Here, value is the variable that takes the value of the item inside the sequence on each iteration.Loop continues until we reach the last item in the sequence. The body of for loop is separated from the rest of the code using indentation.


●if statement
It executes a set of statements conditionally, based on the value of a logical expression.
Syntax-
if expression :
    statement_1
    statement_2
    ......

●if else statement
In Python if .. else statement, if has two blocks, one following the expression and other following the else clause.
Syntax-
if expression :
    statement_1
    statement_2
    ....
 else :
   statement_3
   statement_4


●Nested if else statement:
In general nested if-else statement is used when we want to check more than one conditions. Conditions are executed from top to bottom and check each condition whether it evaluates to true or not. If a true condition is found the statement(s) block associated with the condition executes otherwise it goes to next condition.
Syntax-
if expression1 :
         if expression2 :
          statement_3
          statement_4
        ....
      else :
         statement_5
         statement_6
        ....
     else :
   statement_7
                 statement_8


●Continue Statement
It returns the control to the beginning of the loop.
Example-
for letter in 'hello':
    if letter == 'e' or letter == 'o':
         continue
    print 'Current Letter :', letter

OUTPUT-
          Current Letter : h
          Current Letter : l
          Current Letter : l

Comments