Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs.

Slides:



Advertisements
Similar presentations
Fundamental of C programming
Advertisements

Decisions If statements in C.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Chapter 5: Control Structures II (Repetition)
Loops Programming. COMP104 Lecture 9 / Slide 2 Shortcut Assignment l C++ has a set of operators for applying an operation to a variable and then storing.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
DECISIONS In the Name of Allah The Most Merciful The Most Compassionate DECISIONS
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
CPS120: Introduction to Computer Science Decision Making in Programs.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
1 Chapter 9 Additional Control Structures Dale/Weems.
Chapter 7 Additional Control Structures. 2 2 void GetYesOrNo (/* out */ char& response) // Inputs a character from the user // Postcondition: response.
1 Lecture 04 Structural Programming in C++ You will learn: i) Operators: relational and logical ii) Conditional statements iii) Repetitive statements.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
Copyright © 2000, Department of Systems and Computer Engineering, Carleton University 1 If’s – The Basic Idea “Program” for hubby: take out garbage.
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 15. Dictionaries (1): A Key Table Class.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
CPS120: Introduction to Computer Science Decision Making in Programs.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Looping I (while statement). CSCE 1062 Outline  Looping/repetition construct  while statement (section 5.1)
CNG 140 C Programming (Lecture set 3)
REPETITION CONTROL STRUCTURE
Chapter 5: Control Structures II (Repetition)
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.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 2.1 Control Structures (Selection)
Bools & Ifs.
Flow of Control.
Flow of Control.
Expression Review what is the result and type of these expressions?
Chapter 4: Control Structures I (Selection)
Conditional Construct
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Bools & Ifs.
Flow of Control.
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Flow of Control.
Presentation transcript:

Prof. amr Goneid, AUC1 CSCE 110 PROGRAMMING FUNDAMENTALS WITH C++ Prof. Amr Goneid AUC Part 3. Selection Constructs

Prof. amr Goneid, AUC2 Selection Constructs

Prof. amr Goneid, AUC3 Selection Constructs Basic C++ Constructs Sequential Constructs Selection with if Statement The Ternary Operator The switch Construct

Prof. amr Goneid, AUC4 1.Basic C++Constructs 1. Basic C++ Constructs Sequential construct : statements performed in succession, single or as a block Selection construct : implemented with if- else and switch statements Repetition construct : implemented with for, while, and do- while loops Functional construct : a program is decomposed into functions (Modules) Object-oriented : a program is decomposed into objects.

Prof. amr Goneid, AUC5 2. Sequential Constructs Simple Statement: c = a + b; // Add a to b, store in c Block statement: {// Swap a with b temp = a; a = b; b = temp; }

Prof. amr Goneid, AUC6 C = condition (logical: false/true, zero/non-zero) e.g. a > b S = simple or compound statement Syntax (1): if (c) ; e.g. if (b > a) x = abs(a-b); 3. Selection with if Statement C S F T

Prof. amr Goneid, AUC7 Syntax (2):if (c) s1; else s2; e.g. if (n == 0) sum = 0; else { sum += x; y = sum / n; cout << n << y; } if Statement C S1S2 F T

Prof. amr Goneid, AUC8 Nested if Statements Example: if (m >= 90) g = ‘A’; else if (m >= 80) g = ‘B’; else if (m >= 70) g = ‘C’; else if (m >= 60) g = ‘D’; else g = ‘F’;

Prof. amr Goneid, AUC9 Conditions & Short Circuit Evaluation A condition is built up using logical and relational operators. It evaluates to true/false, (non-zero / zero) e.g. (a d) if a is less than b then the whole condition is true and (c > d) will not be evaluated. (c >= 65) && (c <= 90) if c is less than 65 then the whole condition is false and (c <= 90) will not be evaluated. (a d) (c >= 65) && (c <= 90)

Prof. amr Goneid, AUC10 4. The Ternary Operator The ternary operator will select to evaluate one of two expressions e1, e2 based on a condition c Syntax: c ? e1 : e2e.g.(k == 2) ? (x + 2) : (x + 5) if k equals 2 the expression is evaluated as x + 2 otherwise it will be evaluated as x + 5. Example: int x = 2; int k = 2; y = ((k == 2) ? (x + 2) : (x + 5)) + 3;cout << y;k++; y = ((k == 2) ? (x + 2) : (x + 5)) + 3;cout << y;

Prof. amr Goneid, AUC11 5. The switch Construct

Prof. amr Goneid, AUC12 The switch Construct Syntax: e = ordinal Expression(int, char, bool) switch (e) { case value1 : s1; break; case value2 : s2; break; … default : s;//Optional }

Prof. amr Goneid, AUC13 switch Construct Example char choice ;cin >> choice; switch (choice) { case ‘R’ : cout << “Red” ; break; case ‘G’ : cout << “Green” ; break; case ‘B’ : cout << “Blue” ; break; default : cout << “Error”; }

Prof. amr Goneid, AUC14 Ommiting break switch (choice) { case ‘R’ : cout << “Red” ; case ‘G’ : cout << “Green” ; case ‘B’ : cout << “Blue” ; default : cout << “Error”; } Input : Routput: RedGreenBlueError Input : Goutput: GreenBlueError Input : Boutput: BlueError Input : Youtput: Error

Prof. amr Goneid, AUC15 More than one Label switch (choice) { case ‘R’ : case ‘r’ : cout << “Red” ; break; case ‘G’ : case ‘g’ : cout << “Green” ; break; case ‘B’ : case ‘b’ : cout << “Blue” ; break; default : cout << “Error”; }

Prof. amr Goneid, AUC16 Example: void main() { int choice; cout << ”Assignment 1:\n”; cout << ”Choose 1 to see the due date\n”; cout << ”Choose 2 to see the maximum ” << ”mark\n”; cout << ”Choose 3 to exit\n”; do { cout << ”Enter a choice and press ” << ”return\n”; cin >> choice;

Prof. amr Goneid, AUC17 switch (choice) { case 1: cout << ”The due date is 13/4/01\n”; break; case 2: cout << ”The maximum mark is 20\n”; break; case 3: cout << ”End of program\n”; break; default: cout << ”Not a valid choice.\n” << ”Choose again, please.\n”; } } while (choice != 3); } // End of program

Prof. amr Goneid, AUC18 Sample Dialogue: Assignment 1: Choose 1 to see the due date Choose 2 to see the maximum mark Choose 3 to exit Enter a choice and press return 1 The due date is 23/4/01 Enter a choice and press return 2 The maximum mark is 20 Enter a choice and press return 4 Not a valid choice. Choose again, please. 3 End of program