Presentation is loading. Please wait.

Presentation is loading. Please wait.

 2006 Pearson Education, Inc. All rights reserved. 1 3 3 Control Statements: Part 1: Selection statements: if, if…else, switch.

Similar presentations


Presentation on theme: " 2006 Pearson Education, Inc. All rights reserved. 1 3 3 Control Statements: Part 1: Selection statements: if, if…else, switch."— Presentation transcript:

1  2006 Pearson Education, Inc. All rights reserved. 1 3 3 Control Statements: Part 1: Selection statements: if, if…else, switch

2  2006 Pearson Education, Inc. All rights reserved. 2 Control Statements 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. The control statements are categorized in almost two groups:  Selection control statements.  Repetition control statements.

3  2006 Pearson Education, Inc. All rights reserved. 3 Control Structures Sequence structure –Programs executed sequentially by default Selection structures –if, if…else, switch Repetition structures –while, do…while, for

4  2006 Pearson Education, Inc. All rights reserved. 4

5 Sequential Execution 5 11 2 3 4 5

6  2006 Pearson Education, Inc. All rights reserved. 6

7 7

8 8

9 9

10 10

11  2006 Pearson Education, Inc. All rights reserved. 11 Fig. 3.1 | Equality and relational operators.

12  2006 Pearson Education, Inc. All rights reserved. 12 Fig.3.2 | Arithmetic operators.

13  2006 Pearson Education, Inc. All rights reserved. If Selection Statement Condition –Expression can be either true or false –Can be formed using equality or relational operators. if statement –If condition is true, body of the if statement executes –If condition is false, body of the if statement does not execute. 13

14  2006 Pearson Education, Inc. All rights reserved. 14 If Selection Statement Selection statements –Pseudocode example If student’s grade is greater than or equal to 60 Print “Passed” –If the condition is true »Print statement executes, program continues to next statement –If the condition is false »Print statement ignored, program continues

15  2006 Pearson Education, Inc. All rights reserved. If Selection Statement 15 Relational Expression

16  2006 Pearson Education, Inc. All rights reserved. 16 Arithmetic Expression

17  2006 Pearson Education, Inc. All rights reserved. 17 Outline fig02_13.cpp (1 of 2) using declarations eliminate need for std:: prefix Can write cout and cin without std:: prefix Declare variables if statement compares values of number1 and number2 to test for equality If condition is true (i.e., values are equal), execute this statement if statement compares values of number1 and number2 to test for inequality If condition is true (i.e., values are not equal), execute this statement Compares two numbers using relational operator

18  2006 Pearson Education, Inc. All rights reserved. 18 Outline fig02_13.cpp (2 of 2) fig02_13.cpp output (1 of 3) (2 of 3) (3 of 3) Compares two numbers using relational operators =

19  2006 Pearson Education, Inc. All rights reserved. 19 Fig. 3.2 | Precedence and associativity of the operators discussed so far.

20  2006 Pearson Education, Inc. All rights reserved. 20 Fig. 3.3 | C++ keywords.

21  2006 Pearson Education, Inc. All rights reserved. Selection Statements: IF Statements Example: Write a program that accept an integer from the user and in case integer is even print out the following message “This number is even” 21

22  2006 Pearson Education, Inc. All rights reserved. Answer: 22

23  2006 Pearson Education, Inc. All rights reserved. Important note: If (x==10) if (x=10) These expressions are not the same. 23

24  2006 Pearson Education, Inc. All rights reserved. Selection Statement: IF Else Statement 24

25  2006 Pearson Education, Inc. All rights reserved. Selection Statement: IF Else Statement 25

26  2006 Pearson Education, Inc. All rights reserved. 26 If…else Double-Selection Statement if – Performs action if condition true if…else – Performs one action if condition is true, a different action if it is false Pseudocode – If student’s grade is greater than or equal to 60 print “Passed” Else print “Failed” C++ code – if ( grade >= 60 ) cout << "Passed"; else cout << "Failed";

27  2006 Pearson Education, Inc. All rights reserved. 27

28  2006 Pearson Education, Inc. All rights reserved. 28 Example: Write a program that accept an integer from the user and print it is Positive or Negative number.

29  2006 Pearson Education, Inc. All rights reserved. Answer 29

30  2006 Pearson Education, Inc. All rights reserved. 30 IF…else Double-Selection Statement (Cont.) Ternary conditional operator ( ?: ) – Three arguments (condition, value if true, value if false ) Code could be written: – cout = 60 ? “Passed” : “Failed” ); ConditionValue if trueValue if false

31  2006 Pearson Education, Inc. All rights reserved. Program of calculating the result of students #include //#include using namespace std; int main() { int grade; cout <<"enter the grade of the student \n"; cin>> grade; if (grade>=60) cout <<"Passed\n"; else cout <<"Failed \n"; getchar(); return 0; } 31

32  2006 Pearson Education, Inc. All rights reserved. Nested IF 32

33  2006 Pearson Education, Inc. All rights reserved. 33 Nested If Dangling- else problem – Compiler associates else with the immediately preceding if – Example if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5“;

34  2006 Pearson Education, Inc. All rights reserved. #include using namespace std; int main() { int x, y; cout<<“Enter any two numbers \n”; cin>>x>>y; if ( x > 5 ) if ( y > 5 ) cout 5"; else cout << "x is <= 5“; getch(); } 34

35  2006 Pearson Education, Inc. All rights reserved. Program of assigning a grade to students #include using namespace std; int main() { int studentGrade ; cout<<“Enter the marks”; cin>> studentGrade ; if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F"; getch(); } 35

36  2006 Pearson Education, Inc. All rights reserved. #include using namespace std; int main() { int x, y; cout<<“Enter any two numbers \n”; cin>>x>>y; if ( x > 5 ) { if ( y > 5 ) cout 5"; } else cout << "x is <= 5"; getch(); } 36

37  2006 Pearson Education, Inc. All rights reserved. 37 3.6 if…else Double-Selection Statement (Cont.) Dangling- else problem (Cont.) – Rewrite with braces ( {} ) if ( x > 5 ) { if ( y > 5 ) cout 5"; else cout << "x is <= 5"; } – Braces indicate that the second if statement is in the body of the first and the else is associated with the first if statement

38  2006 Pearson Education, Inc. All rights reserved. 38 3.6 if…else Double-Selection Statement (Cont.) Compound statement – Also called a block Set of statements within a pair of braces Used to include multiple statements in an if body – Example if ( studentGrade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } – Without braces, cout << "You must take this course again.\n"; always executes

39  2006 Pearson Education, Inc. All rights reserved. #include using namespace std; int main() { int grade; cout<<“Enter grade \n”; cin>>grade; if ( studentGrade >= 60 ) cout << "Passed.\n"; else { cout << "Failed.\n"; cout << "You must take this course again.\n"; } 39

40  2006 Pearson Education, Inc. All rights reserved. 40 If-else if statement Also means to write an if statement within another if statement. – One inside another, test for multiple cases – Once a condition met, other statements are skipped – 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”

41  2006 Pearson Education, Inc. All rights reserved. IF- Else IF statement 41

42  2006 Pearson Education, Inc. All rights reserved. 42 IF- Else IF statement (Cont.) Nested if…else statements (Cont.) – Written In C++ if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F";

43  2006 Pearson Education, Inc. All rights reserved. Program of assigning a grade to students #include using namespace std; int main() { int marks; cout<<“Enter the marks”; cin>>marks; if ( marks >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F"; getch(); } 43

44  2006 Pearson Education, Inc. All rights reserved. 44 IF- Else IF statement(Cont.) Nested if…else statements (Cont.) – Written In C++ (indented differently) if ( studentGrade >= 90 ) cout = 80 ) cout = 70 ) cout = 60 ) cout << "D"; else cout << "F";

45  2006 Pearson Education, Inc. All rights reserved. 45 Logical Operators Logical operators – Allows for more complex conditions Combines simple conditions into complex conditions C++ logical operators – & (logical AND) – | (logical OR) – ! (logical NOT)

46  2006 Pearson Education, Inc. All rights reserved. Logical Operators 46

47  2006 Pearson Education, Inc. All rights reserved. 47

48  2006 Pearson Education, Inc. All rights reserved. 48

49  2006 Pearson Education, Inc. All rights reserved. 49 Logical Operators (Cont.) 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 يقوم المترجم بفحص الجزئين ( الشرط الأول والشرط الثاني )

50  2006 Pearson Education, Inc. All rights reserved. 50 Fig. 3.5 | && (logical AND) operator truth table.

51  2006 Pearson Education, Inc. All rights reserved. 51 Logical Operators (Cont.) Logical OR ( | ) Operator – Consider the following if statement if ( ( semesterAverage >= 90 ) |( finalExam >= 90 )) cout << “Student grade is A” << endl; – 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

52  2006 Pearson Education, Inc. All rights reserved. 52 Fig. 3.6 | || (logical OR) operator truth table.

53  2006 Pearson Education, Inc. All rights reserved. 53 Logical Operators (Cont.) 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 في هذه الحالة يقوم المترجم بفحص الجزء الأول ( الشرط الأول ) إذا كانت قيمته صحيحة يتابع فحص الجزء الثاني ( الشرط الثاني ) ، أما إذا كانت قيمته خطأ فلا يواصل في فحص الجزء الثاني.

54  2006 Pearson Education, Inc. All rights reserved. Example 54

55  2006 Pearson Education, Inc. All rights reserved. Example 55

56  2006 Pearson Education, Inc. All rights reserved. What is the output? #include using namespace std; int main() { int a=7; int b=8; if ((a>10) & (++b>7)) { cout<<"that is right \n"; cout<<b; } return 0; } 56 #include using namespace std; int main() { int a=7; int b=8; if ((a>10) & (++b>7)) cout<<"that is right \n"; cout<<b; return 0; } No output 9 #include using namespace std; int main() { int a=7; int b=8; if ((a>10) & (++b>7)) cout<<"that is right \n"; cout<<b; return 0; } 8

57  2006 Pearson Education, Inc. All rights reserved. What is the Output? 57 #include using namespace std; int main() { int x=5; if (x=4) cout<<“True \n"; else cout<<“False \n”; return 0; } #include using namespace std; int main() { int x=5; if (x==4) cout<<“True \n"; else cout<<“False \n”; return 0; } True بما أن القيمة غير صفر فالشرط صحيح حسب القاعدة False #include using namespace std; int main() { int x=5; if (x=0) cout<<“True \n"; else cout<<“False \n”; return 0; } False بما أن القيمة صفر فسيعتبر الشرط false حسب القاعدة

58  2006 Pearson Education, Inc. All rights reserved. What is the output for each program? #include using namespace std; int main() { int x, y; x=7; y=10; if ( x > 5 ) { if ( y > x ) cout 5"; } else cout << "x is <= 5"; } 58 #include using namespace std; int main() { int x, y; X=4; y=10; if ( x > 5 ) { if ( y > x ) cout 5"; } else cout << "x is <= 5"; } #include using namespace std; int main() { int x, y; X=7; y=5 if ( x > 5 ) { if ( y > x ) cout 5"; } else cout << "x is <= 5"; } x and y are > 5x is <= 5No output

59  2006 Pearson Education, Inc. All rights reserved. 59 Logical Operators (Cont.) Logical Negation ( ! ) Operator – Unary operator – Returns true when its operand is false, and vice versa – Example if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; is equivalent to: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl; Stream manipulator boolalpha – Display bool expressions in words, “true” or “false”

60  2006 Pearson Education, Inc. All rights reserved. What is output? 60 #include using namespace std; int main() { int x=5; if (!(x=4)) cout<<“True \n"; else cout<<“False \n”; return 0; } False

61  2006 Pearson Education, Inc. All rights reserved. PART TWO SWITCH STATEMENT 61

62  2006 Pearson Education, Inc. All rights reserved. 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. 62

63  2006 Pearson Education, Inc. All rights reserved. Selection Statements: Switch Statement 63 32

64  2006 Pearson Education, Inc. All rights reserved. Selection Statements: Switch Statement 64

65  2006 Pearson Education, Inc. All rights reserved. Selection Statements: Switch Statement 65

66  2006 Pearson Education, Inc. All rights reserved. Selection Statements: Switch Statement 66 Wrong: Case >=50 Case a+b

67  2006 Pearson Education, Inc. All rights reserved. 67 switch Multiple-Selection Statement (Cont.) switch statement – Controlling expression Expression in parentheses (أقواس)after keyword switch – case labels Compared with the controlling expression Statements following the matching case label are executed – Braces are not necessary around multiple statements in a case label – A break statements causes execution to proceed ( تمضي)with the first statement after the switch Without a break statement, execution will fall through to the next case label

68  2006 Pearson Education, Inc. All rights reserved. 68 switch Multiple-Selection Statement switch statement – Used for multiple selections – Tests a variable or expression Compared against constant integral expressions to decide on action to take – Any combination of character constants and integer constants that evaluates to a constant integer value

69  2006 Pearson Education, Inc. All rights reserved. 69 switch Multiple-Selection Statement (Cont.) switch statement (Cont.) – default case Executes if no matching case label is found Is optional – If no match and no default case Control simply continues after the switch

70  2006 Pearson Education, Inc. All rights reserved. Selection Statements: Switch Statement 70 يتم إختبار القيمة الرقمية للحرف وهي عبارة عن ASCII CODE

71  2006 Pearson Education, Inc. All rights reserved. Selection Statements: Switch Statement 71

72  2006 Pearson Education, Inc. All rights reserved. What is the output #include using namespace std; int main() { int i=20; switch(i+10) { case 10: cout<<"I am in level 1 \n"; break; case 20: cout<<"I am in level 2 \n"; break; case 30: cout<<"I am in level 3 \n“; break; default: cout<<"I am in default \n"; } return 0; } 72 I am in level 3

73  2006 Pearson Education, Inc. All rights reserved. What is the output? #include using namespace std; int main() { int i=20; switch(i+10) { case 10: cout<<"I am in level 1 \n"; //break; case 20: cout<<"I am in level 2 \n"; //break; case 30: cout<<"I am in level 3 \n“; //break; default: cout<<"I am in default \n"; } return 0; } 73 I am in level 1 I am in level 2 I am in level 3 I am in default

74  2006 Pearson Education, Inc. All rights reserved. 74

75  2006 Pearson Education, Inc. All rights reserved. Error in this switch expression 75

76  2006 Pearson Education, Inc. All rights reserved. Error in case statement 76

77  2006 Pearson Education, Inc. All rights reserved. Error in case statement 77

78  2006 Pearson Education, Inc. All rights reserved. Error in case statement 78

79  2006 Pearson Education, Inc. All rights reserved. Can I apply this example for switch? 79 No Because in this if example we put the condition between range. We can not put in switch. We must put specific value.

80  2006 Pearson Education, Inc. All rights reserved. Switch and If else 80

81  2006 Pearson Education, Inc. All rights reserved. 81

82  2006 Pearson Education, Inc. All rights reserved. 82

83  2006 Pearson Education, Inc. All rights reserved. References مراجع: هنالك شرائح تم أخذها من محاضرات المحاضر محمد إبراهيم الدسوقي Programming Basics For Beginners 83

84  2006 Pearson Education, Inc. All rights reserved. End 84


Download ppt " 2006 Pearson Education, Inc. All rights reserved. 1 3 3 Control Statements: Part 1: Selection statements: if, if…else, switch."

Similar presentations


Ads by Google