0% found this document useful (0 votes)
35 views39 pages

Week4 Lecture 83665

The document discusses different types of loops in programming. There are three main types of loops: for loops, which iterate a fixed number of times; while loops, which iterate as long as a condition is true; and do-while loops, which iterate at least once and then continue as long as the condition is true. The document provides examples of each loop type and also covers the break and continue statements, which allow altering the normal flow control of loops.

Uploaded by

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

Week4 Lecture 83665

The document discusses different types of loops in programming. There are three main types of loops: for loops, which iterate a fixed number of times; while loops, which iterate as long as a condition is true; and do-while loops, which iterate at least once and then continue as long as the condition is true. The document provides examples of each loop type and also covers the break and continue statements, which allow altering the normal flow control of loops.

Uploaded by

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

PROGRAMMING

Lecture 4

Sushil Paudel

erspaudel 1
LAST WEEK

• Conditional Statement – If, If Else, If-ElseIf ladder

• Conditional Statement – Switch

erspaudel 2
TODAY’S TOPIC
• Control Statement Loop

• Types of Loop: For, While and Do-While

• Break and Continue

erspaudel 3
REPEAT

erspaudel 4
CONTROL STATEMENT - LOOPS

• In programming languages, loops are used to execute a set of instructions/functions


repeatedly.

• Loop is used in programming to repeat a specific block of code until certain


condition is met (test expression is false).

• Loops are what makes computers interesting machines. Imagine you need to print a
sentence 50 times on your screen.

• Well, you can do it by using print statement 50 times (without using loops).

• How about you need to print a sentence one million times?

erspaudel 5
LOOP

6
TYPES OF LOOPS

There are three types of loops in java.

• for loop

• while loop

• do-while loop

erspaudel 7
FOR LOOP

• The Java for loop is used to iterate a part of the program several times. If the
number of iteration is fixed, it is recommended to use for loop.

• There are two types of for loops in java.

• Simple For Loop

• For-each or Enhanced For Loop

erspaudel 8
SIMPLE FOR LOOP

for(initialization; condition; incr/decr){

//statement or code to be executed

• Initialization: It is the initial condition which is executed once when the loop starts.

• Condition: It is the second condition which is executed each time to test the condition of the
loop. It continues execution until the condition is false.

• Statement: Codes which are executed till the conditions are true

• Increment/Decrement: It increments or decrements the variable value. It is an optional condition.

erspaudel 9
EXAMPLE

public class ForExample {


public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
System.out.println(i);
}
}
}

erspaudel 10
OUTPUT
1
2
3
4
5
6
7
8
9
10

erspaudel 11
erspaudel 12
EXAMPLE

public class ForExample {


public static void main(String[] args) {
for (int i = 10; i >= 0; i--) {
System.out.println(i);
}
}
}

erspaudel 13
OUTPUT
10
9
8
7
6
5
4
3
2
1
0

erspaudel 14
FOR-EACH LOOP

• There is also a "for-each" loop, which is used exclusively to loop through


elements in an array:

Syntax

for (type variableName : arrayName) {

// code block to be executed

erspaudel 15
FOR EACH LOOP EXAMPLE

public class ForEachExample {


public static void main(String[] args) {
String[] cars = {"Volvo", "BMW", "Ford", "Mazda"};
for (String car : cars) {
System.out.println(car);
}
}
}

erspaudel 16
USABLE VS REUSABLE

erspaudel 17
WHILE LOOP

A while loop statement in Java programming language repeatedly executes a target


statement as long as a given condition is true.

Syntax

while(Boolean_expression) {

// Statements

erspaudel 18
WHILE LOOP EXAMPLE

public class WhileTest {


public static void main(String args[]) {
int x = 10;
while (x < 20) {
System.out.println("value of x : " + x);
x++;
}
}
}

erspaudel 19
OUTPUT

value of x : 10
value of x : 11
value of x : 12
value of x : 13
value of x : 14
value of x : 15
value of x : 16
value of x : 17
value of x : 18
value of x : 19

erspaudel 20
erspaudel 21
READ DOCUMENTATION

erspaudel 22
DO WHILE LOOP

• The Java do-while loop is executed at least once because condition is checked after
loop body.

• do while loop in Java is similar to while loop except that the condition is checked
after the statements are executed, so do while loop guarantees the loop execution
at least once.

erspaudel 23
DO WHILE SYNTAX

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

erspaudel 24
DO WHILE EXAMPLE

public class DoWhileExample {


public static void main(String[] args) {
int i = 1;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}

erspaudel 25
OUTPUT

1
2
3
4
5
6
7
8
9
10

erspaudel 26
DO WHILE EXAMPLE

public class DoWhileExample {


public static void main(String[] args) {
int i = 99;
do {
System.out.println(i);
i++;
} while (i <= 10);
}
}

erspaudel 27
OUTPUT

99

erspaudel 28
WHILE VS DO WHILE

erspaudel 29
BREAK IN LOOP

• The break statement is also be used to jump out of a loop.

• When the break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following the
loop.

erspaudel 30
BREAK IN LOOP EXAMPLE

public class BreakExample {


public static void main(String[] args) {
for (int i = 0; i < 10; i++) {
if (i == 4) {
break;
}
System.out.println(i);
}
}
}

erspaudel 31
OUTPUT

1
2

erspaudel 32
BREAK

erspaudel 33
CONTINUE
• Continue statement is mostly used inside loops.

• Whenever it is encountered inside a loop, control directly jumps to the beginning of


the loop for next iteration, skipping the execution of statements inside loop’s body
for the current iteration.

• This is particularly useful when you want to continue the loop but do not want the
rest of the statements(after continue statement) in loop body to execute for that
particular iteration.

erspaudel 34
EXAMPLE

public class ContinueExample {


public static void main(String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 5) {
continue;//it will skip the rest statement
}
System.out.println(i);
}
}
}

erspaudel 35
OUTPUT
1

3
4

9
10

erspaudel 36
CONTINUE

erspaudel 37
HEY, SOMEONE STOP THE LOOP!

erspaudel 38
END OF LECTURE 5

Any questions?

erspaudel 39

You might also like