Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Similar documents
DECISION CONTROL AND LOOPING STATEMENTS

CHAPTER : 9 FLOW OF CONTROL

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

LAB 4.1 Relational Operators and the if Statement

Relational & Logical Operators, if and switch Statements

Programming Language. Functions. Eng. Anis Nazer First Semester

INTRODUCTION TO COMPUTER SCIENCE - LAB

5. Selection: If and Switch Controls

C C C C++ 2 ( ) C C++ 4 C C

Logical Operators and if/else statement. If Statement. If/Else (4.3)

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Add Subtract Multiply Divide

Decision Making in C

Recap. ANSI C Reserved Words C++ Multimedia Programming Lecture 2. Erwin M. Bakker Joachim Rijsdam

CMSC 104 -Lecture 11 John Y. Park, adapted by C Grasso

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

1/22/2017. Chapter 2. Functions and Control Structures. Calling Functions. Objectives. Defining Functions (continued) Defining Functions

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Following is the general form of a typical decision making structure found in most of the programming languages:

I SEMESTER EXAM : : XI :COMPUTER SCIENCE : MAX MARK a) What is the difference between Hardware and Software? Give one example for each.

Other conditional and loop constructs. Fundamentals of Computer Science Keith Vertanen

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

EECS402 Lecture 24. Something New (Sort Of) Intro To Function Pointers

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

C++ PROGRAMMING SKILLS Part 2 Programming Structures

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Control Structures in Java if-else and switch

Score score < score < score < 65 Score < 50

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Name Section: M/W T/TH Number Definition Matching (8 Points)

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Enumerated Types. CSE 114, Computer Science 1 Stony Brook University

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Programming - 1. Computer Science Department 011COMP-3 لغة البرمجة 1 لطالب كلية الحاسب اآللي ونظم المعلومات 011 عال- 3

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

Chapter 11: Structured Data

Decisions. Arizona State University 1

Armstrong State University Engineering Studies MATLAB Marina Switch-Case Statements Primer

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Lecture 5 Tao Wang 1

In this chapter you will learn:

More Complex Versions of the if Statement. Class 13

CHAPTER 9 FLOW OF CONTROL

C++ Programming Lecture 7 Control Structure I (Repetition) Part I

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

Decision Making -Branching. Class Incharge: S. Sasirekha

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

conditional statements

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

LECTURE 11 STRUCTURED DATA

Chapter 11: Structured Data

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

Shell Script Programs with If Then Else Statements

CS349/SE382 A1 C Programming Tutorial

Chapter 11: Abstract Data Types. Abstraction and Data Types. Combining Data into Structures 8/23/2014. Abstract Data Types

Laboratory 4. INSTRUCTIONS (part II) I. THEORETICAL BACKGROUND

Name Section: M/W T/TH Number Definition Matching (6 Points)

Basic Syntax - First Program 1

Introduction to Programming

Introduction to Programming

Chapter 5 Style Requirements

Lecture 12. Data Types and Strings

Cisdem AppCrypt Tutorial

SFU CMPT Topic: Control Statements

Pseudo-code vs. Assembly. Introduction to High-Level Language Programming. Disadvantages of Assembly. Pseudo-code vs. High-level Programs

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Computer Programming

In this lab, you will learn more about selection statements. You will get familiar to

CS 170 Section 3, Spring 2015 Programming in Java Midterm Exam 1. Name (print):

Chapter 4: Making Decisions

BEng (Hons) Electronic Engineering. Resit Examinations for / Semester 1

Chapter 4: Making Decisions

Pseudocode. ARITHMETIC OPERATORS: In pseudocode arithmetic operators are used to perform arithmetic operations. These operators are listed below:

CPE 112 Spring 2015 Exam II (100 pts) March 4, Definition Matching (8 Points)

Lecture 3 Tao Wang 1

Cloning Enums. Cloning and Enums BIU OOP

Control Structures in Java if-else and switch

Computers and FORTRAN Language Fortran 95/2003. Dr. Isaac Gang Tuesday March 1, 2011 Lecture 3 notes. Topics:

CHAPTER 4 CONTROL STRUCTURES

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Unit 7. 'while' Loops

Agenda: Discussion Week 7. May 11, 2009

CSE115 / CSE503 Introduction to Computer Science I Dr. Carl Alphonce 343 Davis Hall Office hours:

REPETITION CONTROL STRUCTURE LOGO

LN #2 (3 Hrs) Variables, Sequence Boolean Logic & Selection CTPS Department of CSE,Coimbatore

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

10/9/2012. Comparison and Logical Operators The if Statement The if else Statement Nested if Statements The switch case. switch case Statement

AQA Decision 1 Algorithms. Section 1: Communicating an algorithm

DHA Suffa University CS 103 Object Oriented Programming Fall 2015 Lab #01: Introduction to C++

View a Students Schedule Through Student Services Trigger:

Computer Science II Lecture 1 Introduction and Background

Transcription:

Programming Language Control Structures: Selection (switch) Eng. Anis Nazer First Semester 2018-2019

Multiple selection choose one of two things if/else choose one from many things multiple selection using nested if/else switch switch is another structure for multiple selection

Multiple selection expression multiple selection can be performed using a switch statement what is the difference between switch and nested if/else? expr == v1 false expr == v2 false true true Statement(s) (1) Statement(s) (2) expr == vn true Statement(s) (n) false Statement(s) (n+1)

switch switch ( expression ) { case v1 : Syntax: Statements 1 break ; } case v2 : Statements 2 break ;... case vn : Statements n break ; default : Statements n+1

Write a program that reads a number that represents a day and displays the name of the day : 1 Sunday 2 Monday 7 Saturday otherwise invalid day number

cin >> day; switch ( day ) { case 1 : cout << "Sunday"; case 2 : cout << "Monday"; case 3 : cout << "Tuesday"; case 4 : cout << "Wednesday"; case 5 : cout << "Thursday"; case 6 : cout << "Friday"; case 7 : cout << "Saturday"; default : cout << "Invalid day number"; }

switch Notes: switch, case, default are reserved keywords the value of the expression is compared to the values in the cases, execution jumps to the matching case after the jump execution continues from there sequentially if no matching case exists, execution jumps to the default case

switch More notes: break statement ends the switch statement, execution continues after the switch break statement is not part of the syntax, can be removed... so why use it? default case is optional, you can write a switch without a default case again, why use it? the cases can be ordered as you wish Note the colon : after the value in the cases also note that the statements may be written without braces

We can combie similar cases together by writing an empty case. Example: Write a program that reads a number that represents a day and displays whether the day is a work day or a weekend : 1,2, 3, 4, 5 Workday 6, 7 Weekend otherwise invalid day number

cin >> day; switch ( day ) { case 1 : case 2 : case 3 : case 4 : case 5 : cout << "Workday"; case 6 : case 7 : cout << "Weekend"; default : cout << "Invalid day number"; }

Write a program that reads a letter grade and displays a message as follows: grade equals 'A' grade equals 'B' grade equals 'C' grade equals 'D' grade equals 'F' otherwise excellent very good good fair fail invalid input

cin >> grade; switch ( grade ) { case 'A' : cout << "Excellent"; case 'B' : cout << "Very Good"; case 'C' : cout << "Good"; case 'D' : cout << "Fair"; case 'F' : cout << "Fail"; default : cout << "Invalid grade"; }

- improved Our last example works for capital letter grades, What happens if the user enters a small letter grade. if the user enters the grade a, the program outputs an invalid grade error How to fix this? We can write cases that perform the same thing using an empty case without a break

cin >> grade; switch ( grade ) { case 'a' : case 'A' : cout << "Excellent"; case 'b' : case 'B' : cout << "Very Good"; case 'c' : case 'C' : cout << "Good"; case 'd' : case 'D' : cout << "Fair"; case 'f' : case 'F' : cout << "Fail"; default : cout << "Invalid grade"; }

Write a program that reads two numbers and reads a character that represents an arithmetic operation ( +, -, /, * ). The program displays the result of the operation. for example: Output Window Enter two numbers, please: 21.2 5.9 Enter the operation: * 21.2 * 5.9 = 125.08

What does the following program do? cin >> N; switch ( N%2 ) { case 0 : cout << N << " is even"; case 1 : cout << N << " is odd"; }

What does the following program do? int n = 13; switch ( n % 9 ) { case 1: cout << n*3 << endl; case 2: cout << n+3 << endl; case 3: cout << n 3 << endl; case 4: cout << n/3 << endl; default: cout << n << endl; }

What does the following program do? int x=1, y = 9; switch(y%4) { case 3: x += 10; case 0: case 1: x += 5; case 2: x += 3; } cout << x;

Rewrite the following if/else using a switch statement int val; cin >> val; if ( val >= 5 && val < 8 ) cout << "AAA" << endl; else if ( val > 2 && val < 5 ) cout << "BBB" << endl; else cout << "CCC" << endl;

Questions questions? anyone?