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

1.3Loops_in_Java

Uploaded by

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

1.3Loops_in_Java

Uploaded by

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

Subject Name

Loops in Java

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Learning Objective

To learn iterative statements in java.

To learn while loop

To learn the for loop.

To learn for-each loop.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

While Loop
A while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a repeating
if statement.
// Initialization
while (test condition)
{
// code block to be executed
// increment or decrement (Optional)
}

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Do –While Loop
A while loop is a control flow statement that allows
code to be executed repeatedly based on a given Boolean
condition. The while loop can be thought of as a repeating
if statement.

do
{
// code block to be executed
}
while (condition);

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

For Loop
for loop provides a concise way of writing the loop structure. Unlike a while
loop, a for statement consumes the initialization, condition and
increment/decrement in one line thereby providing a shorter, easy to debug
structure of looping.
for (Initialization; testing _condition; increment/decrement)
{
// Statements(Code) to be executed
}

Initialization is executed (only ones) before the execution of the code block.
testing condition is the condition for executing the code block.
increment/decrement is executed (every time) after the code block has been
executed.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

For-each Loop
The for-each loop is used to traverse array or collection in
Java. It is easier to use than simple for loop because we
don't need to increment value and use subscript notation.
It works on the basis of elements and not the index. It
returns element one by one in the defined variable.
Syntax
for(data_type variable : array_name)
{
//code to be executed
}

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Break Statement
It terminate from the loop immediately.

Can be used inside a loop,

The control returns from the loop immediately to the first

statement after the loop.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Continue statement
It skips the current iteration of a loop.
Can be used inside any types of loops such as for,
while, and do-while loop.
It will continue the loop but do not execute the
remaining statement after the continue statement.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Quiz

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which Loop construct in Java is best


suited when number of iterations is known
A. for loop

B. while loop

C. do-while loop

D. break statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which Loop construct is used when


number of iterations is not known
A. for loop

B. while loop

C. do-while loop

D. break statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

What is the purpose of continue


statement in loop
A. To exit the loop immediately

B. To skip the current iteration and move to the

next iteration

C. To terminate the program

D. To execute a specific block of code

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

What is the purpose of break


statement in a loop

A. To exit the loop immediately

B. To skip the current iteration and move to the next iteration

C. To terminate the program

D. To execute a specific block of code

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

What is the key difference between


do loop and do while loop ?

A. The syntax used to define the loop

B. The number of iterations performed

C. The condition check timing

D. The ability to use the break statement


103 Java Programming (Dr Anuprita Deshmukh)
1.3 Loops in Java
Subject Name

What is common aspect between do loop


and while do loop?
A. The syntax used to define the loop

B. The number of iterations performed

C. The condition check timing

D. The ability to use the break statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which statement is used to exit loop


prematurely?

A. return statement

B. continue statement

C. break statement

D. exit statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which statement is used to return to


calling program?
A. return statement

B. continue statement

C. break statement

D. exit statement

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which of the following can loop through an


array without referring to the elements by
index?
A. do-while loop

B. for (traditional)

C. for-each

D. while

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Which of the following is best suited


for limited operations?
A. do-while loop

B. for (traditional)

C. for-each

D. while

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Summary
Practical implementation of for loop.

Practical implementation of while loop.

Practical implementation of for-each loop.

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java
Subject Name

Thank You

103 Java Programming (Dr Anuprita Deshmukh)


1.3 Loops in Java

You might also like