Thanks to visit codestin.com
Credit goes to www.scribd.com

0% found this document useful (0 votes)
20 views23 pages

Section 03 Loops

Uploaded by

krish89.ece
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)
20 views23 pages

Section 03 Loops

Uploaded by

krish89.ece
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/ 23

for loops

© luv2code LLC
for loops
• for loops are useful for repeating a section of code multiple times

• For example, if we want to print a statement multiple times

• More ef cient to use a for loop instead of multiple println


fi
www.luv2code.com © luv2code LLC
Basic Example
Repeat this block of code
100 times

for (int i = 0; i < 100; i++)

System.out.println("Hello World")

Hello Worl

Hello Worl
Print this message

Hello Worl
100 times

Hello Worl

Hello Worl

www.luv2code.com © luv2code LLC


General Syntax

Continue loop
Initialize the loop Modify loop
while this
variable variable
condition is true

for (initialization; condition; update)

// block of code
}

code to execute

www.luv2code.com © luv2code LLC


Applying Syntax to our example

Continue loop
Initialize the loop Modify loop
while this
variable variable
condition is true

for (int i = 0; i < 100; i++)

System.out.println("Hello World")

code to execute

www.luv2code.com © luv2code LLC


Display Loop Variable
Loop 1 to 5

for (int i = 1; i <= 5; i++)

System.out.println("Counter: " + i)

} Counter:

Counter:

Counter:

Counter:

Counter: 5

www.luv2code.com © luv2code LLC


Change Loop Increment
Loop 0 to 20
In increments of 5

for (int i = 0; i <= 20; i=i+5)

System.out.println("Counter: " + i)

} Counter:

Counter:

Counter: 1

Counter: 1

Counter: 20

www.luv2code.com © luv2code LLC


Count Down
Loop 5 down to 0

for (int i = 5; i >= 0; i--)

System.out.println("Counter: " + i)

} Counter:

Counter:

Counter:

Counter:

Counter:

Counter: 0

www.luv2code.com © luv2code LLC


Nested Loops
• This example will print the multiplication table: 5x5

for (int row = 1; row <= 5; row++)

for (int col = 1; col <= 5; col++)

int value = row * col

System.out.print(value + "\t")

System.out.println() ;

\t: tab character


} for alignment

www.luv2code.com © luv2code LLC


while loops

© luv2code LLC
while loops
• while loops are useful for repeating a section of code multiple times

• Useful, when the number of iterations are unknown beforehand

www.luv2code.com © luv2code LLC


General Syntax
Continue loop
while this
condition is true

while (condition)

// block of code
}

code to execute

www.luv2code.com © luv2code LLC


Example: Prompt the User
Loop while the user wants
Scanner scanner = new Scanner(System.in)
to continue

boolean done = false

while (!done) {

System.out.println("Hello world") Hello worl

Are we done? (Y/N):

System.out.print("Are we done? (Y/N): ")

String userInput = scanner.nextLine() Hello worl

Are we done? (Y/N):

if (userInput.equalsIgnoreCase("Y"))

done = true Hello worl

Are we done? (Y/N):

System.out.println() Process finished with exit code 0


;

scanner.close();

www.luv2code.com © luv2code LLC


Which One - for loop or while loop???
• for loops are useful when the number of iterations are know

• while loops are useful, when the number of iterations are unknown
beforehan
d

• while loops are useful, if the condition could change based on user input
or other conditional logic (ie account balance, game level, user Y/N, …)

Keep shopping until


Keep playing until you Prompt: Y/N
your balance falls
reach level zzz
below xxx

www.luv2code.com © luv2code LLC


do-while loops
• A variant of the while loo

• The conditional is placed at the end of the loo

• Useful, when you want to execute the block of code at least once

www.luv2code.com © luv2code LLC


General Syntax code to execute
at least once

do

// block of code
} while (condition);

Continue loop
while this
condition is true

www.luv2code.com © luv2code LLC


Example: do-while
Loop while the user wants
Scanner scanner = new Scanner(System.in) to continue

boolean done = false

do
{

System.out.println("Hello world") Hello worl

Are we done? (Y/N):

System.out.print("Are we done? (Y/N): ")

String userInput = scanner.nextLine() Hello worl

Are we done? (Y/N):

if (userInput.equalsIgnoreCase("Y"))

done = true Hello worl


;

Are we done? (Y/N):


}

System.out.println() Process finished with exit code 0


;

} while (!done)
;

scanner.close();

www.luv2code.com © luv2code LLC


break and continue

© luv2code LLC
break statement
• In loops, the break statement will exit out of the current loop

www.luv2code.com © luv2code LLC


Example
for (int i = 1; i <= 10; i++)

if (i == 5) Break at 5
exit the loop
{

break
;

System.out.println("Counter: " + i);


}
Counter:

Counter:

Counter:

Counter:

Process finished with exit code 0

www.luv2code.com © luv2code LLC


continue statement
• In loops, the continue statement will

• Skip the remaing code in the block and continue with next iteration

• Think of it as “skipping over” current iteration

www.luv2code.com © luv2code LLC


Example
for (int i = 1; i <= 10; i++)
At 5, continue

if (i == 5) skip remaining statements in


{

continue current iteration


;

System.out.println("Counter: " + i);


} Counter:

Counter:

Counter:

Counter:

Notice, we “skipped over” 5 Counter:

Counter:

Counter:

Counter:

Counter: 1

Process finished with exit code 0

www.luv2code.com © luv2code LLC


Which One: break or continue???
• Use break when you want to terminate current loo

• Use continue when you want to skip remaining code in current iteration

Can also use break and continue


with while and do-while

www.luv2code.com © luv2code LLC

You might also like