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

Assign1-classX

Uploaded by

deepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views

Assign1-classX

Uploaded by

deepa
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

ASSIGNMENT 1

ITERATIVE CONSTRUCTS IN JAVA

Question 1

What is 'for' loop? What are the parameters used in 'for' loop?

for loop is an entry-controlled loop. The following parameters are commonly used in a
for loop:

An initial value for the loop control variable.

A condition—loop will iterate as long as this condition remains true.

An update expression to modify the loop control variable after every iteration.

Body of the loop which consists of the statements that needs to be repeatedly executed.

Question 2

Define the following with their constructs:

(a) Entry controlled loop

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 loop. for and while loops are entry-
controlled loops.

(b) Exit controlled loop

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.

Question 3

Write down the syntax of:

(a) do - while

do {
//loop-body
} while (condition);
(b) while loop

while (condition) {
//loop-body
}

Question 4

What is the purpose of using

(a) break statement


break statement is used to unconditionally jump out of the loop

(b) continue statement in a program?

continue statement is used to unconditionally jump to the next iteration of the loop,
skipping the remaining statements of the current iteration.

Question 5

Distinguish between while and do-while loop.

while do-while

It is an entry-controlled loop. It is an exit-controlled loop.

It is helpful in situations where numbers of It is suitable when we need to display


iterations are not known. a menu to the user.

Question 6

What is meant by an infinite loop? Give an example.

A loop which continues iterating indefinitely and never stops is termed as infinite loop.
Below is an example of infinite loop:

for (;;)
System.out.println("Infinite Loop");

Question 7

State one difference and one similarity between while loop and do-while loop.

Similarity — Both while and do-while are suitable in situations where numbers of
iterations is not known.
Difference — while is an entry-controlled loop whereas do-while is an exit-controlled
loop

You might also like