while.(contd)



Good Morning boys

Today we will take a step ahead into the concept of looping, with an introduction of the WHILE loop

--------------------------------------------------------------------------------------------------------
By the end of this session, you will be able to:

  • understand the need for while loop
  • Understand the syntax of the WHILE loop
  • Apply the loop to get the desired result
  • Predict the output for a group of statements.
----------------------------------------------------------------------------------------------------------

The Google meet link for the class today is:
----------------------------------------------------------------------------------------------------------
while loop statement in Python programming language repeatedly executes a target statement as long as a given condition is true.

Syntax

The syntax of a while loop in Python programming language is −
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.
In Python, all the statements indented by the same number of character spaces after a programming construct are considered to be part of a single block of code. 
Python uses indentation as its method of grouping statements.

Flow Diagram

while loop in Python

  • Here, key point of the while loop is that the loop might not ever run. 
  • When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.

Example-1

count = 1
while (count < 5):
   print('The count is:', count)
   count = count + 1
print "Good bye!"
When the above code is executed, the following result will be produced:
The count is: 1
The count is: 2
The count is: 3
The count is: 4
Good bye!
The block here, consisting of the print and increment statements, is executed repeatedly until count is no longer less than 5. With each iteration, the current value of the index count is displayed and then increased by 1.

Example-2

count = 1
while (count < 10):
   print('The count is:', count)
   count = count + 2

print "Good bye!"
When the above code is executed, what will be the result produced?  We will discuss before we look in the Python Compiler.

EXERCISE

Write a program to print the following:(even  numbers between 0 and 10)

Comments

Post a Comment

Popular posts from this blog

HOLIDAY HW