0% found this document useful (0 votes)
51 views8 pages

ICT-Presentation ForLoops

This document discusses for loops in Java. It explains that for loops check conditions and repeatedly execute code as long as those conditions are met. It provides the basic structure of a for loop with initialization, condition, and operation sections separated by semicolons. Examples are given to count from 15 to 5 and to get a user input number and count up/down to 9 using for loops. Modifications to the code like using break and if statements or changing the window are permitted.
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)
51 views8 pages

ICT-Presentation ForLoops

This document discusses for loops in Java. It explains that for loops check conditions and repeatedly execute code as long as those conditions are met. It provides the basic structure of a for loop with initialization, condition, and operation sections separated by semicolons. Examples are given to count from 15 to 5 and to get a user input number and count up/down to 9 using for loops. Modifications to the code like using break and if statements or changing the window are permitted.
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/ 8

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

You might also like