0% found this document useful (0 votes)
9 views

Iterative construct in java theory

The document provides an overview of iterative constructs in Java, explaining the concept of loops and their types, including for, while, and do-while loops. It details the parameters for creating loops, the purpose of break and continue statements, and the differences between entry-controlled and exit-controlled loops. Additionally, it discusses inter-conversion of loops, finite and infinite loops, and includes multiple-choice questions for assessment.

Uploaded by

rishiprince823
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
9 views

Iterative construct in java theory

The document provides an overview of iterative constructs in Java, explaining the concept of loops and their types, including for, while, and do-while loops. It details the parameters for creating loops, the purpose of break and continue statements, and the differences between entry-controlled and exit-controlled loops. Additionally, it discusses inter-conversion of loops, finite and infinite loops, and includes multiple-choice questions for assessment.

Uploaded by

rishiprince823
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 7

Iterative Constructs in Java

1. What do you understand by iterative process? How can it be resolved by


using loop?
Ans: It is a construct in which a statement or a set of statements are
repeatedly executed till the condition is satisfied.
It can be resolved by using loops because it allows us to easily specify what
task needs to be executed and for how many times.
2. Explain for loop with an example.
Ans: The for loop is an entry-controlled loop which is suitable when the
number of iterations is fixed.
Example:
for(int i = 1; i <= 10; i++){
System.out.println(“Good Morning”);
}
In above segments “Good Morning” message print for 10 times.
3. Name the different types of loop statements.
Ans: Different types of loop statements are:
a) for loop
b) while loop
c) do-while loop
4. What are the parameters needed to create a for loop?
Ans: Following are the parameters needed to create a for loop:
a) Initial value for the counter variable.
b) Test condition to terminate the loop at a certain point.
c) Update expression to modify the counter variable after each
iteration.
d) Loop body to be executed in each iteration.
5 Define the following with their constructs:
(a) Entry controlled loop
Ans: An entry-controlled loop checks the condition at the time of entry.
Only if the condition is true, the program control enters the body of the

1
loop. for and while loops are entry-controlled loops.

(b) Exit controlled loop


Ans: An exit-controlled loop checks the condition after executing its body.
If the condition is true, loop will perform the next iteration otherwise
program control will move out of the loop. do-while loop is an exit-
controlled loop.

6. Write down the general format of:


(a) for loop
Ans: for(initial value; test condition; step value){
task
}
(b) do-while loop
Ans: do{
task
}while(test condition);

(c) while loop


Ans: while(test condition){
task
}

7. What is the purpose of using:


(a) break statement:
Ans: When a break statement is encountered inside a loop, the loop is
terminated and program control resumes at the next statement following
the loop. Here is a simple example:
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
break; // terminate loop if i is 5

2
}

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


}
Output: 1 2 3 4
(b) Continue statement:
Ans: When a continue statement is encountered inside the body of a loop,
remaining statements are skipped and loop proceeds with the next iteration.
Here is a simple example.
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue; // skip next statement if i is even
}

System.out.println(i + " ");


}
Output: 1 3 5 7 9

8. What do you understand by inter-conversion of loops?


Ans: Inter-conversion of loops is about converting one form of repetitive
structure to another form to model our program logic as per our
convenience.
9. What are the different ways to inter-convert the loops? Name them.
Ans: Following are the different ways to inter-conversion of loops:
a) for loop to while loop
b) for loop to do-while loop
c) do-while loop to while loop
d) do-while loop to for loop
e) while loop to do-while loop
f) while loop to for loop
10. Define the following:
(a) Finite loop:
Ans: Loop in which the statements run for a fixed number of times.
For example: for (int i=1;i<=10;i++)
3
(b) Delay loop:
Ans: Loop that is used to create a delay in the execution by creating a null loop.
For example:
S=a+b;
For(int i=1;i<=10;i++)
{
}
System.out.println(“sum of two numbers”+s);
In the above example for loop repeats 10 times without carrying any useful
operation. Thus it will delay the execution of print statement. Another way of
creating a null loop is to place a semicolon (;) after closing bracket in a for loop. It
indicates that the loop does not execute any statement or the body of the loop is
not available.
For(int i=1;i<=10;i++);

(c) Infinite loop:


Ans: Loop in which a looping structure will never come to an end, created by
omitting one or more parameters.
For example: for(int i=1;i<=10;)
{
Body of the loop
}
In the looping structure the update or step value is missing.

(d) Null loop:


Ans: Loop which doesn’t include any statement to be repeated. Basically, a null
loop is used to create a delay in the execution.
11. 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

4
12. 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.
13. Distinguish between:
for and while loop
Ans; The for loop is a suitable choice when the number of iterations is fixed,
whereas the while loop is a suitable choice when the number of iterations
is not fixed.
14: State one similarity and one difference between while and do while
loop.
Ans: 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.
15. State one similarity and one difference between while and for loop.
Ans. Similarity: Both are entry controlled loops.
Difference: Initialization, test expression and updation can be written at the
beginning of a for loop, whereas in a while loop, the initialization is written
outside the loop, and the updation inside the body of the loop.

16. Give two differences between step loop and continuous loop.
Ans: Step loop is a system of creating a loop where the control variable is updated
by a given value after each iteration. The loop starts with an initial value, repeats
execution of the statements by updating the value with the given step.
Continuous loop is a type of looping structure in which the control variable is
updated only by 1 after each iteration. The loop is terminated as soon as the
control variable exceeds the last limit.

17. 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

5
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.
18. Multiple Choice Questions
1. When the statements are repeated sequentially for a number of times in a
program, the construct is known as:
(a) iteration
(b) sequence
(c) selection
(d) none
2. Which of the following statement is an exit-controlled loop?
(a) for
(b) while
(c) do-while
(d) if-else
3. Which of the following loop does not execute even once if the condition is
false in the beginning?
(a) do-while
(b) while
(c) for
(d) nested loop
4. To execute a loop 10 times, which of the following statement satisfies?
(a) for(i = 6;i <= 26; i = i + 2)
(b) for(i = 3; i <= 30; i = i + 3)
(c) for(i = 0; i < 10; i = i++)
(d) all of the above
5. Which of the following statement uses multiple branches?
(a) loop
(b) continue
(c) switch
(d) break
6. Which of the following loop checks the condition first and then execution
begins?
(a) do-while
(b) do

6
(c) while loop
(d) for
7. To find the sum of whole numbers up to 10, a loop runs:
(a) once
(b) ten times
(c) eleven times
(d) any number of times
8. How many times the loop: for(i = 1;; i++) will execute, if there is no
statement to terminate the loop?
(a) 1
(b) 0
(c) infinite
(d) none
9. Which of the following statements uses a case called default?
(a) do-while
(b) while
(c) switch
(d) all of the above
10. A loop statement is given as:
for(i = 10; i < 10; i++){
statement
}
For how many times will the given loop statement be executed?
(a) never
(b) 1 time
(c) 10 times
(d) infinite

You might also like