Presentation is loading. Please wait.

Presentation is loading. Please wait.

Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if.

Similar presentations


Presentation on theme: "Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if."— Presentation transcript:

1 Selection statements Repetition statements

2 Lesson plan Main concepts Practice session –If- then –Switch –Nested if

3 Sequential sequence and Control Statement Sequential sequences are insufficient to solve most problems. –Get in the Car. –Drive to the supermarket. Tank nearly empty? –Get in the Car. –If we need gas, then fill up. –Drive to the supermarket.

4 Dependent Actions A question is asked. –A condition is checked. The dependent actions are only performed if the answer is Yes (the condition is true). Sometimes the dependent actions are performed, and sometimes they are not.

5 Example: Logging In Computer access: –Request a login name from the user. –Request a matching password. –If the password matches the login name, then grant access.

6 if ( ) ; else ; Simple Choice Statement if ( ) single statement; else single statement;

7 Boolean expression Boolean expression: is a conditional expression that is evaluated to either true or false. Conditional expression: is a three part expression: Boolean expressions can be combined by boolean operators

8 Relational Operators

9 Boolean operators && meansAND ||means OR ! means NOT

10 Boolean Operators

11 Example of Boolean expression testScore < 62 age > 35 Height > 6.5 (testScore 35) testScore = 95 ???????

12 Block -Represents compound statement { }

13 Example If (age < 35) { coPay = 5; System.out.println(“….”); } Else { coPay = 10; System.out.println(“….”); }

14 Practice Identify invalid boolean expression Identify invalid If-then-else statement

15 Answers for last practice exercise I. (i) (x+1>=0) && (x==1) Valid (ii) x 2 +2x-1 Invalid. Arithmetic expression (iii) ((y%2) != 0) Valid

16 Answers to last practice exercise if (x==y) { System.out.println(“ x=y”); } Else { System.out.println(“ x does not equal to y”); } Should be Lowercase

17 Answers to last practice exercise if (x=y) then { grade = “A”; } else { Grade = “B”; } If we actually ask (is x equal y) then it should be (x==y)

18 Answers to last practice exercise if (x==y) { grade = “A”; else grade = “B”; } Hanging “else”

19 if ( ) ; [else ;] // optional Simple Choice Statement if ( ) single statement; [else single statement;] // optional

20 Answers to some questions Java machine code is generated and stored in *.class files. For example: –LoanCalculator.java –LoanCalculator.class When we compile a java program outside of Textpad, we need to use: javac.exe When we run a java program outside of Textpad, we need to use:java.exe

21 Answers to some questions How to clear screen from DOS: for (int i=0; i<25; i++) System.out.println();

22 Nested If Statement if ( ) if ( ) { statement 1 } else if ( ) { statement 2 } else { statement 3 } else ….

23 Example if (score >= 95) grade= “A”; else if (score >=90) grade=“A-”; else if (score>=85) grade =“B+”;

24 SWITCH statement switch (variable) { : …… : } : has the form case or: default : is a block or a single statement, with break variable: of integer, or char data type

25 Example switch (month) { case 1: System.out.println("January"); break; case 2: System.out.println("February"); break; case 3: System.out.println("March"); break; case 4: System.out.println("April"); break; case 5: System.out.println("May"); break; case 6: System.out.println("June"); break; …………….. default: System.out.println("Not a month!"); break; }

26 Char data type & boolean data type Char: –It is 2 bytes –Char constant is denoted by single quote: for example: ‘s’, ‘h’ Special characters: \bbackspace, \ttab Boolean: - Can be true or false

27 Examples char aLetter; aLetter =‘k’; … aLetter =‘k’; boolean flag; flag = true; … flag = false;

28 Practice Evaluate boolean expressions Identify invalid Switch statement

29 Project 1 (d) Exit the system normally: –System.exit(0); Exit the system abnormally: e.g: System.exit(1); –a nonzero status code indicates abnormal termination.

30 Repetition statements Repetition of statements is a further common facet of everyday life and object behavior: –Repeat every day: Get up; have breakfast; go off to work; have lunch; do more work; have an evening meal; relax; go to bed. Java has three types of loop for repetition: –while, do-while, for.

31 The While Loop while( ){ // Repeat multiple statements. statement 1 statement 2 statement 3... }

32 Example // Print the numbers 1 to maximum. Assuming that maximum is 100 int maximum = 100; int nextToPrint = 1; while(nextToPrint <= maximum){ System.out.print(nextToPrint+" "); // Get ready to print the next number. nextToPrint = nextToPrint +1; } System.out.println(); }

33 The Do Loop do{ // Repeat multiple statements. statement 1 statement 2 statement 3... } while(<boolean expression); Note that the statements in the body of the loop are always executed at least one. Note the final semicolon, which is required.

34 Printing a Sequence int maximum = 100; int nextToPrint = 1; do{ System.out.print(nextToPrint+" "); // Get ready for the next number. nextToPrint = nextToPrint + 1; } while(nextToPrint <= maximum); System.out.println(); }

35 The For Loop Used to repeat statements a fixed number of times: –Add three teaspoons of sugar. –Knock twice and then give the password. Used very often with arrays and collections of objects Similar in effect to the while loop.

36 The For-Loop Outline // Repeat multiple statements. for(initialization; condition; post-body update){ // Statements to be repeated. statement 1 statement 2 statement 3... } Commonly used with increment and decrement operators.

37 Increment and Decrement Used as a shorthand for add-one-to and subtract-one-from: value = value+1; value += 1; value++; Prefix and postfix forms: ++value; --value; value--;

38 A For Loop to Print a Sequence int maximum = 100; int nextToPrint; for(nextToPrint = 1; nextToPrint <= maximum; nextToPrint++){ System.out.print(nextToPrint+“$“+” ”); } System.out.println(); The variable is not available outside the loop 1234567891011 1$ 2$ 3$ 4$ 5$ 6$.

39 Practice Develop a java class called: SumCalculator.java which computes a sum of all integer from 1 to 100 and displays the result to the screen


Download ppt "Selection statements Repetition statements. Lesson plan Main concepts Practice session –If- then –Switch –Nested if."

Similar presentations


Ads by Google