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

Slides:



Advertisements
Similar presentations
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Advertisements

 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
C++ Operators CS242 COMPUTER PROGRAMMING T.Banan Al-Hadlaq.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Computer Science Department Relational Operators And Decisions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Control Structures Week Introduction -Representation of the theory and principles of structured programming. Demonstration of for, while,do…whil.
1 CSCE 1030 Computer Science 1 Control Statements in Java.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Selection. Flow Chart If selection If/else selection Compound statement Switch.
Chapter 05 (Part III) Control Statements: Part II.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Chapter 3 - Structured Program Development Outline 3.1Introduction 3.2Algorithms 3.3Pseudocode 3.4Control Structures 3.5The If Selection Structure 3.6The.
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements I.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter#3 Part1 Structured Program Development in C++
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1.
C++ Programming Lecture 5 Control Structure I (Selection) – Part I The Hashemite University Computer Engineering Department (Adapted from the textbook.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Control Statements: Part1  if, if…else, switch 1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
 2006 Pearson Education, Inc. All rights reserved if…else Double-Selection Statement if – Performs action if condition true if…else – Performs.
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
 2008 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
Branching statements.
Introduction to Computer Programming
The if…else Selection Statement
Control Structures Sequential execution Transfer of control
- Standard C Statements
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
EGR 2261 Unit 4 Control Structures I: Selection
Topic 3, Selection Statements
Chapter 2.1 Control Structures (Selection)
CSC113: Computer Programming (Theory = 03, Lab = 01)
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Programming Fundamentals
Control Statements: Part 2
Structured Program
3 Control Statements:.
Chapter 6 Control Statements: Part 2
Chapter 4: Control Structures I (Selection)
2.6 The if/else Selection Structure
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

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

 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.

 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

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

Sequential Execution

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

7

8

9

10

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

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

 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

 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

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

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

 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

 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 =

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

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

 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

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

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

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

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

 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";

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

 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.

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

 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

 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

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

 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“;

 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

 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

 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

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

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

 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

 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”

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

 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";

 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

 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";

 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)

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

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

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

 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 يقوم المترجم بفحص الجزئين ( الشرط الأول والشرط الثاني )

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

 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

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

 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 في هذه الحالة يقوم المترجم بفحص الجزء الأول ( الشرط الأول ) إذا كانت قيمته صحيحة يتابع فحص الجزء الثاني ( الشرط الثاني ) ، أما إذا كانت قيمته خطأ فلا يواصل في فحص الجزء الثاني.

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

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

 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

 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 حسب القاعدة

 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

 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”

 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

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

 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

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

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

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

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

 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

 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

 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

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

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

 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

 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

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

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

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

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

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

 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.

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

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

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

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

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