Class 9-Python - Chapter 5 - loops (while and do while loop)
Class 9-Python - Chapter 5 - loops (while and do while loop)
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?
<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