FOR LOOPS
P R E S E N T A T I O N B Y : C H A R L E S T U A Z O N
INTRODUCTION
For loop is one of the three
java conditional iteration
statements. They are used to
check for certain conditions
and then repeatedly execute a
block of code as long as
those conditions are met.
How to write for loops
for(initialization;condition;operation){
block of code to run;
}
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.
Operation: Increments or decrements the intialized
variable value.
How to write for loops
for(initialization;condition;operation){}
NOTE: For loop components
have semicolons in-between
its elements as seperators.
Some people get confused on
using commas instead
semicolons.
Be aware.
FOR loops vs WHILE loops
PRACTICE:
1. Create a program that uses FOR LOOP that counts from 15 and back
to 5.
Incase that you can't read,
Sample output:
you cannot use while and
15
do/while loops
14
13 You may use the break;
12 and if-statements
11
10
You may modify the
9
program's running
8 window aesthethics.
7
6
5
CHALLENGE:
2. Create a program that uses FOR LOOP that gets an integer from
user input then from that integer, it should count up/down to 9.
Sample Input: Sample Input: Sample Input:
5
Sample Output:
13
Sample Output:
9
Sample Output:
5 13 9
6 12
7 11
8 10
9 9
You may use the break; Incase that you can't You may modify the
and if-statements read, you cannot use program's running
while and do/while
loops window aesthethics.
THANK YOU
P R E S E N T A T I O N B Y : C H A R L E S T U A Z O N