Iteration Through Loops

In a looping statement, the statements execute repeatedly till the result of a test condition remains true. A loop consists of the following parts: initialisation expression, test expression and update expression.

There are three types of looping statements: for loop, while loop and do-while loop. The for loop is a shorthand, which combines initialisation, condition and increment in one statement. Any loop statement can be used to create an infinite loop. If a loop does not contain any statement in its loop-body, it is said to be an empty loop. The while loop is an entry-controlled loop, the loop iterates when the expression evaluates to TRUE. The do-while loop is an exit-controlled loop, in which the loop body is executed first and the condition is checked at the end. A loop placed inside another loop of the same type is called a nested loop.

To Access the full content, Please Purchase

  • Q1

    Write code to print the numbers 4 - 14 on a single line.

    Marks:2
    Answer:

    int num = 4;

    while (num <=14)

    {

    System.out.print(( num++) + " ");

    }

    View Answer
  • Q2

    Which loop should be used when loop is repeated until certain condition is met?

    Marks:2
    Answer:

    A While loop must be used when certain condition is to be checked. If the condition is false at the beginning of the loop, the loop is never executed.

    View Answer
  • Q3

    What is empty loop? Give example.

    Marks:2
    Answer:

    An empty loop does not contain any statement in its loop body.

    Example: for (i=10;(i>=0);--i);

    View Answer
  • Q4

    How do you write an infinite loop using the while statement?

    Marks:1
    Answer:

    while (true) {

    }

    View Answer
  • Q5

    Write an infinite loop using for statement.

    Marks:1
    Answer:

    for ( ; ; )
    {

    }

    View Answer