0% found this document useful (0 votes)
21 views18 pages

05 Looping - & - Jumping Statements

The document provides an overview of repetition and looping statements in programming, specifically focusing on 'while', 'do...while', and 'for' loops. It also covers branching and jump statements such as 'break' and 'continue', along with example code snippets for each type of loop. Additionally, it includes homework assignments related to calculating products and generating tables of squares and cubes.
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)
21 views18 pages

05 Looping - & - Jumping Statements

The document provides an overview of repetition and looping statements in programming, specifically focusing on 'while', 'do...while', and 'for' loops. It also covers branching and jump statements such as 'break' and 'continue', along with example code snippets for each type of loop. Additionally, it includes homework assignments related to calculating products and generating tables of squares and cubes.
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/ 18

D EPARTMENT OF I NFORMATION S YSTEMS

C OMPUTER S CIENCE FACULTY


K ABUL U NIVERSITY

Lecture 05. Repetition/ Looping Statements


Adapted from Java How To Program, 9th Edition, Paul & Harvey Deitel,
Deitel & Associates, inc.

By: Hedayat || Saturday, Nov 16, 2024


O UTLINE

¡ Repetition / Looping Statements


¡ For
¡ While
¡ Do while

¡ Branching / Jump Statements


¡ Continue
¡ Break

BY: HEDAYAT 2
R EPETITION/L OOPING S TATEMENTS
¡ Provides for repeated execution of one or more statements until a terminating
condition is reached
¡ Three basic types:
¡ while
¡ do...while
¡ for

BY: HEDAYAT 3
R EPETITION/L OOPING S TATEMENTS
¡ While Loops
¡ Loop counter
¡ Counts number of times the loop is executed

¡ Two kinds of loops


¡ Pre-test loop
¡ Tests terminating condition at the beginning of the loop

¡ Post-test loop
¡ Tests terminating condition at the end of the loop

BY: HEDAYAT 4
R EPETITION/L OOPING S TATEMENTS
¡ Tests terminating condition at the end of the loop

BY: HEDAYAT 5
R EPETITION/L OOPING S TATEMENTS
¡ While Loop
¡ The while statement continually executes a block of statements while a particular
condition is true.
¡ Syntax: while (expression) {
statement(s)
}

¡ The while statement evaluates expression, which must return a Boolean value. If the
expression evaluates to true, the while statement executes the statement(s) in the
while block. The while statement continues testing the expression and executing its
block until the expression evaluates to false.
BY: HEDAYAT 6
R EPETITION/L OOPING S TATEMENTS
public class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
BY: HEDAYAT 7
R EPETITION/L OOPING S TATEMENTS
¡ do…..While Loop
¡ The difference between do-while and while is that do-while evaluates its expression
at the bottom of the loop instead of the top. Therefore, the statements within the do
block are always executed at least once.
¡ Syntax:

do {
statement(s)
} while (expression);

BY: HEDAYAT 8
R EPETITION/L OOPING S TATEMENTS
public class DoWhileDemo {
public static void main(String[] args){
int count = 1;
do {
System.out.println("Count is: " + count);
count++;
} while (count <= 11);
}
}
BY: HEDAYAT 9
R EPETITION/L OOPING S TATEMENTS
¡ For Loop
¡ The for statement provides a compact way to iterate over a range of values.
¡ Syntax: for (initialization; termination; increment) {
statement(s)
}

¡ The initialization expression initializes the loop; it's executed once, as the loop
begins.
¡ When the termination expression evaluates to false, the loop terminates.
¡ The increment expression is invoked after each iteration through the loop; it is
perfectly acceptable for this expression to increment or decrement a value.
BY: HEDAYAT 10
R EPETITION/L OOPING S TATEMENTS

BY: HEDAYAT 11
R EPETITION/L OOPING S TATEMENTS
public class ForDemo {
public static void main(String[] args){
for(int i=1; i<11; i++){
System.out.println("Count is: " + i);
}
}
}

BY: HEDAYAT 12
R EPETITION/L OOPING S TATEMENTS
¡ Branching/Jump Statements
¡ Break:
¡ The break statement, when executed in a while, for, do...while or switch, causes immediate
exit from that statement. Execution continues with the first statement after the control
statement. Common uses of the break statement are to escape early from a loop or to skip
the remainder of a switch.
¡ Continue:
¡ The continue statement, when executed in a while, for or do...while, skips the remaining
statements in the loop body and proceeds with the next iteration of the loop.

BY: HEDAYAT 13
R EPETITION/L OOPING S TATEMENTS
public class BreakTest {
public static void main(String[] args){
int count;
for ( count = 1; count <= 10; count++ ) {
if ( count == 5 )
break; // terminate loop
System.out.println( count+" " );
}
System.out.println( "\nBroke out of loop at count = \n"+ count );
}
}
BY: HEDAYAT 14
R EPETITION/L OOPING S TATEMENTS
public class ContinueTest{
public static void main( String args[] ){
for( int count = 1; count <= 10; count++ ) {
if ( count == 5 )
continue;
System.out.println(count+" " );
}
System.out.println( "\nUsed continue to skip printing 5" );
}
}
BY: HEDAYAT 15
TO S UM UP

¡ Now, You know:


¡ Repetition / Looping Statements
¡ For
¡ While
¡ Do while

¡ Branching / Jump Statements


¡ Continue
¡ Break

BY: HEDAYAT 16
H OMEWORK
¡ Write an application that calculates the product of the odd integers from 1 to 15.
¡ Write an application that calculates the squares and cubes of the numbers from 0
to 10 and prints the resulting values in table format, as shown below:
Number square cube
0 0 0
1 1 1
2 4 8
3 9 27
4 16 64
5 25 125
6 36 216
7 49 343
8 64 512
9
BY: HEDAYAT
81 729 17
10 100 1000
T HE E ND

BY: HEDAYAT 18

You might also like