Repetition Statement Question Answer
Looping in Java – While, For and Do-While
Topics: Control Flow – Repetition Structure while, do-while, and for loop
Que 1. What is Repetition Structures / Looping?
Answer: The repetition structure allows to perform the same sequence of statements repeatedly until some condition is met.
The ability of a computer to perform the same set of actions again and again is called looping
Que 2. What is the body of loop?
Answer: The sequence of statements that is repeated again and again is called the body of the loop.
Que 3. What is the test condition of loop?
Answer: The test conditions is an expression, which determine whether a loop is entered or exited and it is constructed using relational and logical operators.
Que 4. What is the Iteration?
Answer: A single pass through the loop is called an iteration.
Que 5. How many looping statements are in Java?
Answer: There are three looping statements in Java.
(a) while statement
(b) for statement
(c) do while statement
Que 6. How many types of loop?
Answer: There are two types of loop – Entry Controlled loop and Exit controlled loop.
Que 7. What is entry controlled loop?
Answer: A loop which check the test condition before the execution of the body is called Entry controlled loop.
Que 8. What is exit controlled loop?
Answer: A loop which check the test condition after the execution of body is called Exit controlled loop.
Que 9. Give example of entry controlled and exit controlled loop.
Answer: In Java,
- Entry controlled loop – while and for loop
- Exit controlled loop – do-while loop
Que 10. Differentiate between entry controlled and exit controlled loop.
Answer: Entry controlled loop vs Exit controlled loop
Entry controlled loop | Exit controlled loop |
In this loop, condition / test expression evaluates before the execution of body | In this loop, condition/test expression evaluates after the execution of body. |
In this body of loop does not execute if test condition is false | The body of loop executed at least one time, if the test condition is false. |
Que 11. Explain the element of loop in Java?
Answer: In Java, loop has four components.
- Initialization: Initialization statement is use to initialize a control variable.
- Test condition/expression: A test condition/expression, which check the condition based on control variable.
- Body of loop: The set of statements which execute repeatedly, till the test condition is true.
- Update statement: Update statement is use to change the value of control variable, so that condition becomes false after finite number of times.
Que 12. What is control variable in a loop?
Answer: A variable that, control the execution of loop called control variable. Generally, condition or test expression is based on control variable.
Que 13. What do mean by infinite loop?
Answer: A non-terminating loop is called infinite loop. The loop which runs forever and never exits. This happens when the test condition is always true.
Que 14. What is the while statement?
Answer: The while statement evaluates the body of the loop till the test condition is true. It checks the condition before executing the body of a loop. It is an entry-controlled loop.
The structure of the Java while statement is as shown:
Initialization;
while (expression or test condition) {
statements // body of loop
update_statement
}
Que 15. Write a java program to print the numbers from 1 to 5 using a while loop.
Answer:
public class PrintNumber1to5 {
public static void main (String[ ] args) {
int number = 1;
while (number <= 5) {
System.out.println (number);
++number;
}
}