Concepts of Conditions and Loops
[ICSE Syllabus on this Topic]
Utilization of loops. Fixed number of Iteration. The for Loop, unknown number of Iteration – while, do-while loop, continue, break. Nested Loops.
Q. What are iteration statements? Name the iteration statements provided by Java?
Ans: Iteration statements are statements that allows a set of instructions to be executed repeatedly till some condition is satisfied. The iteration statements provided by Java are: for loop, while loop, do-while loop.
Q. What is the difference between entry controlled and exit controlled loop? or
What is the difference between while and do-while loop?
Ans: while loop is known as entry controlled loop and do-while loop is known as exit-controlled loop. The differences between these two loops are: (1) In while loop the test expression is evaluated at the beginning where as in do-while loop test expression is evaluated at the bottom, after the body of the loop. (2) In while loop if the test expression is false loop does not continued but in do-while what ever the test expression the loop execute at least once.
Q. Explain the difference between break and continue with an example.
Ans: Both statements are used as a jumped statement. But there is a difference between Break and Continue statement. The break statement terminate the loop, but the continue statement skip the rest of the loop statement and continued the next iteration of the loop.
e.g. of Break Statement
e.g. of Continue Statement
Q. Compare and discuss the suitability of three loops in different situation?
Ans: (i) The for loop should be preferred if number of iteration is known beforehand. (ii) The while loop should be preferred if the number iteration is dependent upon some control variable. (iii) The do-while loop should be preferred if the number of iterations is dependent upon user response.
Q. Explain the term for loop with an example.
Ans: In Java the ‘for’ statement is the most common iterative statement. the general syntax of the for loop is,
for(initialization; test-expression; increment)
body of the loop
This loop is executed at initial value, condition and increment. Three statement separated by semi colons are placed with in the parenthesis. for example:
Q. State one similarity and one difference between while and do-while loop.
Ans: Similarity: In both loops there is a chances to forget the increment statement inside the loop. Difference: In while loop the test expression is evaluated at the beginning where as in do-while loop test expression is evaluated at the bottom, after the body of the loop.
Q. What do you meant by an infinite loop? Give an example.
Q. What do you meant by an endless loop? Give an example.
Ans: Infinite loop is an endless loop whose number of iterations are not fixed.
Q. Differentiate fixed and variable iterative type of loops.
Ans: Fixed type of iterative loop is created when the process is to be repeated for defined number of times. Variable iterative loop repeats the process till a given condition is true.
Q. Differentiate Null loop and Infinite loop.
Ans: A Null loop does not contains any statement to repeat where as infinite loop repeats execution of the statements for endless iterations.
e.g. of null loops for(int i=1;i<=10;i++);
e.g. for infinite loop for(int i=10;i>=1;i++)
Q. What do you mean by delay loop?
Ans: A null loop is also called delay loop which does not repeat the execution of any statement but keeps the control engaged until the iterations are completed.