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

Loops in Java

The document provides an overview of loops in Java, explaining their fundamental role in programming through repetitive actions. It details various types of loops including simple for loops, nested for loops, for-each loops, while loops, and do-while loops, along with their syntax and use cases. Additionally, it covers the Java if-else statement, break statement, and switch statement, highlighting their functionalities in controlling program flow.

Uploaded by

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

Loops in Java

The document provides an overview of loops in Java, explaining their fundamental role in programming through repetitive actions. It details various types of loops including simple for loops, nested for loops, for-each loops, while loops, and do-while loops, along with their syntax and use cases. Additionally, it covers the Java if-else statement, break statement, and switch statement, highlighting their functionalities in controlling program flow.

Uploaded by

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

• Loops are a fundamental concept in programming.

• Consider your daily routine, you wake up, you brush, you wear clothes and then
head off to work, come back, eat and then sleep off. Again, the next day, you do
the same things in the same order and go to sleep again. This cycle keeps on
repeating. This concept of repetitive actions performed time and again is called a
loop.
• Looping in Java is defined as performing some lines of code in an ordered fashion
until a condition is false.
• The condition is important because we do not want the loop to be running forever.
1. Simple for Loop
2. Java Nested for loop
3. For-each or Enhanced for Loop
Simple for Loop

• Java for loop consists of 3 primary factors which define the loop itself.
• These are the initialization statement, a testing condition, an increment or
decrement part for incrementing/decrementing the control variable.
Nested for Loop

• If we have a for loop inside another loop, it is known as nested for loop.
• The inner loop executes completely whenever outer loop executes.
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 based on elements and not the index. It returns element one by one in the
defined variable.
While Loop

• The Java while loop is used to iterate a part of the program repeatedly until the
specified Boolean condition is true.
• As soon as the Boolean condition becomes false, the loop automatically stops.
• The while loop is considered as a repeating if statement.
• If the number of iteration is not fixed, it is recommended to use the while loop.
• Syntax:
while (condition){
//code to be executed
Increment / decrement statement
}
do-while Loop

• The Java do-while loop is used to iterate a part of the program repeatedly, until the
specified condition is true.
• If the number of iteration is not fixed and you must have to execute the loop at least
once, it is recommended to use a do-while loop.
• Java do-while loop is called an exit control loop.
• Therefore, unlike while loop and for loop, the do-while check the condition at the
end of loop body.
• The Java do-while loop is executed at least once because condition is checked after
loop body.
Java If-else Statement

• The Java if statement is used to test the condition.


• It checks Boolean condition: true or false.
• various types of if statement in Java.

• if statement
• if-else statement
Break Statement

• When a break statement is encountered inside a loop, the loop is immediately


terminated, and the program control resumes at the next statement following the
loop.
• The Java break statement is used to break loop or switch statement.
• It breaks the current flow of the program at specified condition.
• In case of inner loop, it breaks only inner loop.
• We can use Java break statement in all types of loops such as for loop, while loop
do-while loop, switch case.
Java Switch Statement

• In simple words, the Java switch statement executes one statement


from multiple conditions.
• It is like an if-else-if ladder statement.

You might also like