Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general.

Slides:



Advertisements
Similar presentations
Copyright © 2003 Pearson Education, Inc. Slide 1.
Advertisements

Copyright © 2002 Pearson Education, Inc. Slide 1.
Chapter 2 Flow of Control. Copyright © 2006 Pearson Addison-Wesley. All rights reserved. 2-2 Learning Objectives Boolean Expressions Building, Evaluating.
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Chapter 7 More Flow of Control. Copyright © 2005 Pearson Addison-Wesley. All rights reserved. Slide 2 Overview Introduction Using Boolean Expressions.
1 Lecture 8:Control Structures I (Selection) (cont.) Introduction to Computer Science Spring 2006.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
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)
true (any other value but zero) false (zero) expression Statement 2
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
CSC 200 Lecture 4 Matt Kayala 1/30/06. Learning Objectives Boolean Expressions –Building, Evaluating & Precedence Rules Branching Mechanisms –if-else.
Multi-alternative Selection Both the if clause and the else clause of an if...else statement can contain any kind of statement, including another selection.
Copyright © 2008 Pearson Addison-Wesley. All rights reserved. Chapter 3 More Flow of Control.
Slides prepared by Rose Williams, Binghamton University Chapter 3 Flow of Control if-else and switch statements.
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.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 3 More Flow of Control.
CHAPTER 3 CONTROL STRUCTURES ( SELECTION ) I NTRODUCTION T O C OMPUTER P ROGRAMMING (CSC425)
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)
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Fundamental Programming Fundamental Programming More on Selection.
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
Chapter 3 More Flow Of Control.
Flow of Control Part 1: Selection
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Chapter 2 Flow of Control. Learning Objectives Boolean Expressions – Building, Evaluating & Precedence Rules Branching Mechanisms – if-else – switch –
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
Branches and Program Design
Conditional Statement Chapter 8. Conditional Statements Are statements that check an expression then may or may not execute a statement or group of statement.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
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.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
TK 1914 : C++ Programming Control Structures I (Selection)
Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 3 More Flow of Control.
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.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 CS161 Introduction to Computer Science Topic #8.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Copyright © 2015 Pearson Education, Ltd.. All rights reserved. Chapter 3 More Flow of Control.
Week 4 Program Control Structure
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Chapter 4, cont: Control Structures if else nested if switch.
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.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
A First Book of C++ Chapter 4 Selection.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Chapter 3 Selection Statements
Chapter 3 More Flow of Control. Chapter 3 More Flow of Control.
More on the Selection Structure
EGR 2261 Unit 4 Control Structures I: Selection
Flow of Control.
Chapter 3 Even More Flow of Control 1
Chapter 4: Control Structures I (Selection)
Chapter 3 Even More Flow of Control 1
Chapter 7 Conditional Statements
Chapter 4: Control Structures I (Selection)
CSS 161: Fundamentals of Computing
Presentation transcript:

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. CIS_IS20_CSLO 1. Explain computer programming concepts CSLO1.6. Explain the purpose of general functionality provided by conditional statements/control structures, relational operators, and logical operators

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Multiway Branches

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Multiway Branches A branching mechanism selects one out of a number of alternative actions The if-else-statement is a branching mechanism Branching mechanisms can be a subpart of another branching mechanism An if-else-statement can include another if-else-statement as a subpart Slide 3- 4

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Nested Statements A statement that is a subpart of another statement is a nested statement When writing nested statements it is normal to indent each level of nesting Example: if (count < 10) if ( x < y) cout << x << " is less than " << y; else cout << y << " is less than " << x; Slide 3- 5 indented Display 3.3

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Nested if-else Statements Use care in nesting if-else-statements Example: To design an if-else statement to warn a driver when fuel is low, but tells the driver to bypass pit stops if the fuel is close to full. Other wise there should be no output. Pseudocode: if fuel gauge is below ¾ then: if fuel gauge is below ¼ then: issue a warning otherwise (gauge > ¾) then: output a statement saying don't stop Slide 3- 6

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. First Try Nested if's Translating the previous pseudocode to C++ could yield (if we are not careful) if (fuel_gauge_reading < 0.75) if (fuel_gauge_reading < 0.25) cout << "Fuel very low. Caution!\n"; else cout << "Fuel over 3/4. Don't stop now!\n"; This would compile and run, but does not produce the desired results The compiler pairs the "else" with the nearest previous "if" Slide 3- 7

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Braces and Nested Statements Braces in nested statements are like parenthesis in arithmetic expressions Braces tell the compiler how to group things Use braces around substatements demonstrates the use of braces in nested if-else-statements Slide 3- 8 Display 3.4

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Multi-way if-else-statements An if-else-statement is a two-way branch Three or four (or more) way branches can be designed using nested if-else-statements Example: The number guessing game with the number stored in variable number, the guess in variable guess. How do we give hints? Slide 3- 9

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Number Guessing The following nested statements implement the hints for our number guessing game if (guess> number) cout << "Too high."; else if (guess < number) cout << "Too low."); else if (guess == number) cout << "Correct!"; Slide 3- 10

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Indenting Nested if-else Notice how the code on the previous slide crept across the page leaving less and less space Use this alternative for indenting several nested if-else-statements: if (guess> number) cout << "Too high."; else if (guess < number) cout << "Too low."); else if (guess == number) cout << "Correct!"; Slide 3- 11

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The Final if-else-statement When the conditions tested in an if-else-statement are mutually exclusive, the final if-else can sometimes be omitted. The previous example can be written as if (guess> number) cout << "Too high."; else if (guess < number) cout << "Too low."); else // (guess == number) cout << "Correct!"; Slide 3- 12

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Nested if-else Syntax A Multiway if-else statement is written as if(Boolean_Expression_1) Statement_1 else if ( Boolean_Expression_2) Statement_2 … else if (Boolean_Expression_n) Statement _n else Statement_For_All_Other_Possibilities Slide 3- 13

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Program Example: State Income Tax Write a program for a state that computes tax according to the rate schedule: No tax on first $15,000 of income 5% tax on each dollar from $15,001 to $25,000 10% tax on each dollar over $25,000 Slide Display 3.5 (1) Display 3.5 (2)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Refining if-else-statements Notice that the line else if (( net_income > && net_income < = 25000)) can be replaced with else if (net_income <= 25000) The computer will not get to this line unless it is already determined that net_income > Slide 3- 15

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The switch-statement The switch-statement is an alternative for constructing multi-way branches The example in Display 3.6 determines output based on a letter grade Grades 'A', 'B', and 'C' each have a branch Grades 'D' and 'F' use the same branch If an invalid grade is entered, a default branch is used Slide Display 3.6 (1) Display 3.6 (2)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. switch Structures switch structure: alternate to if-else switch (integral) expression is evaluated first Value of the expression determines which corresponding action is taken Expression is sometimes called the selector 17

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 18 switch Structures (cont’d.)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. switch Structures (cont’d.) One or more statements may follow a case label Braces are not needed to turn multiple statements into a single compound statement When a case value is matched, all statements after it execute until a break is encountered The break statement may or may not appear after each statement switch, case, break, and default are reserved words 19

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. 20 switch Structures (cont’d.)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. switch-statement Syntax switch (controlling expression) { case Constant_1: statement_Sequence_1 break; case Constant_2: Statement_Sequence_2 break;... case Constant_n: Statement_Sequence_n break; default: Default_Statement_Sequence } Slide 3- 21

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The Controlling Statement A switch statement's controlling statement must return one of these types A bool value An enum constant An integer type A character The value returned is compared to the constant values after each "case" When a match is found, the code for that case is used Slide 3- 22

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The break Statement The break statement ends the switch-statement Omitting the break statement will cause the code for the next case to be executed! Omitting a break statement allows the use of multiple case labels for a section of code case 'A': case 'a': cout << "Excellent."; break; Runs the same code for either 'A' or 'a' Slide 3- 23

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. The default Statement If no case label has a constant that matches the controlling expression, the statements following the default label are executed If there is no default label, nothing happens when the switch statement is executed It is a good idea to include a default section Slide 3- 24

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Switch-statements and Menus Nested if-else statements are more versatile than a switch statement Switch-statements can make some code more clear A menu is a natural application for a switch- statement Slide Display 3.7 (1) Display 3.7 (2)

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Chapter 3 -- End Slide 3- 26

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.3 Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.4 Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.5 (1/2) Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.5 (2/2) Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.6 (1/2) Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.6 (2/2) Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.7 (1/2) Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.7 (2/2) Slide Back Next

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.8 (1/2) Slide Next Back

Copyright © 2012 Pearson Addison-Wesley. All rights reserved. Display 3.8 (2/2) Slide Back Next