Selection The Switch Statement 2/16/11. grade = 'P'; switch (grade){ case 'A': cout << "Excellent.\n"; break; case 'P': cout << "Pass.\n"; break; case.

Slides:



Advertisements
Similar presentations
Control Structures.
Advertisements

STRING AN EXAMPLE OF REFERENCE DATA TYPE. 2 Primitive Data Types  The eight Java primitive data types are:  byte  short  int  long  float  double.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Discussion1 Quiz. Q1 Which of the following are invalid Java identifiers (variable names)? a) if b) n4m3 c) Java d) e) DEFAULT_VALUE f) bad-choice.
5/17/ Programming Constructs... There are several types of programming constructs in JAVA. - If-else construct or ternary operator - while - do-while.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
(c) 2003 E.S.Boese1 Conditionals Chapter 10 - Student.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
The If/Else Statement, Boolean Flags, and Menus Page 180
Chapter 4 Making Decisions
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
Switch Statements Comparing Exact Values. 2 The Switch Statement The switch statement provides another way to decide which statement to execute next The.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Other Conditional Methods. Conditional Operator Conditional Operator (?:) Conditional operator ( ?: ) – Ternary operator: 3 arguments expression1 ? expression2.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
Chapter 4 Making Decision Csc 125 C++ programming language Fall 2005.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Copyright © 2010 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Chapter 4: Making Decisions.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Compound Statements If you want to do more than one statement if an if- else case, you can form a block of statements, or compound statement, by enclosing.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Chapter 5.  Conditionals  Booleans  Relational and Logical Operators  Switch statements.
Chapter Making Decisions 4. Relational Operators 4.1.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Copyright 2003 Scott/Jones Publishing Standard Version of Starting Out with C++, 4th Edition Chapter 4 Making Decisions.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
A: A: double “4” A: “34” 4.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Switch-Case Statement. What is a switch-case?  The switch-case statement is really nothing more than a glorified.
 Very often when you write code, you want to perform different actions for different decisions. You can use conditional statements in your code to do.
משפטי תנאי ( לוגיקה ) קרן כליף. 2 © Keren Kalif ביחידה זו נלמד :  משפטי תנאי  משפט switch  משפט if מקוצר.
Discussion 4 eecs 183 Hannah Westra.
If/Else Statements.
Chapter 3 Selection Statements
Midterm Review.
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.
Chapter 4: Making Decisions.
Programming Fundamentals
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Chapter 4: Control Structures I (Selection)
null, true, and false are also reserved.
Other Conditional Methods
Selection CSCE 121 J. Michael Moore.
Selection Control Structure: Switch Case Statement
Flow Control Statements
Topics 4.1 Relational Operators 4.2 The if Statement
If Statements.
Nate Brunelle Today: Conditional Decision Statements
PROGRAM FLOWCHART Selection Statements.
Fundamental Programming
Welcome back to Software Development!
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Presentation transcript:

Selection The Switch Statement 2/16/11

grade = 'P'; switch (grade){ case 'A': cout << "Excellent.\n"; break; case 'P': cout << "Pass.\n"; break; case 'F': cout << "Fail.\n"; break; default: cout << "Invalid.\n"; }

Switch Statement Selection based on a single variable or expression Controlling Expression – char or int – Not double or floating point or string Must have break – Falls through to next statement without it

Rewrite the if-else structure in ch3/factor.cpp so that it uses a switch instead of the if-else chain. Write a switch statement

Boolean Variables Type bool Keep track of whether a event occurred or condition has changed.

Boolean Variables Example bool validDay; if (1 <= day && day <= 31) validDay = true; else validDay = false; if (!validDay) cout << “Incorrect day input.\n” else cout << “Oct. “ << day << end;

Short Circuit Evaluation With an && or || operation, if the first condition determines the value of the operation the second condition isn't evaluated. weekend.cpp candy.cpp

What is the Output? int choice =1; switch (choice * 2 ){ case 1: cout << “Roast beef.\n”; break; case 2: cout << “Fried chicken.\n”; case 3: cout << “Mashed potatoes.\n” break; default: cout << “Bon appetit!\n”; }