Control statements Mostafa Abdallah Engmostafamuficyahoo com Agenda Iteration
Control statements Mostafa Abdallah Eng_mostafa_mufic@yahoo. com
Agenda • Iteration Statements § While. § Do while. § For. • Branching Statements § break. § continue.
Iteration Statements 4 -3
While • The while statement continually executes a block of statements while a particular condition is true. • Its syntax can be expressed as: while (expression) { statement(s) } 4 -4
While Loop Flow Chart while (condition) { // loop-body; Statement(s); } int count = 0; while (count < 100) { System. out. println("Welcome"); count++; } 4 -5
Essentials of loop Counter • • Control variable (or loop counter). Initial value of the control variable. Increment (or decrement). Continuation condition that determines if looping should continue. 4 -6
Loop with a Sentinel Value • You may use an input value to signify the end of the loop. Such a value is known as a sentinel value. • EX: § Write a program that reads and calculates the sum of an unspecified number of integers. The input 0 signifies the end of the input. 4 -7
Caution • Don’t use floating-point values for equality checking in a loop control. • floating-point values are approximations. 4 -8
do-while • The difference between do-while and while is: § 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 4 -9
Do-While Loop Flow Chart do { // Loop body; Statement(s); } while (condition); 4 -10
For • The for statement provides a compact way to iterate over a range of values. 4 -11
For • Its syntax can be expressed as: 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; 4 -12
Nested loop • Nested loops consist of – An outer loop – One or more inner loops. • Each time the outer loop is repeated, the inner loops are reentered, and started as new loop. – Ex: multiplication table 4 -13
Break & Continue Statements 4 -14
Break • The break statement immediately ends the innermost loop that contains it. • It is generally used with an if statement. – Ex: for (int i = 0; i < 10; i++) { String name = JOption. Pane. show. Input. Dialog("Enter name"); if (name. equals. Ignore. Case("mostafa")) { System. out. println("Finished"); break; } 4 -15
Continue • The continue statement only ends the current iteration. • Program control goes to the end of the loop body. • This keyword is generally used with an if statement 4 -16
Continue • for (int i = 0; i < 5; i++) { String name = JOption. Pane. show. Input. Dialog("Enter name"); String gender = JOption. Pane. show. Input. Dialog("Enter Gender"); if (gender. equals("male")) { continue; } System. out. println("Name : "+name+" Gender : "+gender); } 4 -17
Questions
Thanks
- Slides: 19