0% found this document useful (0 votes)
75 views25 pages

For - Part1 PDF

The document discusses why looping is needed in programming and provides examples of for loops in Java. It explains that without loops, programmers would spend more time writing individual lines of code than computers would spend executing them. Loops allow code to be repeated efficiently. The document then demonstrates how a for loop in Java can print doubling calculations from 1 to 5 in fewer lines of code than writing each line individually. It outlines the structure of a for loop, including the initialization, test, and update sections that control loop execution.

Uploaded by

Ankit Anand
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)
75 views25 pages

For - Part1 PDF

The document discusses why looping is needed in programming and provides examples of for loops in Java. It explains that without loops, programmers would spend more time writing individual lines of code than computers would spend executing them. Loops allow code to be repeated efficiently. The document then demonstrates how a for loop in Java can print doubling calculations from 1 to 5 in fewer lines of code than writing each line individually. It outlines the structure of a for loop, including the initialization, test, and update sections that control loop execution.

Uploaded by

Ankit Anand
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/ 25

Why do we need

looping?
Why do we need
looping?
Put in video of
an Android timer
counting from
10 down to 1.
Unleash power of computer
... Time for human
out.println(10); to write one line
out.println(9); of code:
out.println(8);
out.println(7); ~2 seconds
out.println(6);
out.println(5); Time for computer
out.println(4); to execute one
out.println(3);
out.println(2); line of code:
out.println(1); ~0.001 seconds
...
Unleash power of computer
... Without loops…
out.println(10);
out.println(9);
out.println(8);
out.println(7); Humanity would
out.println(6);
out.println(5); spend more time
out.println(4);
out.println(3); writing code than the
out.println(2);
out.println(1); computer would
...
executing it!
Introduction to Java For Loops
Loops make repetition easier

out.println(1 + " doubled = " + 2 * 1);


out.println(2 + " doubled = " + 2 * 2);
out.println(3 + " doubled = " + 2 * 3);
out.println(4 + " doubled = " + 2 * 4);
out.println(5 + " doubled = " + 2 * 5);
Loops make repetition easier

out.println(1 + " doubled = " + 2 * 1);


out.println(2 + " doubled = " + 2 * 2);
out.println(3 + " doubled = " + 2 * 3);
out.println(4 + " doubled = " + 2 * 4);
out.println(5 + " doubled = " + 2 * 5);

Intuition:
"I want to print a line for each number from 1 to 5"
Loops make repetition easier
Java’s for loop: causes lines of code to be repeated
out.println(1 + " doubled = " + 2 * 1);
out.println(2 + " doubled = " + 2 * 2);
out.println(3 + " doubled = " + 2 * 3);
out.println(4 + " doubled = " + 2 * 4);
out.println(5 + " doubled = " + 2 * 5);
Does same as
for (int i = 1; i <= 5; i = i+1) {
out.println(i + " doubled = " + 2 * i);
}
Loops make repetition easier
Java’s for loop: causes lines of code to be repeated
out.println(1 + " doubled = " + 2 * 1);
out.println(2 + " doubled = " + 2 * 2);
out.println(3 + " doubled = " + 2 * 3);
out.println(4 + " doubled = " + 2 * 4);
out.println(5 + " doubled = " + 2 * 5);
Does same as
for (int i = 1; i <= 5; i = i+1) {
out.println(i + " doubled = " + 2 * i);
}
Read as: “for each integer i from 1 to 5, do…”
Execution of the Java
For Loop
Structure of the for loop

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


out.println(i + " doubled = " + 2 * i);
}
Let’s consider this code that
prints 5 lines more closely
Structure of the for loop

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


out.println(i + " doubled = " + 2 * i); body
}
Body:
Code to execute repeatedly
Structure of the for loop
Header:
Tells how many time to repeat
for (int i = 1; i <= 5; i = i+1) { header
out.println(i + " doubled = " + 2 * i); body
}
Body:
Code to execute repeatedly
Execution of the for loop

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


out.println(i + " doubled = " + 2 * i);
}

The header controls the execution


• Contains 3 parts
Execution of the for loop

Initialization – performed once

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


out.println(i + " doubled = " + 2 * i);
}
Execution of the for loop

Initialization – performed once

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


out.println(i + " doubled = " + 2 * i);
}

Tells Java what variable to use in the loop and


gives it a starting value
Execution of the for loop

Test – will loop if test is true

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


out.println(i + " doubled = " + 2 * i);
}
Execution of the for loop

Test – will loop if test is true

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


out.println(i + " doubled = " + 2 * i);
}

Performed before each execution of body


Execution of the for loop

Test – will loop if test is true

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


out.println(i + " doubled = " + 2 * i);
} Typically uses comparison operators:
< less than
<= less than or equal to
> greater than
>= greater than or equal to
Execution of the for loop

Update – change the loop variable

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


out.println(i + " doubled = " + 2 * i);
}
Execution of the for loop

Update – change the loop variable

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


out.println(i + " doubled = " + 2 * i);
}

Performed after each execution of body


Execution of the for loop

Update – change the loop variable

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


out.println(i + " doubled = " + 2 * i);
Executed as if it appeared here
}
Execution of the for loop

Update – change the loop variable

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


out.println(i + " doubled = " + 2 * i);
}
Can be any expression that changes the loop
variable
Usually simply use: i++
Execution of the for loop

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


out.println(i + " doubled = " + 2 * i);
}
Execution continues here when
the loop test is false
Flow chart depicting a for loop

You might also like