0% found this document useful (0 votes)
23 views

Class 9-Python - Chapter 5 - loops (while and do while loop)

sa

Uploaded by

kids11040109
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
23 views

Class 9-Python - Chapter 5 - loops (while and do while loop)

sa

Uploaded by

kids11040109
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

CLASS 9

CHAPTER 5 CONTD.
ITERATION CONSTRUCTS (LOOPS)
PART-2

• for loop
• while loop
• do ... while loop
while loop
•It is known as entry controlled or pre tested loop. Ie the condition is first
tested before the loop is executed.
•A while loop repeats as long as the given condition is True.
•If the condition evaluates to false, the statements within the body of the loop
won’t execute at all and the loop terminates.

while (condition)
{
code to be executed
}
while loop
<script type="text/JavaScript">
var i = 1;
while (i<=5)
{
document.write("<br> i = "+i);
i++;
}
</script>
Predict the output

a)
i=4;
Convert using for loop.
while(i<25)
i = 5;
{
s = 0;
document.write( i + ",");
while (i<25)
i+=4 ;
{
}
s += i;
i += 5;
b) i = 4;
}
while (i>=4)
{
document.write( i);
i += 10;
}
Which of the following is true about a while loop?

a) Executes at least once


b) The condition is tested in the beginning only
c) The loop executes as many times as the condition is true
d) None of the above
do ... while loop

The do...while loop is a variant of the "while" loop.


This is a type of post tested or exit controlled loop, as the condition is checked after
the loop statements are executed. First it will execute the block of code, and then it
will check the condition and will repeat the loop as long as the specified condition is
true.
Therefore, this type of loop executes at least once.
The syntax is:
do
{
code to be executed
}
while (condition);
do .. while loop
<script
type="text/JavaScript">
var i = 1;
do
{
document.write(i+”<BR
>”);
i++;
}while(i<=5);
</script>
do ... while loop

<script type="text/JavaScript">
var x = 8;
do
{
document.write("<br> x = "+x);
x++;
}
while (x<5)
</script>
1. Write the output of following code:
<script language="javascript">
var i = 1, number = 0;
do
{
number = number + i
i=i + 1;
alert(i)
} while(i < 5);
</script>
How many times, the following loop gets executed?
i=0;
do
{
…….
…….
}while (i> 20);

a. Zero times
b. Infinite number of times
c. Once
d. none of these
• Write scripts for the following (WHILE LOOP)
a) Print the cubes for numbers between 5 and 10
b) Accept 2 numbers and print all the numbers in the
range.(include the nos. accepted in the range)
c) Print the sum of the even numbers between 20 and 30

You might also like