0% found this document useful (0 votes)
6 views16 pages

COM01 Week11

The document provides an overview of iterative statements in Java, specifically focusing on FOR, WHILE, and DO-WHILE loops. It includes syntax, examples of each loop type, and highlights the differences between them, such as the execution order of the DO-WHILE loop. Additionally, it presents sample problems for practice with these looping constructs.

Uploaded by

goenitzcometome
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)
6 views16 pages

COM01 Week11

The document provides an overview of iterative statements in Java, specifically focusing on FOR, WHILE, and DO-WHILE loops. It includes syntax, examples of each loop type, and highlights the differences between them, such as the execution order of the DO-WHILE loop. Additionally, it presents sample problems for practice with these looping constructs.

Uploaded by

goenitzcometome
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/ 16

COM01

JAVA ITERATIVE STATEMENT


Iterative Statement
Iterative Statement or loops allow a set of instructions to
be performed until a certain condition is reached. There
are three Iterative statements: FOR, WHILE and DO-
WHILE statement
FOR
The for statement allows many variants, but there are three main
parts:
• The initialization is usually an assignment statement that is used to set
the loop control variable
• The conditional is a relational expression that determines when the
loop will exit.
• The increment defines how the loop control variable will change each
time the loop is repeated
These three major sections must be separated by semicolons
The for loop continues to execute as long as the condition is true.
Once the condition becomes false
FOR
For(initialization; condition; increment/decrement)
{
Statement 1;
Statement n;
}
FOR
Example 1:
public class name {
public static void main (String [] arg){
for ( int x = 1; x <= 100; x++)
{
System.out.println(x);
}
}
}
FOR
Example 2 (Loop that is not executed)
public class name {
public static void main (String [] arg){
int x = 10;
for ( int y = 10; y != x; x++)
{
System.out.println(y);
}
}
}
FOR
Example 3 (Infinite Loop)
public class name {
public static void main (String [] arg){
for ( ; ;)
{
System.out.println(“This loop will run forever”);
}
}
}
FOR
Example 4 (Loop with no body)
public class name {
public static void main (String [] arg){
for ( int x = 1; x <= 100; x++)
{
}
}
}
WHILE
While statement is an empty statement, a single statement, or a
block of statements that is to be repeated. The condition may be any
expression, with true being any nonzero value. The loop iterates while
the condition is TRUE. When the condition is FALSE, program control
passes to the line after the loop code
WHILE
while(condition)
{
Statement 1;
Statement n;
}
WHILE
Example 1
public class name {
public static void main (String [] arg){
int num = 0;
while (num < 5 )
{
System.out.println(num);
num++
}
}
}
DO-WHILE
The difference between the two types of loops involves when the
controlling logical expression is checked.

With a do-while statement, the body of the loop is executed first and
the logical expression is checked after the loop body is executed.

The do-while statement always executes the loop body at least once
WHILE
do
{
Statement 1;
} while (condition);
WHILE
Example 1
public class name {
public static void main (String [] arg){
int num = 0;
do
{
System.out.println(num);
num++
} while (num < 0 )
}
}
NESTED LOOP
Example 1
public class name {
public static void main (String [] arg){
for (int ctr = 1; ctr < 3; ctr++)
{
int count = 1;
while(count < 2)
{
System.out.println(“Hi”);
count++;
}
}
System.out.println(“Hello”);
}
}
SAMPLE PROBLEM
1. Write a program to print numbers from 1 to 10.
2. Write a program to calculate the sum of first 10 natural number.
3. Write a program that print the first 10 even integer.
4. Write a program that print the first 20 odd integer.
5. Write a program that prompts the user to input a positive integer. It
should then print the multiplication table of that number.
Ex: 1 x 1 = 1
1x2=2
1x3=3…
(for and while)

You might also like