The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.

Slides:



Advertisements
Similar presentations
Control Statements. Define the way of flow in which the program statements should take place. Control Statements Implement decisions and repetitions.
Advertisements

Fundamental of C programming
CSE 1301 Lecture 5B Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Briana B. Morrison.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
 2005 Pearson Education, Inc. All rights reserved Introduction.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Switch structure Switch structure selects one from several alternatives depending on the value of the controlling expression. The controlling expression.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
7. C program structure.
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
- SEARCHING - SORTING.  Given:  The array  The search target: the array element value we are looking for  Algorithm:  Start with the initial array.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Chapter 3 Selections Liang, Introduction to Java Programming, Seventh Edition, (c) 2009 Pearson Education, Inc. All rights reserved
© Copyright 1992–2005 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Tutorial 9 – Income Tax Calculator Application: Introducing.
Flow of Control Part 1: Selection
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
A First C Program /* Print a Message */ #include main() { printf("This is a test!\n"); } The program is compiled and run as cc pg32.c  a.out  This is.
Nested LOOPS.
Additional Control Structures. Chapter 9 Topics Switch Statement for Multi-way Branching Do-While Statement for Looping For Statement for Looping Using.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Lecture 2: Introduction to C Programming. OBJECTIVES In this lecture you will learn:  To use simple input and output statements.  The fundamental data.
Previously Repetition Structures While, Do-While, For.
Lecture 4: Calculating by Iterating. The while Repetition Statement Repetition structure Programmer specifies an action to be repeated while some condition.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
CSE 1301 Lecture 8 Conditionals & Boolean Expressions Figures from Lewis, “C# Software Solutions”, Addison Wesley Richard Gesick.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 6. else-if & switch Copyright © 2012 Pearson Education, Inc.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Dr. Soha S. Zaghloul2 Let arr be an array of 20 integers. Write a complete program that first fills the array with up to 20 input values. Then, the program.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
Copyright 2004 Scott/Jones Publishing Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
1 Programming in C++ Dale/Weems/Headington Chapter 9 Additional Control Structures (Switch, Do..While, For statements)
Lecture 7: Menus and getting input. switch Multiple-selection Statement switch Useful when a variable or expression is tested for all the values it can.
 CSC111 Quick Revision. Problem Write a java code that read a string, then show a list of options to the user to select from them, where:  L to print.
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
 2003 Prentice Hall, Inc. All rights reserved. 1 Basic C++ Programming.
Sudeshna Sarkar, IIT Kharagpur 1 Programming and Data Structure Sudeshna Sarkar Lecture 3.
Repetition statements
Decision making If.. else statement.
The if…else Selection Statement
ECE Application Programming
Chapter 4 C Program Control Part I
Decisions Chapter 4.
Chapter 5 Control Statements: switch Statement
Chapter 3 Selections Liang, Introduction to Java Programming, Eighth Edition, (c) 2011 Pearson Education, Inc. All rights reserved
Conditionals & Boolean Expressions
Chapter 4: Making Decisions.
Control Statement Examples
INPUT & OUTPUT scanf & printf.
Functions.
1) C program development 2) Selection structure
Decision making If statement.
Program Control Topics While loop For loop Switch statement
Strings Dr. Soha S. Zaghloul updated by Rasha ALEidan
REPETITION STATEMENTS
Functions Extra Examples.
Selection Control Structure
Chapter 8 JavaScript: Control Statements, Part 2
Switch Case Structures
Presentation transcript:

The switch Statement

 Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each of the constant integral values it may assume, and different actions are taken.  This is called multiple selection.  C provides the switch multiple-selection statement to handle such decision making.  The switch statement consists of a series of case labels, an optional default case and statements to execute for each case. © by Pearson Education, Inc. All Rights Reserved. Dr. Soha S. Zaghloul2

switch (identifier) // must be a constant expressions integer or char { case ‘value1’: block of statements 1; break; case ‘value2’: block of statements 2; break; … case ‘value n’: block of statements n; break; default: block of statements; break; } Dr. Soha S. Zaghloul3

 break is used to exit the switch statement.  default is used if the variable did not satisfy any value of the listed cases. Dr. Soha S. Zaghloul4

 Write a program that displays a menu and prints a message for each selection made by the user. The program should prompt the user in case of an invalid input.  The menu to be displayed is as follows: Enter your choice: ‘E’: Edit my program ‘C’: Compile my program ‘R’: Run my program Dr. Soha S. Zaghloul5

1.Display the menu 2.Get the input from the user 3.If the user entered ‘E’, then print “Calling the editor” 4.If the user entered ‘C’, then print “Calling the compiler” 5.If the user entered ‘R’, then print “The program starts execution” 6.For any other input, print “Invalid input” ALGORITHM Dr. Soha S. Zaghloul6

Dr. Soha S. Zaghloul7 START Display Menu READ choice choice = ‘E’ choice = ‘C’ choice = ‘R’ Print “Call Editor” Print “Call Compiler” Print “The program starts execution” Print “Invalid Input” END TRUE FALSE

1.Display the menu 2.Scanf choice 3.Switch for the value of choice: a.Case choice = ‘E’ printf (“Calling the Editor”) b.Break c.Case choice = ‘C’ printf (“Calling the Compiler”) d.Break e.Case choice = ‘R’ printf (“The program starts execution”) f.Break g.Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul8

#include int main (void) { printf (“Enter your choice \n”); printf (“E: Edit \n”); printf (“C: Compile \n”); printf (“R: Run \n”); printf (“What do you want to do? “); } 1.Display the menu 2.Scanf choice 3.Switch for the value of choice: a.Case choice = ‘E’ printf (“Calling the Editor”) b.Break c.Case choice = ‘C’ printf (“Calling the Compiler”) d.Break e.Case choice = ‘R’ printf (“The program starts execution”) f.Break g.Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul9

#include int main (void) { char choice; printf (“Enter your choice \n”); printf (“E: Edit \n”); printf (“C: Compile \n”); printf (“R: Run \n”); printf (“What do you want to do? “); scanf (“%c”, &choice); } 1.Display the menu 2.Scanf choice 3.Switch for the value of choice: a.Case choice = ‘E’ printf (“Calling the Editor”) b.Break c.Case choice = ‘C’ printf (“Calling the Compiler”) d.Break e.Case choice = ‘R’ printf (“The program starts execution”) f.Break g.Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul10

Dr. Soha S. Zaghloul11 #include int main (void) { char choice; printf (“Enter your choice \n”); printf (“E: Edit \n”); printf (“C: Compile \n”); printf (“R: Run \n”); printf (“What do you want to do? \n“); scanf (“%c”, &choice); switch (choice) { case ‘E’: printf (“Calling the Editor \n”); break; case ‘C’: printf (“Calling the Compiler \n”); break; case ‘R’: printf (“The program starts execution \n”); break; default: printf (“Invalid Input \n”); break; } // end switch } 1.Display the menu 2.Scanf choice 3.Switch for the value of choice: a.Case choice = ‘E’ printf (“Calling the Editor”) b.Break c.Case choice = ‘C’ printf (“Calling the Compiler”) d.Break e.Case choice = ‘R’ printf (“The program starts execution”) f.Break g.Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE

#include int main (void) { char choice; printf (“Enter your choice \n”); printf (“E: Edit \n”); printf (“C: Compile \n”); printf (“R: Run \n”); printf (“What do you want to do? \n“); scanf (“%c”, &choice); switch (choice) { case ‘E’: printf (“Calling the Editor \n”); break; case ‘C’: printf (“Calling the Compiler \n”); break; case ‘R’: printf (“The program starts execution \n”); break; default: printf (“Invalid Input \n”); break; } // end switch return (0); } // end of main 1.Display the menu 2.Scanf choice 3.Switch for the value of choice: a.Case choice = ‘E’ printf (“Calling the Editor”) b.Break c.Case choice = ‘C’ printf (“Calling the Compiler”) d.Break e.Case choice = ‘R’ printf (“The program starts execution”) f.Break g.Otherwise printf (“Invalid input”); 4. End of program PSEUDOCODE Dr. Soha S. Zaghloul12

 Sometimes, the same actions are to be performed on two different values.  For example, capital and small letters in the previous example.  The code will be then updated as follows: Dr. Soha S. Zaghloul13

#include int main (void) { char choice; printf (“Enter your choice \n”); printf (“E: Edit \n”); printf (“C: Compile \n”); printf (“R: Run \n”); printf (“What do you want to do? \n“); scanf (“%c”, &choice); switch (choice) { case ‘E’: case ‘e’: printf (“Calling the Editor \n”); break; case ‘C’: case ‘c’: printf (“Calling the Compiler \n”); break; case ‘R’: case ‘r’: printf (“The program starts execution \n”); break; default: printf (“Invalid Input \n”); break; } // end switch return (0); } // end of main Case choice = ‘E’ or ‘e’ printf (“Calling the Editor \n”) The default part is optional Dr. Soha S. Zaghloul14

 Strings CANNOT be used as labels (cases) in a switch statement.  The following code is WRONG because name is a string. char name[20]; printf (“Enter your first name: \n”); scanf (“%s”, name); switch (name) { case “Ahmad”: printf (“Ahmad is a nice boy \n”); break; case “Laila” : printf (“Laila is a nice girl \n”); break; } Dr. Soha S. Zaghloul15