Looping Control Structure

Question: Explain the term loop with an example.

Answer: A loop is a sequence of instruction s that is continually repeated until a certain condition is reached.

The following program illustrates the use of for loop.

public class Loops

{

static void NaturalNumbers()

{

int i;

for(i=1 ; i<=10 ; i++)

{

System.out.println(i);

}

}

}

Now let us learn how the components of the loop gets executed.

Step 1:

At the beginning the initialization expression is executed i.e. i=1 which assigns 1 to i.

Step 2:

Then the test expression is checked i.e. i<=10 which checks whether the value of i is less than or equal to 10.

Step 3:

Since the test expression is true it enters the body of the loop i.e. System.out.println(i); is executed which prints the current value of i in the next line.

Step 4:

After executing the loop-body, the update expression i.e. i++ is executed which increments the value of i. (The value of i becomes 2 after the execution of i++, since initially i was 1).

Step 5:

After the update expression is executed, the test-expression is again evaluated. If it is true the sequence is repeated from step number 3, otherwise the loop terminates.


Question: Name the Elements that Control a Loop.

Answer: Every loop has elements that control its execution. These elements can be grouped under the following in general:

1. Initialization Expression(s). Before entering a loop, the variable that controls the execution of it is called initialization. The initialization of a variable may be inside a loop or outside it. Whatever it may be it is executed only once before the iteration starts.

2. Test Expression. The test-expression (also called test-condition) is a condition, which decides whether the loop body will be executed or not. Till the test expression evaluates to true, the loop body gets executed otherwise the loop is terminated.

3. Update Expression(s). The update expression(s) change the value(s) of the control variable(s). The update expression(s) is usually made to be executed; before the next iteration.

4. The Body of the Loop. The statements that are executed repeatedly (as long as the, test expression is true) form the body of the loop.


Question: What are the three types of looping statement available in Java?

Answer: The three types of looping statements are: for, while and do-while.

Question: Classify loops depending upon the priority in which a test-expression is evaluated.

Answer: Depending upon the way a test-expression is evaluated in a loop it can be classified under the following:

Entry controlled loop

Exit control loop

In an entry controlled loop, the test expression is evaluated before entering into a loop whereas

in an exit controlled loop, the test expression is evaluated before exiting from the loop.


Question: Give the general syntax of a for-loop.

Answer: The syntax of the for loop statement is

for (initialization expression(s); test expression; update expression(s))

{

body of the loop;

}


Question: Give an example to show how multiple initialization and updation are performed in a forloop.

Answer:

int i, j;

for(i=1, j=10 ; i<=10 ; j-- , i++)

{

System.out.println(“i=” +i+ “ ”+ “j=”+j);

}


Question: What is an empty for-loop. Give an example.

Answer: If the loop doesn’t contain a body, it is called an empty loop. For example,

for(i=1;i<=100;i++);

Notice the semicolon at the end of the parenthesis, indicating that the for loop does not have body and therefore no statement is repeatedly executed again and again.


Question: State one similarity and one difference between while and do while loop.

Answer: 

Similarity: Both the loops are generally used if the number of iterations are not known.

Difference: The while loop is an entry controlled loop and the do-while is an exit controlled loop.


Question: State one similarity and one difference between while and for loop.

Answer: 

Similarity: Both are entry controlled loops.

Difference: Initialisation, test expression and updation can be written at the beginning of a forloop, whereas in a while loop, the initialisation is written outside the loop, and the updation inside the body of the loop.


Question: What is meant by an infinite loop ? Give one example.

Answer: A loop whose test expression always results to true is an infinite loop. For example,

for(int i=1;i!=0;i++)

{

}


Question: Give the general syntax of a while-loop. How do you create infinite loops using a while-loop structure?

Answer: The syntax of a while loop is,

while (condition or test-expression)

{

body-of-the-loop;

}


Question: Give the general syntax of a do-while loop. How do you create infinite loops using do-while loop structure?

Answer: The syntax of the do-while loop is:

do

{

statement;

}while (condition);

To create an infinite you need to make the condition always result to true. For example,

do

{

System.out.println(“Hello”);;

}while (true);


Question: Compare loops according to its usage.

Answer: The for loop is appropriate when you know in advance how many times the loop will be executed, i.e., the first and the last limit is already known. The other two loops while and do-while loops

are more suitable in situations when it is not known in advance when the loop will terminate. The while should be preferred when you may not want to execute the loop-body even once (in case the test condition is false), and the do-while loop should be preferred when you’re sure you want to execute the loop-body at least once.


Question: What are Nested-loops? Give an example of a nested for-loop to show its functionality.

Answer: 

int x , y;

for(x=1;x<=3;x++)

{

for(y=1; y<=3 ; y++)

{

System.out.println(x+ “ ” +y);

}

}


Question: Name two jump statements and their use.

Answer: The ‘break’ statement is used to exit from a loop or switch block. The ‘continue’ statement is used to skip the remaining statements in a loop and continue with the next iteration.