0% found this document useful (0 votes)
7 views15 pages

Session6 Loops

The document covers loops in Java, detailing the while loop, for loop, enhanced for loop, and do-while loop. It provides syntax and examples for each type of loop, illustrating their functionality and use cases. The enhanced for loop is specifically noted for iterating through arrays and collections without modifying them or tracking indices.

Uploaded by

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

Session6 Loops

The document covers loops in Java, detailing the while loop, for loop, enhanced for loop, and do-while loop. It provides syntax and examples for each type of loop, illustrating their functionality and use cases. The enhanced for loop is specifically noted for iterating through arrays and collections without modifying them or tracking indices.

Uploaded by

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

Course Code: 19CSC313A

Course Title: Programming Paradigms

Lecture 6:
loops in Java

Course Leader: M Shona

1
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Loops in Java

• Looping in programming languages is a feature which facilitates the


execution of a set of instructions/functions repeatedly while some
condition evaluates to true.
 while
 for
 Enhanced for loop
 do-while

2
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
While loop

While loop starts with the checking of condition. If it evaluated to


true, then the loop body statements are executed otherwise first
statement following the loop is executed. For this reason it is also
called Entry control loop
Syntax :
while (boolean condition)
{
loop statements...
}

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

public class MainClass {

public static void main(String[] arg) {


int limit = 10;
int sum = 0;
for (int i = 1, j = 0; i <= limit; i++, j++) {
sum += i * j;
}
System.out.println(sum); Output:
330
}
}
8
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Enhanced for loop – for-each loop

In Java, the enhanced for loop is used to iterate through elements of


arrays and collections (like ArrayList). It is also known as for-each loop
Used for traversing an array
• for-each loops are not appropriate when you want to modify
the array
• for-each loops do not keep track of index. So we cannot obtain
array index using for-Each loop
• for-each only iterates forward over the array in single steps

9
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Syntax – Enhanced for loop

• array - an array or a collection of items


• Variable - each item of array/collection is assigned to this
variable
• typeName - the data type of the array/collection
10
Faculty of Engineering & Technology ©M. S. Ramaiah University of Applied Sciences
Example 1 – for-each loop
public class enhancedforloop
{
public static void main(String args[])
{
String array[] = {"Ron", "Harry", "Hermoine"};

//enhanced for loop


for (String x:array) Output:
{ Ron
System.out.println(x); Harry
} Hermoine

}
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

You might also like