CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.

Slides:



Advertisements
Similar presentations
Character Arrays (Single-Dimensional Arrays) A char data type is needed to hold a single character. To store a string we have to use a single-dimensional.
Advertisements

Fundamental of C programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Testing a program Remove syntax and link errors: Look at compiler comments where errors occurred and check program around these lines Run time errors:
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.
CMSC 104, Version 8/061L22Arrays1.ppt Arrays, Part 1 of 2 Topics Definition of a Data Structure Definition of an Array Array Declaration, Initialization,
Spring 2005, Gülcihan Özdemir Dağ Lecture 4, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 4 Outline 4.0 Reading.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
1 Lecture 4 for loops and switch statements Essentials of Counter-Controlled Repetition Counter-controlled repetition requires  Name of control.
Conditional Statement
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Computer programming Lecture 4. Lecture 4: Outline Making Decisions [chap 6 – Kochan] –The if Statement –The if-else Construct –Logical Operators –Boolean.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
CMSC 104, Version 8/061L11Relational&LogicalOps.ppt Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else.
Assignment Operators = +=-= *= /=%= Statement Equivalent Statement a = a + 2 ;a += 2 ; a = a - 3 ;a -= 3 ; a = a * 2 ;a *= 2 ; a = a / 4 ; a /= 4 ; a =
Programming C for Engineers An exercise is posted on the web site! Due in one week Single submission.
CMSC 104, Lecture 171 More Loops Topics l Counter-Controlled (Definite) Repetition l Event-Controlled (Indefinite) Repetition l for Loops l do-while Loops.
Spring 2005, Gülcihan Özdemir Dağ Lecture 5, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 5 Outline 5.0 Revisiting.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
C++ Programming Lecture 7 Control Structure I (Selection) – Part II The Hashemite University Computer Engineering Department.
CMSC 104, Version 9/011 The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section 4.7,
CMSC 104, Version 9/011 More Loops Topics Counter-Controlled (Definite) Repetition Event-Controlled (Indefinite) Repetition for Loops do-while Loops Choosing.
CMSC 1041 More Loops ‘for’ loops and ‘do-while’ loops.
Beginning C For Engineers Fall 2005 Lecture 3: While loops, For loops, Nested loops, and Multiple Selection Section 2 – 9/14/05 Section 4 – 9/15/05 Bettina.
Variables in C Topics  Naming Variables  Declaring Variables  Using Variables  The Assignment Statement Reading  Sections
CMSC 104, Version 9/011 Relational and Logical Operators Topics Relational Operators and Expressions The if Statement The if-else Statement Nesting of.
CMSC 104, Version 8/061L09VariablesInC.ppt Variables in C Topics Naming Variables Declaring Variables Using Variables The Assignment Statement Reading.
BIL 104E Introduction to Scientific and Engineering Computing Lecture 6.
Dale Roberts Program Control Department of Computer and Information Science, School of Science, IUPUI Fall 2003 CSCI 230 Dale Roberts, Lecturer
 2003 Prentice Hall, Inc. All rights reserved. ECE 2551 Dr. S. Kozaitis Fall Chapter 5 - Control Statements: Part 2 Outline 5.3 for Repetition.
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
Chapter 4 - Program Control
Chapter 2.1 Control Structures (Selection)
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
The C “switch” Statement
Arrays, Part 1 of 2 Topics Definition of a Data Structure
مبانی برنامه‌سازی با C++ جلسه دوم
The C “switch” Statement
- Additional C Statements
2008/10/22: Lecture 12 CMSC 104, Section 0101 John Y. Park
Structured Program
The switch Statement Topics Multiple Selection switch Statement
The switch Statement Topics Multiple Selection switch Statement
Program Control Topics While loop For loop Switch statement
Arrays, Part 1 of 2 Topics Definition of a Data Structure
switch Selection Structure
Computer Programming Techniques Semester 1, 1998
Arrays, Part 1 of 2 Topics Definition of a Data Structure
2.6 The if/else Selection Structure
More Loops Topics Counter-Controlled (Definite) Repetition
Dale Roberts, Lecturer IUPUI
Chapter 4 - Program Control
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
More Loops Topics Counter-Controlled (Definite) Repetition
Variables in C Topics Naming Variables Declaring Variables
The switch Statement Topics Multiple Selection switch Statement
2008/11/19: Lecture 18 CMSC 104, Section 0101 John Y. Park
UMBC CMSC 104 – Section 01, Fall 2016
Arrays, Part 1 of 2 Topics Definition of a Data Structure
More Loops Topics Counter-Controlled (Definite) Repetition
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
More Loops Topics Counter-Controlled (Definite) Repetition
Switch Case Structures
Presentation transcript:

CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading Section

CMSC 104, Version 8/062L15Switch.ppt Multiple Selection So far, we have only seen binary selection. if ( age >= 18 ) { printf(“Vote!\n”) ; } if ( age >= 18 ) { printf(“Vote!\n”) ; } else { printf(“Maybe next time!\n”) ; }

CMSC 104, Version 8/063L15Switch.ppt Multiple Selection (con’t) Sometimes it is necessary to branch in more than two directions. We do this via multiple selection. The multiple selection mechanism in C is the switch statement.

CMSC 104, Version 8/064L15Switch.ppt Multiple Selection with if if (day == 0 ) { printf (“Sunday”) ; } if (day == 1 ) { printf (“Monday”) ; } if (day == 2) { printf (“Tuesday”) ; } if (day == 3) { printf (“Wednesday”) ; } (continued) if (day == 4) { printf (“Thursday”) ; } if (day == 5) { printf (“Friday”) ; } if (day == 6) { printf (“Saturday”) ; } if ((day 6)) { printf(“Error - invalid day.\n”) ; }

CMSC 104, Version 8/065L15Switch.ppt Multiple Selection with if-else if (day == 0 ) { printf (“Sunday”) ; } else if (day == 1 ) { printf (“Monday”) ; } else if (day == 2) { printf (“Tuesday”) ; } else if (day == 3) { printf (“Wednesday”) ; } else if (day == 4) { printf (“Thursday”) ; } else if (day == 5) { printf (“Friday”) ; } else if (day = 6) { printf (“Saturday”) ; } else { printf (“Error - invalid day.\n”) ; } This if-else structure is more efficient than the corresponding if structure. Why?

CMSC 104, Version 8/066L15Switch.ppt The switch Multiple-Selection Structure switch ( integer expression ) { case constant 1 : statement(s) break ; case constant 2 : statement(s) break ;... default: : statement(s) break ; }

CMSC 104, Version 8/067L15Switch.ppt switch Statement Details The last statement of each case in the switch should almost always be a break. The break causes program control to jump to the closing brace of the switch structure. Without the break, the code flows into the next case. This is almost never what you want. A switch statement will compile without a default case, but always consider using one.

CMSC 104, Version 8/068L15Switch.ppt Creating Menus When you want to give your user a choice on what to do next, you can display a set of choices (a menu). The user then enters his or her choice. You must validate the choice to make sure it is valid before you continue the program!

CMSC 104, Version 8/069L15Switch.ppt Sample Program int choice; choice = 0; do { printf( “my menu\n\n” ); printf( “1 – edit\n” ); printf( “2 – delete\n” ); printf( “3 – quit\n” ); printf( “enter your choice: “ ); scanf( “%d”, &choice ); } while ( ( choice >= 1 ) && ( choice <= 3 ) );

CMSC 104, Version 8/0610L15Switch.ppt Sample Program (cont’d) switch( choice ) { case 1: printf( “Do edit\n” ); break; case 2: printf( “Do delete\n” ); break; case 3: printf( “Done\n” ); break; default: printf( “Invalid choice!\n” ); break; }

CMSC 104, Version 8/0611L15Switch.ppt Good Programming Practices Include a default case to catch invalid data. Inform the user of the type of error that has occurred (e.g., “Error - invalid day.”). If appropriate, display the invalid value. If appropriate, terminate program execution (discussed in CMSC 201).

CMSC 104, Version 8/0612L15Switch.ppt switch Example switch ( day ) { case 0: printf (“Sunday\n”) ; break ; case 1: printf (“Monday\n”) ; break ; case 2: printf (“Tuesday\n”) ; break ; case 3: printf (“Wednesday\n”) ; break ; case 4: printf (“Thursday\n”) ; break ; case 5: printf (“Friday\n”) ; break ; case 6: printf (“Saturday\n”) ; break ; default: printf (“Error -- invalid day.\n”) ; break ; } Is this structure more efficient than the equivalent nested if-else structure?

CMSC 104, Version 8/0613L15Switch.ppt Why Use a switch Statement? A nested if-else structure is just as efficient as a switch statement. However, a switch statement may be easier to read. Also, it is easier to add new cases to a switch statement than to a nested if-else structure.

CMSC 104, Version 8/0614L15Switch.ppt Common Programming Errors The following if statement is true for all values of x! if( 0 <= x <= 4) printf(“Condition is true\n” ); Instead,use if( 0 <= x && x <= 4) The following always prints the same thing: if ( x = 10 ) printf( “ x is 10\n” );

CMSC 104, Version 8/0615L15Switch.ppt The char Data Type The char data type holds a single character. char ch; Example assignments: char grade, symbol; grade = ‘B’; symbol = ‘$’; The char is held as a one-byte integer in memory. The ASCII code is what is actually stored, so we can use them as characters or integers, depending on our need.

CMSC 104, Version 8/0616L15Switch.ppt The char Data Type (con’t) Use scanf (“%c”, &ch) ; to read a single character into the variable ch. (Note that the variable does not have to be called “ch”.”) Use printf(“%c”, ch) ; to display the value of a character variable.

CMSC 104, Version 8/0617L15Switch.ppt char Example #include int main ( ) { char ch ; printf (“Enter a character: “) ; scanf (“%c”, &ch) ; printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ; } If the user entered an A, the output would be: The value of A is 65.

CMSC 104, Version 8/0618L15Switch.ppt The getchar ( ) Function The getchar( ) function is found in the stdio library. The getchar( ) function reads one character from stdin (the standard input buffer) and returns that character’s ASCII value. The value can be stored in either a character variable or an integer variable.

CMSC 104, Version 8/0619L15Switch.ppt getchar ( ) Example #include int main ( ) { char ch ; /* int ch would also work! */ printf (“Enter a character: “) ; ch = getchar( ) ; printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ; } If the user entered an A, the output would be: The value of A is 65.

CMSC 104, Version 8/0620L15Switch.ppt Problems with Reading Characters When getting characters, whether using scanf( ) or getchar( ), realize that you are reading only one character. What will the user actually type? The character he/she wants to enter, followed by pressing ENTER. So, the user is actually entering two characters, his/her response and the newline character. Unless you handle this, the newline character will remain in the stdin stream causing problems the next time you want to read a character. Another call to scanf() or getchar( ) will remove it.

CMSC 104, Version 8/0621L15Switch.ppt Improved getchar( ) Example #include int main ( ) { char ch, newline ; printf (“Enter a character: “) ; ch = getchar( ) ; newline = getchar( ) ; /* could also use scanf(“%c”, &newline) ;*/ printf (“The value of %c is %d.\n”, ch, ch) ; return 0 ; } If the user entered an A, the output would be: The value of A is 65.

CMSC 104, Version 8/0622L15Switch.ppt Additional Concerns with Garbage in stdin When we were reading integers using scanf( ), we didn’t seem to have problems with the newline character, even though the user was typing ENTER after the integer. That is because scanf( ) was looking for the next integer and ignored the newline (whitespace). If we use scanf (“%d”, &num); to get an integer, the newline is still stuck in the input stream. If the next item we want to get is a character, whether we use scanf( ) or getchar( ), we will get the newline. We have to take this into account and remove it.

CMSC 104, Version 8/0623L15Switch.ppt EOF Predefined Constant getchar( ) is usually used to get characters from a file until the end of the file is reached. The value used to indicate the end of file varies from system to system. It is system dependent. But, regardless of the system you are using, there is a #define in the stdio library for a symbolic integer constant called EOF. EOF holds the value of the end-of-file marker for the system that you are using.

CMSC 104, Version 8/0624L15Switch.ppt getchar( ) Example Using EOF #include int main () { int grade, aCount, bCount, cCount, dCount, fCount ; aCount = bCount = cCount = dCount = fCount = 0 ; while ( (grade = getchar( ) ) != EOF ) { switch ( grade ) { case ‘A’: aCount++; break ; case ‘B’: bCount++; break ; case ‘C’ : cCount++; break ; case ‘D’: dCount++; break ; case ‘F’: fCount++; break ; default : break ; } return 0 ; }