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

8.1 Loop (While Loop)

The document discusses loops in programming and the while loop in Java. It explains the components of a loop using flowcharts and compares example flowcharts to their Java code implementations using a while loop. Key aspects covered include the initialization, condition, body, and steps of a while loop in Java.
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)
36 views

8.1 Loop (While Loop)

The document discusses loops in programming and the while loop in Java. It explains the components of a loop using flowcharts and compares example flowcharts to their Java code implementations using a while loop. Key aspects covered include the initialization, condition, body, and steps of a while loop in Java.
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/ 11

Loops (while)

Intro to loop
Loop is one of fundamental concepts of programming.
It is a control structure that allows one to repeatedly execute a block of codes.
Components of a loop:
1) Initialization: one or more variable(s) to keep track of the loop.

2) Condition: boolean expression based on which the loop runs.

3) Body: block of codes which is executed each time the loop runs.

4) Steps: updating the initialized variable(s) to make sure the loop runs certain

amount of times only.


START
Scenario to Flowchart
Print “CSE
Initialization i=1
is fun”
Before diving into programming, let’s try to visualize
how loop actually works using Flowcharts. Print “CSE
Condition is is
fun” FALSE

}
i <= 500
Scenario 1: Draw a flowchart that print “CSE is fun” ?
500 times. TRUE 496 times
Body
Print “CSE
Two Possible solutions: is fun”
Print “CSE
is fun”
1. Draw the Print block 500 times.
i += 1
Steps Print “CSE
is fun”
2. Using Loops!!
END
END
START
Scenario to Flowchart (Contd) Initialization i=1
Let’s try to solve another scenario using flowchart
Condition FALSE
is
Scenario 2: Draw a flowchart that prints all i <= 120 ?

numbers between 1 to 120 (including) that are TRUE


divisible by 7. is FALSE
i%7==0
Body ?
As you can see from this example, the looping TRUE
body can contain branching conditions as well. Print i
In complex operations, we can use loops within
loops when needed.
i += 1
Steps

END
Loops in Java
Now that you have a brief visualization of how loops work. Let’s head into coding in
Java. There are three types of loops in Java.

1. while loop.
2. for loop.
3. do while loop.

❖ for and while loop only enters the loop’s body if the condition is true.

❖ On the other hand, do while loop executes the body’s code first then checks the
condition.
while loop

Structure of while loop: Key points of the structure:


➢ We initialize the variable outside the loop’s
1. INITIALIZATION
2. while ( CONDITION ){ body.

3. BODY OF THE LOOP ➢ After the while keyword we write the


4. STEPS condition within parenthesis.
5. } ➢ Then at the end of the loop’s body we
increase/decrease the initialized variable.
Let’s try to compare the Scenario 1 flowchart with the Java
while loop (Contd) code of the same problem.

START
1. public class WhileLoopScenario1{
2. public static void main(String[] args) { Initialization
i=0
3. int i = 0;
4. while ( i < 500 ) {
5. System.out.println(“CSE IS FUN”); Condition is FALSE
6. i+=1; i < 500
?
7. }
TRUE
8. } Body
Print “CSE
9. } is fun”
Welcome to DrJava. Working directory is /home/Java110
> run WhileLoopScenario1
Steps i += 1
CSE IS FUN
CSE IS FUN
……. END
How about printing numbers? Let’s try to print 1 to 15 using
while loop (Contd) while loop.

START
1. public class WhileLoopNumbers{
2. public static void main(String[] args) { Initialization
i=1
3. int i = 1;
4. while ( i <= 15 ) {
5. System.out.print(i + “ ”); Condition
is FALSE
6. i+=1; i <= 15?
7. }
TRUE
8. } Body
9. } Print i

Welcome to DrJava. Working directory is /home/Java110


> run WhileLoopNumbers
Steps i += 1
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 >

END
Let’s try to compare the Scenario 2 flowchart with the Java
while loop (Contd) code of the same problem. START

Initialization i=1
1. public class WhileLoopScenario2{
2. public static void main(String[] args) {
3. int i = 1; Condition FALSE
is
4. while ( i <= 120 ) { i <= 120 ?
5. If ( i%7==0 ) {
6. System.out.print(i+“ ”); TRUE
7. } is FALSE
8. i+=1; i%7==0
9. } ?
10. } Body
TRUE
11. }
Print i

Welcome to DrJava. Working directory is /home/Java110


Steps
> run WhileLoopScenario2
i += 1
7 14 21 28 35 42 49 56 63 70 77 84 91 98 105 112 119 >

END
Practice
● Print the even numbers up to n
● Print the sum of first n integers
● Print the sum of all odd integers from 0 to n
● Write a program to keep asking for a number until you enter a negative
number. At the end, print the sum of all entered numbers
THANK YOU!

You might also like