Presentation is loading. Please wait.

Presentation is loading. Please wait.

Control Statements: Part1  if, if…else, switch 1.

Similar presentations


Presentation on theme: "Control Statements: Part1  if, if…else, switch 1."— Presentation transcript:

1 Control Statements: Part1  if, if…else, switch 1

2  Normally, statements in program are executed one after the other in the order in which they’re written.  This is called sequential execution  Example: calculate area of rectangle.  There are control statements enable you to specify that the next one in sequence.  This is called transfer of control. control statements  The control statements are categorized in almost two groups:  Selection control statements.  Repetition control statements. 2

3  Sequential execution ◦ Statements are normally executed one after the other in the order in which they are written  Transfer of control ◦ Specifying the next statement to execute that is not necessarily the next one in order ◦ Can be performed by the goto statement  Structured programming eliminated goto statements 3

4  Java has three kinds of control structures  Sequence structure  Programs executed sequentially by default  Selection structures  if, if…else, switch  Repetition structures  while, do…while, for 4

5  Selection Statements ◦ if statement  Single-selection statement ◦ if…else statement  Double-selection statement ◦ switch statement  Multiple-selection statement 5

6  Are used to choose among alternative courses of action.  if Single-Selection Statement ◦ Execute an action if the specified condition is true  For example: suppose the passing mark on an exam is 60. The pseudo code statement: if student’s mark is greater than or equal to 60 then Print “Passed” 6

7 7

8  Pseudo code  If student’s grade is greater than or equal to 60  Print “Passed”  If the condition is false, the Print statement is ignored, and next pseudo code statement in order is performed.  The preceding pseudo code If in Java:  If (studentGrade>=60)  System.out.println(“Passed”); 8

9  Condition ◦ Expression can be either true or false  if statement ◦ If a condition is true, then the body of the if statement executed ◦ Control always resumes after the if statement ◦ Conditions in if statements can be formed using equality or relational operators (next slide) 9

10 10

11  Relational expression: is an expression which compares two operands and returns a true or false answer.  Examples:  a>b c<d a==b  Relational expressions are used to test the conditions in selections, and looping statements. 11

12 12 Decision Making: Equality and Relational operators Condition An expression that can be true or false

13  Compar ison.java  (1 of 2)  1. Class Comparison 1.1 main 1.2 Declarations 1.3 Input data (nextInt) 1.4 Compare two inputs using if statements 13 Test for equality, display result using printf. Compares two numbers using relational operator <.

14  Comparis on.java  (2 of 2)  Program output 14 Compares two numbers using relational operators >, =.

15 ◦ Line 6: begins class Comparison declaration ◦ Line 12: declares Scanner variable input and assigns it a Scanner that inputs data from the standard input ◦ Lines 14-15: declare int variables ◦ Lines 17-18: prompt the user to enter the first integer and input the value ◦ Lines 20-21: prompt the user to enter the second integer and input the value 15

16 ◦ if statement to test for equality using (==)  If variables equal (condition true)  Line 24 executes  If variables not equal, statement skipped  No semicolon at the end of line 23  Empty statement  No task is performed ◦ Lines 26-27, 29-30, 32-33, 35-36 and 38-39  Compare number1 and number2 with the operators !=,, =, respectively 16 23 if ( number1 == number2 ) 24 System.out.printf( "%d == %d\n", number1, number2 );

17  Confusing the equality operator, ==, with the assignment operator, =, can cause a logic error or a syntax error. The equality operator should be read as “is equal to,” and the assignment operator should be read as “gets” or “gets the value of.” To avoid confusion, some people read the equality operator as “double equals” or “equals equals.” 17

18 18

19

20 20

21

22 22  Ternary conditional operator ( ?: ) ◦ Three arguments (condition, value if true, value if false )  Code could be written: ◦ System.out.println( grade >= 60 ? “Passed” : “Failed” ); ConditionValue if trueValue if false

23  Nested if…else statements ◦ if…else statements can be put inside other if…else statements  Dangling-else problem ◦ elses are always associated with the immediately preceding if unless otherwise specified by braces { }  Blocks ◦ Braces { } associate statements into blocks ◦ Blocks can replace individual statements as an if body  Empty statements ◦ Represented by placing a semicolon ( ; ) where a statement would normally be Can be used as an if body 23

24 24

25  Can test multiple cases by placing if … else statements inside other if… else statements to create nested if…. Else statements  Pseudo code:  Example  If student’s grade is greater than or equal to 90 ◦ Print “A” ◦ Else If student’s grade is greater than or equal to 80 Print “B” Else If student’s grade is greater than or equal to 70 Print “C” Else If student’s grade is greater than or equal to 60 Print “D” Else  Print “F” 25

26 ◦ This pseudo code may be written in Java as  if ( studentGrade >= 90 ) System.out.println("A“); else if (studentGrade >= 80 ) System.out.println("B“); else if (studentGrade >= 70 ) System.out.println("C“); else if ( studentGrade >= 60 ) System.out.println("D“); else System.out.println("F“); 26

27 27

28 28

29 29

30 30

31  Logical operators ◦ Allows for more complex conditions ◦ Combines simple conditions into complex conditions  Java operators ◦ & (logical AND) ◦ | (logical OR) ◦ ! (logical NOT) 31

32 32

33  Logical AND (&) Operator ◦ Consider the following if statement  if (( gender == 1) & (age >= 65 ))  seniorFemales++; ◦ Combined condition is true  If and only if both simple conditions are true ◦ Combined condition is false  If either or both of the simple conditions are false 33

34 34

35  Logical OR (|) Operator ◦ Consider the following if statement  if ( ( semesterAverage >= 90 ) |( finalExam >= 90 ))  Syste.out.println ( “Student grade is A” ); ◦ Combined condition is true  If either or both of the simple conditions are true ◦ Combined condition is false  If both of the simple conditions are false 35

36 36

37  Short-Circuit Evaluation of Complex Conditions ◦ Parts of an expression containing && or || operators are evaluated only until it is known whether the condition is true or false ◦ Example  (( gender == 1 ) && ( age >= 65 ))  Stops immediately if gender is not equal to 1  Since the left-side is false, the entire expression must be false 37

38  A for exam marks greater than or equal 90 and less than or equal 100  B for exam marks greater than or equal 80 and less than 90  C for exam marks than or equal to 70 and less than 80  D for exam marks than or equal to 60 and less than 70  F for all other marks If (marks>=90 && marks<=100) System.out.println(“A”); Else If (marks>=80 && marks<90) System.out.println(“B”); Else If (marks>=70 && marks<80) System.out.println(“C”); Else If (marks>=60 && marks<70) System.out.println(“D”); Else System.out.println(“F”); 38

39 int a=7; int b=8; if ((a>10) & (++b>7)) { System.out.print(" that is right” ); System.out.print(b); } 39 int a=7; int b=8; if ((a>10) & (++b>7)) System.out.print("that is right \n“); System.out.print(b); No output 9 int a=7; int b=8; if ((a>10) &&(++b>7)) Sytem.out.print("that is right \n”); System.out.print(b); 8

40 int i = 1; int j = 2; int k = 3; if (i > j) { if (i > k) System.out.println("A"); } else System.out.println("B"); 40

41  Logical Negation (!) Operator ◦ Unary operator ◦ Returns true when its operand is false, and vice versa ◦ Example  if ( !( grade == sentinelValue ) ) System.out.println( "The next grade is " +grade); is equivalent to: if ( grade != sentinelValue ) System.out.println("The next grade is " +grade );  Stream manipulator boolalpha ◦ Display bool expressions in words, “true” or “false” 41

42 42

43 43

44 44

45 45

46 46

47 PART TWO SWITCH STATEMENT 47

48 48

49 Selection Statements: Switch Statement The switch control statement that allows us to make a decision from the number of choices. Expression: It could be an integer constant like 1,2 or 3, or an expression that evaluates to an integer. Constant: is a specific value.

50 32 50

51 51

52 52

53 53

54 54

55 55 Wrong: Case >=50 Case a+b

56 char ch=‘’; Switch (ch) { case ‘a’: System.out.println(“a”); break; case ‘b’: System.out.println(“b”); break; case ‘c’: System.out.println(“c”); break; default: System.out.println(“Default”); }

57 int i=3; Switch (i) { case 1: System.out.println(“Welcome”); break; case 2: System.out.println(“Hello”); break; case 3: System.out.println(“JAVA”); break; default: System.out.println(“Default”); }

58  مراجع :  بعض الشرائح تمت الإستعانة بها من محاضرات :  د. حكمت جابر  أ. محمد إبراهيم الدسوقي


Download ppt "Control Statements: Part1  if, if…else, switch 1."

Similar presentations


Ads by Google