While
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:
The Google meet link for the class today is:
The WHILE loop discussed so far is primarily used for repetitive statements which are repeated if a given condition is met.
----------------------------------------------------------------------------
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:
The WHILE loop discussed so far is primarily used for repetitive statements which are repeated if a given condition is met.
We will first take a recap of the following example
Example
count = 1 # initialization while (count < =5): # Test expression print('The number is:', count) count = count + 1 #increment print "Good bye!"
When the above code is executed,
Initial value of count= 1
|
Value of count
|
print('The number is: ', count)
|
Count=count+1
|
WILL THE LOOP GET EXECUTED?
|
1
|
The number is: 1
|
2
|
Yes
|
|
2
|
The number is: 2
|
3
|
Yes
|
|
3
|
The number is: 3
|
4
|
Yes
|
|
4
|
The number is: 4
|
5
|
Yes
|
|
5
|
The number is: 5
|
6
|
No
|
---------------------------------------------------------------------------------------------------------------------------
If we change the question and print only the odd numbers from 0-10 then
- Keep in mind- ODD NUMBERS between 0 and 10 are-1,3,5,7,9
- count will be initialized to 1 , because 1 is the first odd number
- every time the loop executes, the increment will be with value 2
- condition will be tested for count<=10
count = 1 # initialization
while (count < =10): # Test expression
print('The number is:', count)
count = count + 2 #increment
print "Good bye!"
Initial value of count= 1
|
Value of count
|
print com
|
Count=count+2
|
WILL THE LOOP
GET EXECUTED? |
1
|
The number is: 1
|
3
|
Yes
|
|
3
|
The number is: 3
|
5
|
Yes
|
|
5
|
The number is: 5
|
7
|
Yes
|
|
7
|
The number is: 7
|
9
|
Yes
|
|
9
|
The number is: 9
|
11
|
No
|
If we change the question and print only the EVEN NUMBERS from 0-10 then
- Keep in mind- EVEN NUMBERS between 0 and 10 are-2,46,8,10
- count will be initialized to 2 , because 2 is the first even number
- every time the loop executes, the increment will be with value 2
- condition will be tested for count<=10
count = 2 # initialization
while (count < =10): # Test expression
print('The number is:', count)
count = count + 2 #increment
print "Good bye!"
Initial value
of count= 2 |
Value of
count |
print comm
|
Count=count+2
|
WILL THE LOOP
GET EXECUTED?
|
| 2 |
The number is: 2
| 4 |
Yes
| |
| 4 |
The number is: 4
| 6 |
Yes
| |
| 6 |
The number is: 6
| 8 |
Yes
| |
| 8 |
The number is: 8
| 10 |
Yes
| |
10
|
The number is:
10 |
12
|
No
|
Good Morning Ma''am
ReplyDeleteGood morning mam
ReplyDeleteAshish
Good morning mam
ReplyDeleteSahil
Good morning ma'am
ReplyDeleteTejas
1. WAP to print the following:
ReplyDelete10,7,4,1
2. WAP to print:
20,15,10,5,0
git@stcolumbas.edu.in