COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Chapter 4: Making Decisions.
Flow Control if, while, do-while Juan Marquez (03_flow_control.ppt)
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computers and Programming Lecture 6 Professor: Evan Korth New York University.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
Control Structures Control structures control the flow of program execution. 3 types of control structures: sequence, selection.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
Today’s Lecture  Boolean Expressions  Building, Evaluating & Precedence Rules  Branching Mechanisms  if-else  switch  Nesting if-else  Loops  While,
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
Unit 3 Lesson 8 Selection Structures Mr. Dave Clausen La Cañada High School.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lecture 11: Control Flow. Selection (Decision) structures (cont)
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.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving,
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
CPS120: Introduction to Computer Science Decision Making in Programs.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
CHAPTER#3 STRUCTURED PROGRAM DEVELOPMENT IN C++ 1 st semester King Saud University College of Applied studies and Community Service Csc 1101.
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.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
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.
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.
Selection Structures: if and switch Statements. 2 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Week 4 Program Control Structure
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
CPS120: Introduction to Computer Science Decision Making in Programs.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
CSE202: Lecture 5The Ohio State University1 Selection Structures.
Chapter 4 Selection Structures: if and switch Statements Lecture Notes Prepared By: Blaise W. Liffick, PhD Department of Computer Science Millersville.
C++ Programming Control Structures I (Selection).
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.
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):
The Ohio State University
CNG 140 C Programming (Lecture set 3)
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
Chapter 7 Conditional Statements
Chapter 4 Selection.
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Chapter 4: Selection Structures: if and switch Statements
Presentation transcript:

COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot Koffman, Course lecturer: Assoc. Prof. Stoyan Bonev, PhD

Lecture 11: Control Flow. Selection (Decision) structures (cont)

3 Lecture Contents: t The switch statement t Demo programs t Exercises

4 4. Control Structures t Three methods of processing a program –In sequence –Branching –Looping t Branch: altering the flow of program execution by making a selection or choice t Loop: altering the flow of program execution by repetition of statement(s)

5 5. Flow of Execution

Reminder Single (one-way) selection Double (two-way) selection

7 The if statement (then version) Syntax and flowchart fragment: if ( ) ;

8 The if statement (then – else version) Syntax and flowchart fragment: if ( ) ; else ;

New Multiple selection

10 Multiple selection

11 The switch statement Syntax and flowchart fragment: The switch statement: If you have a large decision tree and all the decisions depend on different values of the same variable (control expression) you’ll probably want to consider a switch statement instead of a series of nested if…else if…else statements or alternate sequence of if statements.

12 The switch statement Syntax&flowchart fragment in general: switch ( ) { case : ; break;... case : ; break; default: ; break; }; Don’t forget the break statement: Without it control passes down to (or falls through) the statements for the next case which is usually not what you want

13 The switch statement What is ? It’s an expression that may be evaluated at compile time. In other words, all operands must be with fixed, defined values during the compilation process. See examples on next slide(s)

14 The switch statement What is ? t Expression whose operands are literal values: YES –5 + 6 * 8 –10 * (4 +16) – 5 –100 % / 5

15 The switch statement What is ? t Expression whose operands are constant ( const ) variables: YES – const int a=15;const int b=24; –5 + a * 8 –10 * (a + b) – 5 –100 % a + 20 / b

16 The switch statement What is ? t Expression whose operands are standard scalar variables: NO/YES –int a=15;int b=24; –5 + a * 8 –10 * (a + b) – 5 –100 % a + 20 / b

17 The switch statement What is ? t Expression whose operands are standard scalar variables: NO –int a;int b;cin >> a >> b; –5 + a * 8 –10 * (a + b) – 5 –100 % a + 20 / b

18 The switch statement Example: int speed; cout >speed; switch(speed) { case 33: cout<<”Long play album”; break; case 45: cout<<”Single selection”; break; case 78: cout<<”Obsolete format”; break; default: cout<<”Wrong value entered”; }; Reminder: Don’t forget the break statement: Without it control passes down to (or falls through) the statements for the next case which is usually not what you want

19 More on switch statement Extract from Friedman/Koffman, chapter 4

Selection Structures: if and switch Statements Chapter 4

21 Selection Statements –In this chapter we study statements that allow alternatives to straight sequential processing. In particular: if statements (do this only if a condition is true) if-else statements (do either this or that) Logical expressions (evaluate to true or false) Boolean operators (not: ! and: && or: ||) Bit-wise boolean operators (and: & or: |)

22 Logical Expressions –Logical expressions often use these relational operators:

23 Boolean Variables  boolean variable – basic data type bool bool leapYear, same; t boolean assignment Form:variable = expression; Example: leapYear = true; // Non zero return value leapYear = false; // Zero return value same = (x == y);

24 Truth Tables for Boolean Operators t Truth tables Logical operators !, ¦¦, && –1 is a symbol for true –0 is a symbol for false

25 Bit-wise logical operators t &- bit wise AND operator t |- bit wise OR operator

26 Precedence of Operators

The switch Control Statement switch ( switch-expression ) { case value-1 : statement(s)-1 break ;... // many cases are allowed case value-n : statement(s)-n break ; default : default-statement(s) }

28 Switch Control –When a switch statement is encountered, the switch-expression is evaluated. This value is compared to each case value until switch-expression == case value. All statements after the colon : are executed –It is important to include the break statement

29 Example switch Statement: switch(watts) // Assume char option = '?’ { case 25: cout << " Life expectancy is 2500 hours. " << endl; break; case 40: case 60: cout << " Life expectancy is 1000 hours. " << endl; break; case 75: case 100: cout << " Life expectancy is 750 hours. " << endl; break; default: cout << "Invalid Bulb !!" << endl; }; // end switch

30 Trace the previous switch –Show output when watts = '?'____________? watts = 40____________? watts = 10____________? watts = 200____________? watts = 100____________?

31 Before lecture end Lecture: Control Flow. Selection (decision) structures More to read: Friedman/Koffman, Chapter 04

Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Selection Structures: if and switch Statements Problem Solving, Abstraction, and Design using C++ 5e by Frank L. Friedman and Elliot B. Koffman

33 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4.8 The switch Control Statement switch ( switch-expression ) { case label 1 : statements 1 ; break; case label 2 : statements 2 ; break;. case label n : statements n ; break; default: statements d ; // optional }

34 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Switch Control Statement Alternative to multiple if statements in some cases Most useful when switch selector is single variable or simple expression Switch selector must be an integral type (int, char, bool)

35 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Switch Control Statement Switch selector value compared to each case label. When there is an exact match, statements for that case are executed. If no case label matches the selector, the entire switch body is skipped unless it contains a default case label. break is typically used between cases to avoid fall through.

36 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Listing 4.5 switch statement to determine life expectancy of a lightbulb

37 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley 4.9 Common Programming Errors Use parentheses to clarify complex expressions Use logical operators only with logical expressions Use braces { } to group multiple statements for use in control structures

38 Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Common Programming Errors When writing a nested if statement, use multiple-alternative form if possible –if conditions are not mutually exclusive, put the most restrictive condition first Make sure switch selector and case labels are the same type. Provide a default case for a switch statement whenever possible.

39 Exercise 11.1 Build programs based on branch algorithms using the switch statement:  To test an input integer value entered from the keyboard as a valid speed of a gramophone (obsolete record player) – 33, 45, 78;

40 Exercise 11.2 Build programs based on branch algorithms using the switch statement: t To test a character as a valid grade ‘A’ or ‘a’- excellent ‘B’ or ‘b’- good ‘C’ or ‘c’- average ‘D’ or ‘d’- poor. Student is to retake.

41 Exercise 11.3 Build programs based on branch algorithms using the switch statement: t To classify the type of a ship through its class identifier: ‘B’ or ‘b’- Battle ship ‘C’ or ‘c’- Cruiser ‘D’ or ‘d’- Destroyer ‘F’ or ‘f’- Frigate

42 Exercise 11.4 Build programs based on branch algorithms using the switch statement: t To associate the expected brightness of a standard light bulb depending on its power (wattage); WattsBrightness (in lumens)

43 Exercise 11.5 Build programs to test logical operators negation conjunction disjunction

44 Exercise 11.6 t Build programs to test the bit wise And (&) operator t Build programs to test the bit wise Or (|) operator

45 Homework 3 t Paper record to be presented before the end of next class t Tasks: –See.doc file.

46 Thank You For Your Attention!