While loop, for loop , break and Continue Statements with example of python programming

Iterative Control:

Iterative control statement is also known as looping statement.  An iterative control statement is control statement providing the repeated execution of a set of instructions. An iterative control structure is a set of instructions and the iterative control statement controlling their execution. Because of their repeated execution.

These are following looping Statements used in python programming-

While loop:

It is many times used, when the number of times the loop to be executed. In this loop first check the condition, if the condition is true then if executes a statement, at the end of loop increment/decrement is done to change in variable value. After execution it again checks the condition and if the condition is found to be true, it again executes the block of statement. Statement is executed continuously until the condition is true, otherwise condition is found false, it exit out of loop.

Syntax:

while test_expression:

    Body of while

Flowchart:


 

 

 

 

 

 

 

 

For Example: Write a program to print 1 to 10 natural numbers

no = 1

while (no<= 10):

   print('The number is:',no)

   no = no + 1

Output: 


 

 

 

 

 

for loop:

The for loop is used to iterate over a sequence (list, tuple, string) or other iterate objects. Iterating over a sequence is called traversal.

Syntax:

            for val in sequence:

                        Body of for

Here, val 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.

Flowchart:

 

 

 

 

 

 

 

For Example: Write a program to demonstrate the use for loop.

for letter in 'ARYAN COMPUTERS':   

   print ('Print Letter :', letter)

Output:

 


 

 

 

 

 

 

Break Statement (terminating loop):

The break statement is used to terminate the loop. Control of the program flows to the statement immediately after the body of the loop. If break statement is inside a nested loop (loop inside another loop), break will terminate the innermost loop.

Syntax:

            break

Flowchart:

 


 

 

 

 

 

 

 

For Example: Write a program to print 1 to 4 number

no = 0

for no in range(10):

   no = no + 1

   if no == 5:

      break    # Here  break  statement is used

   print('Number is: ' + str(no))

Output:


 

 

 

 

Continue Statement: The continue statement is used to ‘continue’ the loop with its next iteration. In other words, it skips any remaining statements in the current iteration and immediately passes the control to the next iteration. It does not terminate the loop rather it only terminates the current iteration of the loop.

Syntax:

            continue

Flowchart: 


 

 

 

 

 

 

 

For Example: Write a program to demonstrate the use of continue statement.

for val in "Aryan Computers":

    if val == "n":

        continue

    print(val)

Output:


 

 

Comments

Popular posts from this blog

operator of python

Precedence of operators and converstion in python programming with subtable example