






















Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
Community
Ask the community for help and clear up your study doubts
Discover the best universities in your country according to Docsity users
Free resources
Download our free guides on studying techniques, anxiety management strategies, and thesis advice from Docsity tutors
Iteration statements in Java, also known as loops, which include the while loop, do-while loop, for loop, and nested loops. It covers the basic elements of iterations, loop structures, loop continuation condition, initialization, and jump statements such as break and continue. Examples are provided for each loop type and jump statement.
Typology: Study notes
1 / 30
This page cannot be seen from the preview
Don't miss anything!
a = b 5 ; if( a>=c 5 ) while( a>= 5 ) b = a / 10 ; a = a + 4 ; { c = a + b); else b = 9 + a; a = a – 7 ; a --; } // while
Iteration (repetition) statements causes Java to execute one or more statements as long as a condition exist. Each repetition statement request :
while (loop-continuation-condition) { // loop-body; Statement(s); } int count = 0 ; while (count < 100 ) { System.out.println("Welcome to Java!"); count++; } // while Loop Continuation Condition? true Statement(s) (loop body) false (count < 100)? true System.out.println("Welcome to Java!"); count++; false (A) (B) count = 0;
Every iteration res is incremented by 1 Declaration during input
Example 1 : Example 2 : int count= 1 ; double num=2.0; while (count != 50 ) while ( num !=0.0) count+ = 2 ; num = num - 0.1; This loop will never terminate because count will never equal 50. This loop will never terminate because num will never have a value exactly equal to 0.
initialization conditional iteration For loop body false true Three kinds of for loop header expressions ● Before the loop begins the first part of the header, called initialization , is executed. ● The second part of the header is the boolean condition , which is evaluated before the loop body. If true ,the body is executed. ● The iteration part is executed after each iteration of the loop. Initialization is executed only once
int fact= 1 ; // factorial System.out_._ println("enter an integer : "); int n=reader.nextInt(); // input variable for (int i= 1 ; i<=n ;i++) fact *= i; System.out.println( “ factorial is :" + fact); n i i<=n fact output 3 1 3 1 T 1 3 2 T 2 3 3 T 6 3 4 F 6 Before loop execution Trace table ( טבלת מעקב ) A trace table is a technique used to test programs.
int prime = 1 ; / / help variable System.out_._ println( “ enter an integer : “ ); int x = reader.nextInt(); / / input variable if (x> 3 ) { for( int i = 2 ; i <x && prime == 1 ; i++ ) if( x%i == 0 ) prime = 0 ; switch (prime) { case 1 : System.out.println( x +" is prime number “ ); break; case 0 : System.out.println (x +" is not prime number “ ); break; } // switch } / / if (x> 3 ) else System.out.println (x +" is prime number "); This program section checks if the input integer is the prime number
do { statement(s) } while (expression) ;
Note that the loop begins with the reserved word do Note that the loop ends with semicolon
Waiting for legal input
This program section reads an integer and reverses its digit mathematically.