Session6 Loops
Session6 Loops
Lecture 6:
loops in Java
1
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Loops in Java
2
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
While loop
3
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example for while loop:
class whileLoopDemo
{ public static void main(String args[])
{
int x = 1;
// Exit when x becomes greater than 4
while (x <= 4)
{ Output:
System.out.println("Value of x:" + x); Value of x:1
// Increment the value of x for Value of x:2
// next iteration Value of x:3
Value of x:4
x++;
}
}
} 4
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
For loop
Syntax:
for (initialization condition; testing condition; increment/decrement)
{
statement(s)
}
Example1 for infinite loop:
for(; ;)
{
System.out.print(“Infinite loop”)
}
5
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example2:
class forLoopDemo
{
public static void main(String args[])
{
int x;
// Exit when x becomes greater than 4
for (x=1;x <= 4;x++)
{
System.out.println("Value of x:" + x);
}
}
}
6
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example3
we can have multiple variable declaration in for loop.
public class Example3
{
public static void main(String[] args)
{
// x is integer
int x = 0;
long y = 10; for (y = 0, x = 1; x < 5; x++)
{ Output:
System.out.print(x + " "); 1234
} }
}
7
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example 4
9
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Syntax – Enhanced for loop
}
11
}
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example2-for-each loop
class Main {
public static void main(String[] args) {
// an array of numbers
int[] numbers = {3, 4, 5, -5, 0, 12};
int sum = 0;
// iterating through each element of the array
for (int number: numbers) {
sum += number;
}
System.out.println("Sum = " + sum);
}
}
Output: 19
12
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
do while
do-while check for the condition after executing the statements of the
loop body.
Syntax:
do
{
statements..
}
while (condition);
13
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example – do-while
class dowhileLoopDemo
{
public static void main(String args[])
{
int x = 1;
// Exit when x becomes greater than 4
do
{
System.out.println("Value of x:" + x);
// Increment the value of x for next iteration Output:
x++; Value of x:1
Value of x:2
} while (x <= 4);
Value of x:3
} Value of x:414
}
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example2 –do-while
class Hello {
// Main driver method
public static void main(String args[])
{
// Declaring and initialization expression
Output:
int i = 1; Hello World
do { Hello World
Hello World
System.out.println("Hello World"); Hello World
i++;
}
while (i < 5);
}
}
15
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences