Docsity
Docsity

Prepare for your exams
Prepare for your exams

Study with the several resources on Docsity


Earn points to download
Earn points to download

Earn points by helping other students or get them with a premium plan


Guidelines and tips
Guidelines and tips

Java Iteration Statements and Loops - Prof. Singh, Study notes of Computer Science

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

2020/2021

Uploaded on 05/26/2021

abhishek-singh-87
abhishek-singh-87 🇮🇳

1 document

1 / 30

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
Java iteration statements
Iteration statements are statements which appear in the
source code only once, but it execute many times.
Such kind of statements are called loops.
Almost all the programming languages support looping
instructions.
1
a = b5; if( a>=c5) while( a>=5 )
b = a / 10; a = a + 4; {
c = a + b); else b = 9 + a;
a = a 7; a --;
} // while
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14
pf15
pf16
pf17
pf18
pf19
pf1a
pf1b
pf1c
pf1d
pf1e

Partial preview of the text

Download Java Iteration Statements and Loops - Prof. Singh and more Study notes Computer Science in PDF only on Docsity!

Java iteration statements

● Iteration statements are statements which appear in the

source code only once , but it execute many times.

● Such kind of statements are called loops.

● Almost all the programming languages support looping

instructions.

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 statements - Loops

Java has three kinds of iteration statements ( לולאות/ הוראות חזרה ) :

WHILE loop

FOR loop

DO... WHILE loop

Iteration (repetition) statements causes Java to execute one or more statements as long as a condition exist. Each repetition statement request :

  1. a control variable /loop counter ( משתנה הלולאה/ מונה הלולאה )
  2. the initial value of control variable
  3. the increment ( decrement ) by which the control variable is modified each time through the loop
  4. the loop continuation condition that determines if looping should continue.

Java Loop structures

Do-while structure While structure

While loop statement

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;

While loop example 1

  • Input :
    • Two integers – num 1 and num 2
  • Output :
    • How many times num 1 contains num 2 This is the result of the integer division num 1 /num 2
  • Note :
    • Do not use the division operator ( / )!

Solution

public class Ex 1 While

static Scanner reader = new Scanner(System.in);

public static void main(String[ ] args)

int res= 0 ; // help variable

System.out.println(“enter two integers : “);

int num 1 =reader.nextInt();

int num 2 =reader.nextInt();

while ( (res+ 1 ) * num 2 <= num 1 )

res ++;

System.out.println(“num 1 contains num 2 : “+res+” times”);

} // main

} // class

Every iteration res is incremented by 1 Declaration during input

Infinite loops

● It is the programmer’s responsibility to ensure that the

condition of a loop will eventually become false.

If it doesn’t, the loop body will execute forever.

● This situation, called an infinite loop ( לולאה אין סופית ).

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.

FOR loop statement

  • Equivalent to while… Any for loop can be converted to while loop and vice versa.
  • If we want to perform something for a predefined number of times, better use for.
  • If we just wait for something to happen (not after a certain number or iterations), better use while.
  • The for loop has three expressions that are contained within parentheses and separated with a semicolon.

FOR loop – order of execution

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

FOR loops example 1

This program section reads an integer and computes its

factorial ( n!).

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.

input: 3

FOR loops example 2

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 …while loop statement

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

  • Similar to while loops
    • Except the condition is evaluated after the loop body.

The condition is written at the end of the loop to indicate

that it is not evaluated until the loop body is executed.

  • The loop body is always executed at least once , even if

the expression is never true.

Note that the loop begins with the reserved word do Note that the loop ends with semicolon

Do …while loop example 1

// program statement before the do…while loop

System.out.print( "Please, enter a positive number: “ );

do

int num = reader.nextInt();

if (num <= 0 )

System.out.println( “Input error! Try again “ );

} while ( num <= 0 );

// program statements after the do…while loop

Waiting for legal input

Do …while loop example 2

int reversNum = 0 ; // reversed number

System.out.println("enter an integer : ");

int num=reader.nextInt( ); // input variable

do

int lastDigit = num % 10 ;

reversNum = (reversNum* 10 )+ lastDigit;

num = num / 10 ;

} while (num > 0 );

System.out.println(" That number reversed is "+reversNum);

This program section reads an integer and reverses its digit mathematically.