Decision Structures Case Structures.

Slides:



Advertisements
Similar presentations
Basic Control Structures Control order of execution of statements sequential selection iteration - Repeat some action while a certain condition is true.
Advertisements

If Statements & Relational Operators Programming.
Conditional Operator (?:) Conditional operator (?:) takes three arguments (ternary) Syntax for using the conditional operator:
Computer Science 1620 Variables and Memory. Review Examples: write a program that calculates and displays the average of the numbers 45, 69, and 106.
Programming Switch command. COMP102 Prog. Fundamentals: Switch command / Slide 2 Multiple Selection: The switch Statement value1 action 1 value2 action.
1 10/20/08CS150 Introduction to Computer Science 1 do/while and Nested Loops Section 5.5 & 5.11.
CS150 Introduction to Computer Science 1
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Chapter 5: Control Structures II (Repetition)
Programming in C++ Lecture Notes 2 – Choice Statements Andreas Savva.
EGR 2261 Unit 5 Control Structures II: Repetition  Read Malik, Chapter 5.  Homework #5 and Lab #5 due next week.  Quiz next week.
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
CSE 332: C++ execution control statements Overview of C++ Execution Control Expressions vs. statements Arithmetic operators and expressions * / % + - Relational.
Lecture 4 Looping. Building on the foundation Now that we know a little about  cout  cin  math operators  boolean operators  making decisions using.
1 09/20/04CS150 Introduction to Computer Science 1 Let ’ s all Repeat Together.
CS Class 05 Topics  Selection: switch statement Announcements  Read pages 74-83, ,
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Refer to Ivor Horton’s Beginning ANSI C++ The Complete Language, 3rd Ed. APress Media, LLC. Choices and Decisions if statement if-else statement Relational.
ENUMERATED DATATYPES. USER DEFINED DATA TYPES  Data Type Defined By Programmer  Allows Use Of More Complex Data  Typically Defined Globally So Variables.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CS Class 03 Topics  Sequence statements Input Output Assignment  Expressions Read pages Read pages 40 – 49 for next time.
Control Structures RepetitionorIterationorLooping Part I.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Multiplication Facts X 3 = 2. 8 x 4 = 3. 7 x 2 =
© The McGraw-Hill Companies, 2006 Chapter 3 Iteration.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
1 9/26/05CS150 Introduction to Computer Science 1 Life is Full of Alternatives.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
Loop Design What goes into coding a loop. Considerations for Loop Design ● There are basically two kinds of loops: ● Those that form some accumulated.
Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.
Today in CS161 Lecture #7 Learn about… If and else statements Rewrite our First Program Using if and else statements Create new Graphics Demos Using if.
Example 21 #include<iostream.h> int main() { char Letter = 0;
The Ohio State University
Review 1.
REPETITION CONTROL STRUCTURE
CHAPTER 4 REPETITION CONTROL STRUCTURE / LOOPING
Chapter 5: Control Structures II (Repetition)
Exercise 1 – Datentypen & Variablen
Selection (also known as Branching) Jumail Bin Taliba by
CS161 Introduction to Computer Science
A mechanism for deciding whether an action should be taken
Decisions Given hours worked and pay rate, calculate total pay
Control Structures II (Repetition)
Programming Fundamentals
Control Structures - Repetition
Chapter 8 Repetition Statements
Decisions Given hours worked and pay rate, calculate total pay
Compound Assignment Operators in C++
Flow of Control October 16, 2017.
Additional Control Structures
Counting Loops.
Control Structures: Selection Statement
Coding Concepts (Basics)
Objectives Solve systems of linear equations in two variables by elimination. Compare and choose an appropriate method for solving systems of linear equations.
Notes Solving a System by Elimination
Control Structures Part 1
Fundamental Programming
Decisions, decisions, decisions
Control Structures: Selection Statement
Solving Systems by Elimination
Strings …again.
Bell Work Solve for “x” and check your solution
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Life is Full of Alternatives Part 3
Presentation transcript:

Decision Structures Case Structures

Decisions As we saw before, our program may need to make decisions based on the users input. But what happens when there is more than 2 or 3 options. In this case the if structure becomes onorous.

Case Structure The solution to this problem is called the case structure. It allows us to consider many options at the same time. The down side is this structure only allows us to work with integers ( or things that can be represented with integers).

Syntax switch (_variable name_) { case _option1_: instructions; break; default: }

Syntax cout << "Pick a number between 4 and 6 inclusive" << endl; cin >> number; switch (number) { case 4: cout << "Four is a terrible number" << endl; cout << " And neither should you" << endl; break; case 5: cout << "Five is an appropriate choice" << endl; case 6: cout << "Six... seriously?" << endl; default: cout << "That is not between 4 and 6" << endl; }

The drawback is that it works only with integers. There are, however, ways to circumvent this problem using what are called “Structures” which we will discuss at a later time.

Exercise Convert your quiz program so that it uses multiple choice questions and the CASE structure to check the validity of the answers. When you are done, explore the “while” command