0% found this document useful (0 votes)
20 views24 pages

Chapter 4 Iteration

Uploaded by

Jiji Lionel
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)
20 views24 pages

Chapter 4 Iteration

Uploaded by

Jiji Lionel
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/ 24

CSC 2301: PRINCIPAL OF

COMPUTER PROGRAMMING
Iteration
ITERATION STATEMENTS

• Loops allow a block of statements to be executed repeatedly


• A Boolean condition (loop condition) is commonly used to
determine when to terminate the loop. There are three type
of loops.
 The while statement
 The do-while statement
 The basic for statement
The while statement
while(loop-condition){
loop_body;
}
//outside while loop

The loop condition is evaluated


before executing the loop body.
The while statement executes the
loop body as long as the loop
condition true.
The do-while statement
do{
loop-body;
}while(loop-condition)
//outside do-while loop

In the do-while statement,


the loop condition is evaluated
after executing the loop body.
The loop body is executed at least
once.
•The for(;;) statement
for( initialization; loop_condition; update_expression){
loop_body
}
// outside for loop
The initialization usually declares
and initializes a loop variable that
Controls execution of the loop-body
The loop condition must evaluate
to boolean or Boolean.
If the loop condition is true, the loop body is executed, otherwise
execution continues with the statement following the for(;;).
After each iteration(that is, execution of the loop body), the update
expression is executed.
Ex: public class DisplayStars{
public static void main(String[] args){
for(int i=0; i < 3; i++){
System.out.println(“*****”);
}
}
}
Output: *****
*****
*****
Any variable declared in the for(;;) block is not accessible after
the for loop terminates.
for( ; ;){
doProgramming; // infinite loop
}

The for(:) statement

The enhanced for loop is convenient when we need to iterate


over array or a collection
Transfer Statements
There are six language constructs for transferring control in a
program.

 break
 continue
 return
 Try-catch-finally
 Throw
 Assert
Labeled Statements
A statement may have a label
label : statement
Example:
L1: if(i> 0){
L1: System.out.println(i); // not ok Label L1 redeclared
}
L1:while(i<0){
L2:System.out.println(i); // OK
}
L1:{
int j =10;
System.out.println(j);
}
 Label names exists in their own namespaces so that they do not
conflict with name of packages, classes, interfaces, methods, fields
and local variables.
 The scope of a label is the statement predefined by the label.

The break statement

The break statement has two forms


 break; // the unlabeled form
 break label; // the labeled form
 The unlabeled break terminates loops for(;;), for(:), while, do-while
and switch statement and transfer control out of the current context.
The rest of the statement body is skipped and execution continue
after the enclosing statement.

for(int i = 0; i < 10; i++){


System.out.println(“Guess a number”)’
int numb = keyboard.nextInt();
if(numb == 27){
System.out.println(“Bravo You guess Right”);
break; // exit loop even if it has not yet finished ten iteration
}
}
The continue statement
The continue statement has 2 forms
 continues; //the unlabeled form
 Continues label; // the labeled form

 The continue statement can be used only in for(;;), for(:), while, do-
while loops to prematurely stop the current iteration of the loop and
proceed with the next iteration, if possible.

 In the case of while and do-while loops, the rest of the loop is
skipped with execution continuing with the loop condition
 In the case of the for(;;) loop, the rest of the loop body is skipped
with execution continuing with the update expression.
Consider the following program:

public class IterationQ1{


public static void main(String[] args){ a)4
for(int i = 1; i <= 4; i++) {
b)YES
System.out.println("YES");
} YES
System.out.println("OK"); YES
} YES
} OK
(a) How many times does this for loop repeat?
(b) What would be the output of this program?
Consider the following program:
public class IterationQ2{
public static void main(String[] args){
for(int i = 1; i < 4; i++){ a)3
System.out.println("YES"); b)YES
System.out.println("NO"); NO
} YES
System.out.println("OK"); NO
} YES
} NO
(a) How many times does this for loop repeat? OK
(b) What would be the output of this program?
Consider the following program
public class IterationQ4{ a)8
public static void main(String[] args){ b)1
for(int i=1; i<=15; i= i +2){ 3
System.out.println(i); 5
} 7
}
}
9
(a) How many times does this for loop repeat? 11
(b) What would be the output of this program? 13
(c) What would be the consequence of 15
changing the test of the loop to (i >= 15)? c) No iteration
public class IterationQ5{
public static void main(String[] args){
for(int i=5; i>=2; i--){
switch (i){
case 1: case 3: System.out.println("YES"); break;
case 2: case 4: case 5: System.out.println("NO");
}
}
System.out.println("OK");
}
}
(a) How many times does this for loop repeat?
(b) What would be the output of this program?
(c) What would be the consequence of changing the loop counter to (i++)
instead of (i−−)
a) 4
b) NO
NO
YES
NO
OK
c) Infinite loop
What would be the output of the following program?
public class IterationQ8{
public static void main(String[] args){
for(int i=1; i<=10; i++){
if (i > 5){
break; 1
} 2
System.out.println(i); 3
} 4
} 5
}
What would be the output of the following program?
public class IterationQ9{
public static void main(String[] args){
for(int i=1; i<=10; i++){
if (i <= 5){
continue; 6
} 7
System.out.println(i); 8
} 9
} 10
}
 Write a program to print numbers from 1 to 10.
 Write a program to calculate the sum of first 10 natural
number.
 Write a program that accepts three numbers from the
user and prints "increasing" if the numbers are in
increasing order, "decreasing" if the numbers are in
decreasing order, and "Neither increasing or decreasing
order" otherwise. Go to the editor
Test Data
Input first number: 1524
Input second number: 2345
Input third number: 3321
Expected Output :
THE END

You might also like