Chapter (4)
Iteration Statements
Iteration statements are for, while, and do-while loops. A loop repeatedly executes the same set
of instructions until a termination condition is met and reduce the duplicate code.
For Loop
The for loop loops through a block of code a number of times.
for (statement 1; statement 2; statement 3) {
// code block to be executed
}
Statement 1(initialization) is executed (one time) before the execution of the code block.
Statement 2(condition) defines the condition for executing the code block.
Statement 3 (increment/decrasement) is executed (every time) after the code block has been
executed.
Example-1
class ForLoop{
public static void main (String[] args) {
for(int i=0; i<=10; i++){
System.out.println ("Hello");
}
}
}
While Loop
The while loop loops through a block of code as long as a specified condition is true:
//initialization
while (condition) {
//statement
//increment/decrement
}
Example-2
class Whileloop{
public static void main (String[] args) {
int i = 1;
while (i < =5) {
System.out.println(“*\t”);
i++;
}
}
}
Note: Do not forget to increase the variable used in the condition, otherwise the loop will
never end!
Do..While Loop
The do/while loop is a variant of the while loop. This loop will execute the code block once, before
checking if the condition is true, then it will repeat the loop as long as the condition is true.
//initialization
do {
// statement
//increment/decrement
}while (condition);
Basic Software Engineering Ch4-1
Example-3
class DoWhileloop{
public static void main (String[] args) {
int i = 1;
do{
System.out.println(“2*”+i+”=”+(2*i));
i++;
} while (i < =12);
}
}
Break Statement
The break statement can also be used to jump out of a loop.
Example-4
class LoopwithBreak{
public static void main (String[] args) {
for (int i = 1; i <= 10; i++) {
if (i == 6) {
break;
}
System.out.println(i);
}
}
}
Continue Statement
Continue statement causes control to be transferred directly to the conditional expression that
controls the loop.
Example-5
class LoopwithContinue{
public static void main (String[] args) {
for (int i = 1; i <=10; i++) {
if (i == 4 || i == 8) {
continue;
}
System.out.println(i);
}
}
}
Example-6
// Determine primes number or not.
class FindPrime {
public static void main(String args[]) {
int num=14;
for(int i=2; i <= num i++) {
if((num % i) == 0) {
break;
}
}
if(num==i) System.out.println("Prime");
Basic Software Engineering Ch 4-2
else System.out.println("Not Prime");
}
}
Example-7
class ch4ex1{
public static void main (String[] args) {
int i=20;
if(i%2==0){ System.out.println (i+" is Even number "); }
else{ System.out.println (i+" is Odd number "); }
}
}
Exercise 1
class ch4ex2{
public static void main (String[] args) {
for(int i=0; i<=10; i++){
System.out.println ("2 * "+ +" = "+ );
}
}
}
Exercise 2.
class ch4ex3{
public static void main (String[] args) {
for(int i=9; i>=1; i-=1){
if( ){ System.out.print(i); }
else{ System.out.print ( );
}
}
}
Exercise 3. Write a program to count the odd numbers between 1 to 200.
Exercise 4. Write a Java Program to find the factorial number of following number using for loop,
while loop and do…. while loop. (number=5)
(Hints: Factorial of 5 is 5*4*3*2*1 =120)
Basic Software Engineering Ch 4-3