450 likes | 459 Vues
Application development with Java. Lecture 6 Rina Zviel-Girshin. Control flow statements. Control Structures and Statements. There are several types of statements: Expression statements Declaration statements Iteration statements Selection statements Examples:
E N D
Application development with Java Lecture 6 Rina Zviel-Girshin
Control flow statements Control Structures and Statements • There are several types of statements: • Expression statements • Declaration statements • Iteration statements • Selection statements • Examples: int number; // declaration statement number = 25; // assignment statement number++; // increment expression statement Rina Zviel-Girshin @ASC
Control Flow Statements • The control structures affect the flow of control in a program. • Control flow statements can be divided into two types:. • iteration statements • for • while • do-while • selection statements • if • switch Rina Zviel-Girshin @ASC
statement statement statement statement Selection Statement selection? Rina Zviel-Girshin @ASC
If Statement • Doing something under some condition: if (Boolean_condition) statement; • Boolean_condition is a Boolean expression that can have true or false values. • If the value is true than statementis performed, otherwise (the value is false) statementis skipped. Rina Zviel-Girshin @ASC
statement statement If Flow Chart If ? true false statement Rina Zviel-Girshin @ASC
Boolean Expression Example if ( x<1 || x%2!=0 ) { System.out.println( “x is not a positive even number!”); } if ( y+2 < x && !(y == 17) ) { System.out.println( “y is not 17 and is smaller than x by more than 2!”); } Rina Zviel-Girshin @ASC
If Statement Example class FailTest { public static void main(String[] args) { int grade = Integer.parseInt(args[0]); System.out.println( “Your grade is: ” + grade); if (grade < 60) System.out.println(“You failed”); } } Rina Zviel-Girshin @ASC
If .. Else Statement • A choice between doing two things: if (Boolean_condition) statement1; else statement2; • If the Boolean_condition is true, statement1 is executed; if the condition is false,statement2is executed. Rina Zviel-Girshin @ASC
statement statement If..Else Flow Chart If..else ? true false statement statement Rina Zviel-Girshin @ASC
If .. Else Example class FailTest { public static void main(String[] args) { int grade=Integer.parseInt(args[0]); System.out.println( “Your grade is: ”+grade); if (grade < 60) System.out.println(“You failed”); else System.out.println(“You passed!!”); } } Rina Zviel-Girshin @ASC
Nested If’s if (a != b) if (a > b) System.out.println(a + ” is greater”); else System.out.println(b + ” is greater”); else System.out.println(“They are equal!”); Rina Zviel-Girshin @ASC
statement statement statement statement If & If..Else Statements If ? true If..else ? true false false statement statement statement Rina Zviel-Girshin @ASC
Switch Statement • The switch statement is a choice between doing several things (usually more then two things). • The switchstatement evaluates an expression, then attempts to match the result to one of a series of values. • Execution transfers to statement list associated with the first value that matches. Rina Zviel-Girshin @ASC
Switch Syntax switch(exp){ case value1: statement1; break; … case valueN: statementN; break; default: defaultStatement; break; } Rina Zviel-Girshin @ASC
Choice of Execution • If the value of exp equals to value1 then statement1 is performed. • … • If the value of exp equals to valueN then statementNis performed. • If the value of exp is different from value1 , ... , valueN then defaultStatement is performed. Rina Zviel-Girshin @ASC
Switch Statement Details • exp can be an integer variable or an expression that evaluate to an integer value. • statementN can be empty or can be a set of instructions. • A default case can be added to the end of the list of cases, and will execute if no other case matches. Rina Zviel-Girshin @ASC
The Break Statement • The break statement is usually used to terminate the statement list of each case. This causes the control to jump to the end of the switch statement and continue. • Note: if the break statement is omitted execution continues (“falls”) to the next case! Rina Zviel-Girshin @ASC
Example public class ST { public static void main(String[] args) { // variables int action=Integer.parseInt(args[0]); int a=Integer.parseInt(args[1]); int b=Integer.parseInt(args[2]); Rina Zviel-Girshin @ASC
Example switch(action) { case 1: System.out.println(a+"+"+b+"="+add(a,b)); break; case 2: System.out.println(a+"*"+b+"="+mult(a,b)); break; default: System.out.println("Illegal input"); }//end of switch }//end of main Rina Zviel-Girshin @ASC
Example // methods static int add(int x,int y) { return x+y; } static int mult(int x,int y) { return x*y; } } //end of class Rina Zviel-Girshin @ASC
More Assignment Operators • Often we perform an operation on a variable, then store the result back into that variable. • Java provides assignment operators that simplify that process. Instead of : sum = sum + value; you can write: sum += value; Rina Zviel-Girshin @ASC
Operator += -= *= /= %= Example x += y x -= y x *= y x /= y x %= y Equivalent To x = x + y x = x - y x = x * y x = x / y x = x % y Assignment Operators List Rina Zviel-Girshin @ASC
Right Hand Side Expression • The right hand side of an assignment operator can be a complete expression. • The entire right-hand expression is evaluated first, then combined with the additional operation. result /= (total-MIN) % n; is equivalent to result = result / ((total-MIN) % n); Rina Zviel-Girshin @ASC
The Conditional Operator • The conditional operator evaluates a Boolean condition that determines which of two expressions is evaluated. condition ? exp1 : exp2 • If conditionis true, exp1is evaluated; if it is false, exp2is evaluated. • The result of the chosen expression is the result of the entire conditional operator. Rina Zviel-Girshin @ASC
Conditional Operator Usage • The conditional operator is similar to an if-else statement, except that it is an expression that returns a value: int max = (a > b) ? a : b; • If a is greater that b, then a is assigned to max; otherwise, b is assigned to max. • The conditional operator is ternary, meaning it requires three operands Rina Zviel-Girshin @ASC
Conditional Operator Example System.out.println ("Your change is " + count + ((count == 1) ? "Dime" : "Dimes”)); If count equals 1, "Dime" is printed, otherwise "Dimes" is printed Rina Zviel-Girshin @ASC
statement statement statement More Control Flow • What if we want to repeat some sequence of statements many times? statement Rina Zviel-Girshin @ASC
Iteration Statements • Iteration statements are also called loop control structures. • A loop is a repetition of certain pieces of the code several times. • In Java there are Three types of Loops: • for loop, while loop, do loop. Rina Zviel-Girshin @ASC
For Statement for( start; limit; step_exp) { statement; } • start is a statement that initializes the loop. • limitis a Boolean statement that determines when to terminate the loop. • It is evaluated before each iteration. Rina Zviel-Girshin @ASC
For Statement (Cont.) • Whenlimit is true statementis performed, when it is false the loop is terminated. • step_exp is an expression that is invoked after each iteration of the loop and is called the step of the loop. • The for loop is often used for counting from start to limit by a step size. Rina Zviel-Girshin @ASC
Start false Limit Condition true Statement Step For Diagram Rina Zviel-Girshin @ASC
For Example class For_Example { public static void main(String[] args) { int fact = 1; for(int k=1; k<5; k++) { fact *= k; System.out.println(“The factorial of “ + k + “ is:“ + fact); } } } Rina Zviel-Girshin @ASC
While Statement while( Boolean_cond) statement; • The value of Boolean_cond can be: • true and than the statement is performed • false and than loop terminates • The statement is executed over and over until the boolean_condition becomes false. Rina Zviel-Girshin @ASC
false Boolean Condition true Statement While Diagram Rina Zviel-Girshin @ASC
While Example class While_Example { public static void main(String[] args) { int sum=0,k=0; while(sum<100) { sum=sum+k; k++; System.out.print(“the sum of 0 to “ + k + ” is ” + sum); } } } Rina Zviel-Girshin @ASC
Do.. While Statement do { statement; } while (Boolean_cond); • First, the statement performed once! • Then, the Boolean_cond is checked. If it is true the next iteration of the loop is performed. If it is false, the loop terminates. Rina Zviel-Girshin @ASC
Statement Boolean Condition true false Do.. While Diagram Rina Zviel-Girshin @ASC
Infinite Loops • If Boolean_cond in one of the loop structures is always true then loop will be executed forever - it is ‘unbounded’. • Such a loop is called an infinite loop. • The infinite loop will execute until the program is interrupted. Rina Zviel-Girshin @ASC
Infinite Loop Example // Two infinite loops program Class Infinity { public static void main(String[] args){ int count=0; for( ; ; ) System.out.print(“Infinity”); while(count<10) { System.out.println( “Another infinite loop”); System.out.println( “The counter is “+counter); } } } Rina Zviel-Girshin @ASC
Nested Loops Example for (int row = 1; row <= numRows; row++) { for (int column = 1; column <= numColumns; column++) { System.out.print(row * column + “ ); } System.out.println(); } Rina Zviel-Girshin @ASC
Break inside a Loop • The break statement, (already used within the switchstatement), can also be used inside a loop • When the break statement is executed, control jumps to the statement after the loop (the condition is not evaluated again) Rina Zviel-Girshin @ASC
Continue inside a Loop • A similar statement to the break is continue inside loops • When the continue statement is executed, control jumps to the end of the loop and the condition is evaluated (possibly entering the loop statements again) Rina Zviel-Girshin @ASC
Break and Continue Example for (;;) { int n = args[0]; if (n == 0) break; if (n%2) continue; sum += n; } System.out.print( “The sum of all input even numbers is “ + sum); Rina Zviel-Girshin @ASC
Any Questions? Rina Zviel-Girshin @ASC