Page 234 - CA_Blue( J )_Class9
P. 234
13. Write the difference between a for loop and a while loop?
Ans.
For Loop While Loop
In for loop, initialisation, test condition and In while loop, initialisation, test condition
increment or decrement are in the same line. and increment or decrement are written in
separate lines.
This loop executes a set of statements This loop is used to execute set of statements
repeatedly for a fixed number of times. repeatedly based on the truth of a condition, if
the number of iterations is not known.
14. State the difference between an entry-controlled loop and an exit-controlled loop.
Ans. Entry control loop Exit control loop
Test condition is checked before the loop body Test condition is checked after the loop body
gets executed. gets executed
Example for and while Example do-while
15. Write one similarity and one difference between a while loop and a do-while loop.
Ans. While loop Do-while loop
Similarity
This loop is used when the number of This loop is used when the number of
iterations are not known. iterations are not known.
Difference
If the test condition is false, then the loop will The loop will execute at least once, even if
not execute. So, it is Entry control loop. the test condition returns false. So, It is Exit
Control Loop
16. What is the difference between a break statement and a continue statement when they occur in a loop?
Ans.
break continue
Used to exit from a loop. Used to go to the next iteration in the loop
for(i=1;i<=5;i++) for(i=1;i<=5;i++)
{ {
if(i%2==0) if(i%2==0)
break; continue;
System.out.print(i); System.out.print(i+ " ” );
} }
Output will be : 1 Output will be : 1 3 5
17. Using a while loop, give an example of an infinite loop.
Ans. Example:
int i;
i=1;
while(i>=1)
{
System.out.println(i);
}
18. Using a for loop give an example of a null loop.
Ans. Example:
for(int i=1;i<=1000;i++)
{ }
232 Touchpad Computer Applications-IX

