Presentation is loading. Please wait.

Presentation is loading. Please wait.

Application development with Java Lecture 6 Rina Zviel-Girshin.

Similar presentations


Presentation on theme: "Application development with Java Lecture 6 Rina Zviel-Girshin."— Presentation transcript:

1 Application development with Java Lecture 6 Rina Zviel-Girshin

2 Rina Zviel-Girshin @ASC2 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 Control flow statements

3 Rina Zviel-Girshin @ASC3 Control Flow Statements The control structures affect the flow of control in a program. Control flow statements can be divided into two types:. selection statements selection statements – if – switch iteration statementsiteration statements – for – while – do-while

4 Rina Zviel-Girshin @ASC4 Selection Statement statement selection?

5 Rina Zviel-Girshin @ASC5 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 statement is performed, otherwise (the value is false) statement is skipped.

6 Rina Zviel-Girshin @ASC6 If Flow Chart statement If ? true false

7 Rina Zviel-Girshin @ASC7 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!”); }

8 Rina Zviel-Girshin @ASC8 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 ” ); }

9 Rina Zviel-Girshin @ASC9 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, statement2 is executed.

10 Rina Zviel-Girshin @ASC10 If..Else Flow Chart statement If..else ? false statement true

11 Rina Zviel-Girshin @ASC11 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!! ” ); }

12 Rina Zviel-Girshin @ASC12 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!”);

13 Rina Zviel-Girshin @ASC13 If & If..Else Statements statement If ? true false statement If..else ? false statement true

14 Rina Zviel-Girshin @ASC14 Switch Statement The switch statement is a choice between doing several things (usually more then two things). The switch statement 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.

15 Rina Zviel-Girshin @ASC15 Switch Syntax switch(exp){ case value1: statement1; break; … case valueN: statementN; break; default: defaultStatement; break; }

16 Rina Zviel-Girshin @ASC16 Choice of Execution If the value of exp equals to value1 then statement1 is performed. … If the value of exp equals to valueN then statementN is performed. If the value of exp is different from value1,..., valueN then defaultStatement is performed.

17 Rina Zviel-Girshin @ASC17 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.

18 Rina Zviel-Girshin @ASC18 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!

19 Rina Zviel-Girshin @ASC19 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]);

20 Rina Zviel-Girshin @ASC20 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

21 Rina Zviel-Girshin @ASC21 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

22 Rina Zviel-Girshin @ASC22 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;

23 Rina Zviel-Girshin @ASC23 Assignment Operators List 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

24 Rina Zviel-Girshin @ASC24 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);

25 Rina Zviel-Girshin @ASC25 The Conditional Operator The conditional operator evaluates a Boolean condition that determines which of two expressions is evaluated. condition ? exp1 : exp2 If condition is true, exp1 is evaluated; if it is false, exp2 is evaluated. The result of the chosen expression is the result of the entire conditional operator.

26 Rina Zviel-Girshin @ASC26 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

27 Rina Zviel-Girshin @ASC27 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

28 Rina Zviel-Girshin @ASC28 More Control Flow What if we want to repeat some sequence of statements many times? statement

29 Rina Zviel-Girshin @ASC29 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.

30 Rina Zviel-Girshin @ASC30 For Statement for( start; limit; step_exp) { statement; } start is a statement that initializes the loop. limit is a Boolean statement that determines when to terminate the loop. It is evaluated before each iteration.

31 Rina Zviel-Girshin @ASC31 For Statement (Cont.) When limit is true statement is 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.

32 Rina Zviel-Girshin @ASC32 For Diagram Limit Condition Start true Statement Step false

33 Rina Zviel-Girshin @ASC33 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); }

34 Rina Zviel-Girshin @ASC34 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.

35 Rina Zviel-Girshin @ASC35 While Diagram Statement true false Boolean Condition

36 Rina Zviel-Girshin @ASC36 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); }

37 Rina Zviel-Girshin @ASC37 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.

38 Rina Zviel-Girshin @ASC38 Do.. While Diagram Boolean Condition Statement true false

39 Rina Zviel-Girshin @ASC39 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.

40 Rina Zviel-Girshin @ASC40 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); }

41 Rina Zviel-Girshin @ASC41 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(); }

42 Rina Zviel-Girshin @ASC42 Break inside a Loop The break statement, (already used within the switch statement), 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)

43 Rina Zviel-Girshin @ASC43 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)

44 Rina Zviel-Girshin @ASC44 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);

45 Rina Zviel-Girshin @ASC45 Any Questions?


Download ppt "Application development with Java Lecture 6 Rina Zviel-Girshin."

Similar presentations


Ads by Google