Intro to Programming Week # 4 Switch Statement Lecture # 7

Slides:



Advertisements
Similar presentations
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Advertisements

Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
PRINCIPLES OF PROGRAMMING Revision. A Computer  A useful tool for solving a great variety of problems.  To make a computer do anything (i.e. solve.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 1: Selection statements: if, if…else, switch.
Selection Statements in C++ If Statement in C++ Semantics: These statements have the same meaning as in the algorithmic language. 2- Two way selection:
Lecture 5 Selection Control Structures Selection Control Structures Dr. Hebbat Allah A. Elwishy Computer & IS Assistant Professor
Lecture 4 Introduction to Programming. if ( grade ==‘A’ ) cout
Computer Science Department Relational Operators And Decisions.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
LECTURE 11 TYPES OF USER DEFINE FUNCTIONS ITC-414.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
CS102 Introduction to Computer Programming Chapter 4 Making Decisions.
Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Previously Repetition Structures While, Do-While, For.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
Switch Statements Comparing Exact Values. The Switch Statement: Syntax The switch statement provides another way to decide which statement to execute.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
Copyright 2003 Scott/Jones Publishing Making Decisions.
Control structures Algorithm Development Conditional Expressions Selection Statements 1.
CHAPTER#3 PART1 STRUCTURED PROGRAM DEVELOPMENT IN C++ 2 nd semester King Saud University College of Applied studies and Community Service Csc.
1 Programming 2 Overview of Programming 1. Write the equations in C++ notation : a) R =   a + b  24  2 a  b) W = a 12 + b 2 – 2abcos(y) 2a.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
CHAPTER 5 MAKING DECISION Hidayah Elias BFC2042 – Computer Programming.
Chapter#3 Part1 Structured Program Development in C++
1 CS161 Introduction to Computer Science Topic #8.
Statements
We have to discuss following:-  Statements  Statement flow control  Selection statement  Iteration statement.
LESSON 4 Decision Control Structure. Decision Control Structures 1. if statement 2. if-else statement 3. If-else-if statement 4. nested if statement 5.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Lecture 7 Computer Programming -1-. Conditional Statements 1- if Statement. 2- if ….. else Statement. 3- switch.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
FLOW OF CONTROL In C++ the program execution starts with the first statement in the main() and ends with the last statement of main(). This is called.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Switch Statements Comparing Exact Values
BY ILTAF MEHDI(MCS,MCSE, CCNA) 1. INSTRUCTOR: ILTAF MEHDI (MCS, MCSE, CCNA, Web Developer) BY ILTAF MEHDI(MCS,MCSE, CCNA) 2 Programming Fundamentals Chapter.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 23, 2005 Lecture Number: 11.
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
LESSON 4 Decision Control Structure
Decisions Chapter 4.
Chapter 4: Making Decisions.
EKT150 INTRODUCTION TO COMPUTER PROGRAMMING
Relational Operators A relational operator compares two values. The values can be any built-in C++ data type, such as Character Integer Floating point.
Programming Fundamentals
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Chapter 4: Making Decisions.
Chapter 4: Making Decisions.
Intro to Programming Week # 5 Repetition Structure Lecture # 9
Intro to Programming Week # 3 If-else Statement Lecture # 6
Compound Assignment Operators in C++
Introduction to Programming
CS-161 Computer Programming Lecture 14: Arrays I
Chapter#3 Structured Program Development in C++
Chapter 7 Conditional Statements
Intro to Programming Week # 2 Variables/Data type Lecture # 4 & 5
© Copyright 2016 by Pearson Education, Inc. All Rights Reserved.
CprE 185: Intro to Problem Solving (using C)
CPS125 Week 5.
Structured Program Development in C++
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)
Selection Control Structure
CprE 185: Intro to Problem Solving (using C)
Programming Fundamental
Switch Case Structures
Programming Fundamental-1
Presentation transcript:

Intro to Programming Week # 4 Switch Statement Lecture # 7 Department of Computer Science & Engineering Air University Intro to Programming Week # 4 Switch Statement Lecture # 7 By: Saqib Rasheed

CS 161 Intro To Programming Quiz Time [20 Mins] Marks[10] Write a program to check whether a triangle is valid or not, when the three angles of the triangle are entered through the keyboard. A triangle is valid if the sum of all the three Angles is equal to 180 degrees CS 161 Intro To Programming

CS 161 Intro To Programming switch statement The control statement that allows us to make a decision from the number of choices is called a switch statement CS 161 Intro To Programming

CS 161 Intro To Programming switch statement switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; … } CS 161 Intro To Programming

CS 161 Intro To Programming switch statement switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … … } CS 161 Intro To Programming

CS 161 Intro To Programming switch statement case ‘A’ : cout << “ Excellent ” ; … … CS 161 Intro To Programming

CS 161 Intro To Programming Example switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : cout << “Good ” ; case ‘D’ : cout << “ Poor ” ; case ‘F’ : cout << “ Fail ” ; } CS 161 Intro To Programming

CS 161 Intro To Programming break; CS 161 Intro To Programming

CS 161 Intro To Programming Example char grade; cin>>grade; switch ( grade ) { case ‘A’ : cout << “ Excellent ” ; break ; case ‘B’ : cout << “ Very Good ” ; break ; case ‘C’ : cout << “Good ” ; break ; case ‘D’ : cout << “ Poor ” ; break ; case ‘F’ : cout << “ Fail ” ; } CS 161 Intro To Programming

CS 161 Intro To Programming default : default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ; CS 161 Intro To Programming

Flow Chart of switch statement switch (grade) case ‘A’ : Display “Excellent” case ‘B’ : Display “Very Good” … Default : “……..” CS 161 Intro To Programming

CS 161 Intro To Programming Example int grade; cin>>grade; switch ( grade ) { case 1 : cout << “ Excellent ” ; break ; case 2 : cout << “ Very Good ” ; break ; case 3 : cout << “Good ” ; break ; case 4 : cout << “ Poor ” ; break ; case 5 : cout << “ Fail ” ; } CS 161 Intro To Programming

CS 161 Intro To Programming Example # 1 int i = 2 ; switch ( i ) { case 1 : cout<<"I am in case 1 \n"; break; case 2 : cout<<"I am in case 2 \n"; break; case 3 : cout<<"I am in case 3 \n"; break; default : cout<<"I am in default \n"; } Output I am in case 2 CS 161 Intro To Programming

CS 161 Intro To Programming if-else Switch switch (x) { case 1: cout << "x is 1"; break; case 2: cout << "x is 2"; default: cout << "value of x unknown"; } if (x == 1) { cout << "x is 1"; } else if (x == 2) cout << "x is 2"; else cout << "value of x unknown"; CS 161 Intro To Programming

switch Versus if-else Ladder There are some things that you simply cannot do with a switch. These are: A float expression cannot be tested using a switch Cases can never have variable expressions (for example it is wrong to say case a +3 : Multiple cases cannot use same expressions. Thus the following switch is illegal: switch ( a ) { case 3.2 : case 4: case 1 + 2 : case 4: : CS 161 Intro To Programming

CS 161 Intro To Programming Example # 2 int day; cout<<"Eneter Day Number"; cin>>day; switch (day) { case 1 : cout << "\nSunday"; break; case 2 : cout << "\nMonday"; case 3 : cout << "\nTuesday"; case 4 : cout << "\nWednesday"; case 5 : cout << "\nThursday"; case 6 : cout << "\nFriday"; case 7 : cout << "\nSaturday"; default : cout << "\nNot an allowable day number"; } CS 161 Intro To Programming

CS 161 Intro To Programming switch (day) { case 1 : case 7 : cout << "This is a weekend day"; break; case 2 : case 3 : case 4 : case 5 : case 6 : cout << "This is a weekday"; default : cout << "Not a legal day"; break; } Break 1 Break 2 CS 161 Intro To Programming

CS 161 Intro To Programming Example # 3 char ch ; cout<< "Enter any of the alphabet a, b, or c "; cin>>ch; switch ( ch ) { case 'a' : case 'A' : cout<<"a is an apple" ; break ; case 'b' : case 'B' : cout<<"b as in brain"; break ; case 'c' : case 'C' : cout<<"c as in cookie"; break ; default : cout<<"wish you knew what are alphabets" ; } CS 161 Intro To Programming

Is switch a replacement for if? Yes and no. Yes, because it offers a better way of writing programs as compared to if, and no because in certain situations we are left with no choice but to use if. The disadvantage of switch is that one cannot have a case in a switch which looks like: case i <= 20 : CS 161 Intro To Programming

switch works faster than an equivalent if-else ladder. compiler generates a jump table for a switch during compilation during execution it simply refers the jump table to decide which case should be executed . if-elses are slower because they are evaluated at execution time CS 161 Intro To Programming

CS 161 Intro To Programming Question # 1 Make a calculator using switch statement It should take two numbers from user. Ask the user the function to perform Display the result CS 161 Intro To Programming

CS 161 Intro To Programming Question # 2 Que Write a menu driven program program in C++ using switch statement that contain option as under.( The Menu should ) Enetr 1--> To Find Largest Number Among Three Variables. Enetr 2--> To Find ODD or EVEN Enetr 3--> To Find Condition of Water Enetr 4--> To Find Grade Of Student CS 161 Intro To Programming

CS 161 Intro To Programming Question # 2 (Code) #include<iostream.h> Void main () { int option; cout<<"\n\t\tEnter the option\n"; cout<<"Enetr 1--> To Find Largest Number Among Three Variables.\n"; cout<<"Enetr 2--> To Find ODD or EVEN\n"; cout<<"Enetr 3--> To Find Condition of Water\n"; cout<<"Enetr 4--> To Find Grade Of Student\n"; cin>>option; CS 161 Intro To Programming

CS 161 Intro To Programming Question # 2 (Code) switch (option) { case 1: int a,b,c, larg; cout<<"Enter First Integer="; cin>>a; cout<<"Enter Second Integer="; cin>>b; cout<<"Enter Third Integer="; cin>>c; CS 161 Intro To Programming

CS 161 Intro To Programming Question # 2 (Code) if (a > b) larg = a; else larg = b; if (larg > c ) cout<<"Largest is ="<<larg<<endl; cout<<"Largest is ="<<c<<endl; break; CS 161 Intro To Programming

CS 161 Intro To Programming Question # 2 (Code) case 2: int value; cout<<"Enter an Interger value "; cin>>value; if (value % 2 == 0) cout<<"Your number is EVEN\n"; else cout<<"Your number is ODD\n"; break; CS 161 Intro To Programming

CS 161 Intro To Programming case 3: int t; cout<<"Temperature Less than 0 = ICE \n" <<"Temperature Greater than 0 & " <<"Temperature Less than 100 = Water\n" <<"Temperature Greater than 100 = STEAM\n"; cout<<"\n\n\t\tPlease enter the Temperature="; cin>>t; if ( t <= 0 ) cout<<"Form of water is \"ICE\""<<endl; else if( t > 0 && t < 100 ) cout<<"Form is \"WATER\"\n"; else if ( t >= 100 ) cout<<"Form of water is \"steam\"\n“; break; CS 161 Intro To Programming

CS 161 Intro To Programming case 4: int grade; cout<<"Enter the grade of student="; cin>>grade; if ( grade >= 90 ) cout<< " Grade A \n"; else if (grade >= 80 ) cout<<" Grade B \n"; else if ( grade >=70 ) cout<<" Grade C \n"; else if (grade >=60) cout<<" Grade D \n"; CS 161 Intro To Programming

CS 161 Intro To Programming else { cout<<" Grade F \n"; cout<<" You have to take the classes again\n"; cout<<" Work Hard To Get Good Grade\n"; } break; default: cout<<"You Entered Invalid Option\n"; cout<<"Enetr A Valid Option\n"; } //End Of Switch } //End Of Main CS 161 Intro To Programming