Control Structures Selection or Decision or Branching.

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
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.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
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.
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
C++ for Engineers and Scientists Third Edition
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Computer Science Department Relational Operators And Decisions.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Selection Structures (if & switch statements) (CS1123)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Chapter 5 Control Structure (Repetition). Objectives In this chapter, you will: Learn about repetition (looping) control structures Explore how to construct.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Control Structures Repetition or Iteration or Looping Part II.
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.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
CCSA 221 Programming in C CHAPTER 6 MAKING DECISIONS 1.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Control Structures RepetitionorIterationorLooping Part I.
Chapter 4 Control Structures: Selection We introduced the three fundamental control structures from which all programs are developed: 1. Sequence structures.
Chapter Making Decisions 4. Relational Operators 4.1.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Chapter 4, cont: Control Structures if else nested if switch.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Control Statements: Part1  if, if…else, switch 1.
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.
 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.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Branching statements.
Chapter 4: Making Decisions.
A mechanism for deciding whether an action should be taken
Chapter 4: Making Decisions.
3 Control Statements:.
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
Control Structures Selection or Decision Branching.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

Control Structures Selection or Decision or Branching

Decision Which the statements are executed Two types: simple alternativeif compound alternativeif...else

Single Alternative Decision An action is taken if the condition is true, otherwise the control goes to the next statement.

Syntax if (expression) statement If expression is true, statement is executed; otherwise statement is skipped. no ; * * Single Alternative Decision if (stomach empty) eat a Snickers bar; Example: if (stomach == empty) eat a Snickers bar; = note: 2 = signs

An expression is any combination of variables, constants, or function calls that evaluate to a value. ex.5x + y a = 3 + j Nn++ f(12.3, a, “Yvonne”) Syntax if (expression) statement Single Alternative Decision * *

Syntax if (expression) statement If expression is true, statement is executed; otherwise statement is skipped. no ; * * Single Alternative Decision if (stomach == empty) eat a Snickers bar; eat a marshmallow sunday; Example: if (stomach == empty) eat a Snickers bar; eat a marshmallow sunday; = note: 2 = signs

Single Alternative Decision if (grade >= 90) cout = 90) cout << “Congratulations!\n”; cout << “Your grade is “ << grade << “.\n";

The Compound Statement Syntax if (expression) { { statement; statement; if (expression) { statement; statement; } } Example: if (u > v){ a = 1; b = 2; if ( u > z) { x =11; y = 12; }} * * * * The compound statement is itself a statement.

if if Examples Valid: Valid: if (y != 0.0) z = x/y; if (a < b && b < c) { d = a + b + c; cout << "All OK\n"; } * * Not Valid Not Valid: if b == a area = a * a; if (a < b) && (b < c) if (a < b) ; Valid But... if (a < b) ;

if if Problems Using = in place of == Using = in place of == if (toss == 7) cout << “You win the bet.”; What is the difference between these two? if (toss == 7) cout << “You win the bet.”; if (toss = 7) cout << “You win the bet.”;

if Compounding 2 if Statements if (j < k) {min = j; cout << “the smaller number is “ << min; } if (j < k) cout << "j is smaller than k\n"; More Efficient: More Efficient: if (j < k) {min = j; cout << “the smaller number is “ << min; cout << "j is smaller than k\n"; } *

Double Alternative Decision An action (or set of actions) is taken if the condition is true, another action (or set of actions) is taken if the condition is false, then the control goes to the next statement. if... else if... else is the typical double alternative. *

if-else The if-else Statement Syntax if (expression) statement1 else statement2 If expression is nonzero then statement1 is executed and statement2 is skipped. If expression is zero statement1 is skipped and statement2 is executed.

if... else if... else Examples if (stomach == empty) { eat a pizza; eat a Snickers bar; } else eat a salad; if ( j < k ) { min = j; k = k * 3; } else { min = k; j = j * 3; } *

Interactive Program Finding the minimum of three values

Finding the Minimum of Three Values * * * int x, y, z, min; cout << “Input three integers: “; cin >> x >> y >> z; if (x < y) min = x; else min = y; if (z < min) min = z; cout << “The minimum value is “ << min << ‘\n’;

Output: Input three integers: Interactive Program Finding the minimum of three values * The minimum value is -12 _

if-else The if-else Statement Syntax if (expression) statement1 else statement2 if (a > b) max = a; else max = b; expression1 ? expression2 : expression3 max = (a > b) ? a : b; * * *

if Nested if Statements A nested if statement is an if statement that is included within another if statement. Syntax if (expression1) { if (expression2) statement }

if Nested if Example if (number == secretnumber) cout << “You guessed it!”; if (number != secretnumber) { cout << “Sorry, that’s not the number.\n”; if (number > secretnumber) cout << “You guessed too high.\n”; else cout << “You guessed too low.\n”; } *

2 statement1 3 else if (expression2) 4 statement else if (expressionN) 7 statementN 8 else 9 last statement 10 next statement * if...else Chained if...else Example Syntax 1 if (expression1)

if...else Chained if...else Example if (total >=90) grade = ‘A’; else if (total >= 80) grade = ‘B’; else if (total >= 70) grade = ‘C’; else if (total >= 60) grade = ‘D’; else grade = ‘E’; next statement *

else The Dangling else if (avg >= 60.0) if (avg < 70.0) cout << “Passing, but marginal”; else cout << “Failing”; * if (avg >= 60.0) { if (avg < 70.0) cout << “Passing, but marginal”; } else cout << “Failing”;

else The Dangling else if (avg >= 60.0) { if (avg < 70.0) cout << “Passing, but marginal”; } else cout << “Failing”;

AND vs. OR if( (rel == 'S') && (rel == 'M') && (rel == 'F') ) cout << "\nImmediate family.\n"; if( (rel != 'S') && (rel != 'M') && (rel != 'F') ) { cout << "\nNot immediate family,\n"; cout << " but a close relation.\n"; }

Random Numbers #include // defines rand() & srand() #include // defines time() in main():srand(time(NULL));  num1 = 1 + rand() % 3; num1 = 1 + rand() % 6; num1 = 6 + rand() % 5; 1, 2, 3 1, 2, 3, 4, 5, 6 6, 7, 8, 9, 10 how many numbers * * * starting number

switch The switch Statement « Similar to if statements « Can list any number of branches « Used in place of nested if statements « Avoids confusion of deeply nested ifs

switch The switch Statement Syntax switch (expression) { case value1: statement1; break; case value2: statement2; break;  case valuen: statementn; break; default: statement; } no ; use : *

switch The switch Statement switch Syntax switch (expression) { case value1: statement1; break break; case value2: statement2; break break;  case valuen: statementn; break break; default default: statement; } no ; use :

switch The switch Statement switch (let_grd) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; cont.

switch The switch Statement case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; } next statement

switch The switch Statement switch (let_grd) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; }

break The break Statement switch (let_grd) { case ‘A’: cout << “Grade is between 90 & 100”; break; case ‘B’: cout << “Grade is between 80 & 89”; break; case ‘C’: cout << “Grade is between 70 & 79”; break; case ‘D’: cout << “Grade is between 60 & 69”; break; case ‘E’: cout << “Grade is between 0 & 59”; break; default: cout << “You entered an invalid grade.”; }

break The break Statement switch (let_grd) { case ‘A’: case ‘B’:cout << “Good Work”; break; case ‘C’:cout << “Average Work”; break; case ‘D’: case ‘E’:cout << “Poor Work”; }

break The break Statement switch (let_grd) { case ‘A’: case ‘a’: case ‘B’: case ‘b’:cout << “Good Work”; break; case ‘C’: case ‘c’:cout << “Average Work”; break; etc.

switch The switch Statement * * * * Menu * * * * 1. NY Yankees 2. Orioles 3. Dodgers Choose either 1, 2, or 3:

switch The switch Statement switch (choice) { case 1: cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } What will be the output?

switch The switch Statement What will be the output when the user enters 1 World ChampsGood GuysDa Bums 2 Good GuysDa Bums 3 Da Bums * * * * 4 skips the switch

switch The switch Statement * switch (choice) { case 1: cout << “World Champs”; case 2: cout << “Good Guys”; case 3: cout << “Da Bums”; } switch (choice) { case 1: cout << “World Champs”; break; case 2: cout << “Good Guys”; break; case 3: cout << “Da Bums”; break; default: cout << “Enter a 1, 2, or 3”; }

Common Errors Using = in place of == Improper braces in nested ifs Too deeply nested ifs Missing break statements in switch the statement Copyright © 1999 by Freedom TLC, Inc.

Debugging Syntax errors vs. Logic error Prevention - plan first! Valuation tables Display values C++ Debugger Copyright © 1999 by Freedom TLC, Inc.

“I discovered I always have choices, and sometimes it’s only a choice of attitude” Judith M. Knowlton Copyright © 1999 by Freedom TLC, Inc.