Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures.

Slides:



Advertisements
Similar presentations
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures.
Advertisements

Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and Final Review Topics Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 16P. 1Winter Quarter Strings Lecture 16.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 12P. 1Winter Quarter User-Written Functions.
Making Choices in C if/else statement logical operators break and continue statements switch statement the conditional operator.
Engineering EG167C - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 19P. 1Winter Quarter MATLAB: Script and.
1 Loops. 2 Often we want to execute a block of code multiple times. Something is always different each time through the block. Typically a variable is.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
Switch Switch case statements are a substitute for long if statements that compare a variable to several "integral" values ("integral" values are simply.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 13P. 1Winter Quarter Scope of Variables.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 11P. 1Winter Quarter Arrays Lecture 11.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect M1 P. 1Winter Quarter Midterm I Review Topics.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 6P. 1Winter Quarter I/O in C Lecture 6.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 8P. 1Winter Quarter Control Statements Lecture.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect FXP. 1Winter Quarter Course Wrap Up and.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 3P. 1Winter Quarter Structured Engineering.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 15P. 1Winter Quarter Arrays and Pointers.
CMSC 104, Version 8/061L15Switch.ppt The switch Statement Topics Multiple Selection switch Statement char Data Type and getchar( ) EOF constant Reading.
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration.
Spring 2005, Gülcihan Özdemir Dağ Lecture 3, Page 1 BIL104E: Introduction to Scientific and Engineering Computing, Spring Lecture 3 Outline 3.1 Introduction.
Conditional Statement
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
EC-111 Algorithms & Computing Lecture #4 Instructor: Jahan Zeb Department of Computer Engineering (DCE) College of E&ME NUST.
1 CSC103: Introduction to Computer and Programming Lecture No 11.
IF-ELSE IF-ELSE STATEMENT SWITCH-CASE STATEMENT Computer Programming Asst. Prof. Dr. Choopan Rattanapoka and Asst. Prof. Dr. Suphot Chunwiphat.
Flow of Control Part 1: Selection
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 23P. 1Winter Quarter Structs and Enumeration Lecture 23.
PROBLEM SOLVING & ALGORITHMS CHAPTER 5: CONTROL STRUCTURES - SELECTION.
Previously Repetition Structures While, Do-While, For.
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 =
Logic Our programs will have to make decisions in terms of what to do next –we refer to the decision making aspect as logic Logic goes beyond simple if.
Chapter 05 (Part III) Control Statements: Part II.
Lecture 3 – Selection. Outline Recall selection control structure Types of selection One-way selection Two-way selection Multi-selection Compound statement.
The switch Statement.  Occasionally, an algorithm will contain a series of decisions in which a variable or expression is tested separately for each.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 5P. 1Winter Quarter C Programming Basics.
Input/output streams. Character Input / Output and Input Validation Input and output devices: keyboards, disk drives, mouse, monitors, printers, network.
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,
1 CS161 Introduction to Computer Science Topic #8.
IT CS 200: C ONDITION Lect. Napat Amphaiphan. T HE ABILITY TO CONTROL THE FLOW OF YOUR PROGRAM, LETTING IT MAKE DECISIONS ON WHAT CODE TO EXECUTE 2.
Lecture 15: Course Review BJ Furman ME 30 16MAY2011.
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.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
COMP Loop Statements Yi Hong May 21, 2015.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CHARACTER INPUT / OUTPUT AND INPUT VALIDATION. Introduction Input and output devices: keyboards, disk drives, mouse, monitors, printers. I/O functions.
IT CS 200: R EPEATATION Lect. Napat Amphaiphan. T HE ABILITY TO DO THE SAME TASK AGAIN BY AGAIN UNTIL THE CONDITION IS MET LOOP 2.
1 CSC103: Introduction to Computer and Programming Lecture No 9.
Program Control: Selection Subject: T0016 – ALGORITHM AND PROGRAMMING Year: 2013.
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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.
Engineering H192 - Computer Programming Gateway Engineering Education Coalition Lect 10P. 1Winter Quarter Repetition Structures Lecture 10.
The if…else Selection Statement
Chapter 4 C Program Control Part I
The C “switch” Statement
The C “switch” Statement
I/O in C Lecture 6 Winter Quarter Engineering H192 Winter 2005
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
The switch Statement Topics Multiple Selection switch Statement
C++ Programming Lecture 7 Control Structure I (Selection) – Part II
Switch Case Structures
Presentation transcript:

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 1Winter Quarter Switch Case Structures Lecture 9

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 2Winter Quarter Switch Multiple Selection Structure A multiple selection structure is useful when an algorithm contains a series of decisions in which a variable or expression is tested separately for one of several possible integral values. Each integral value represents a different action to be taken in the algorithm. C provides the switch multiple selection structure to implement this type of decision making.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 3Winter Quarter Switch-Case Structures The switch - case syntax is: switch (integer expression test value) { case case _1_fixed_value : action(s) ; case case_2_fixed_value : action(s) ; default : action(s) ; } Note use of colon!

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 4Winter Quarter Switch-Case Structures The switch is the "controlling expression" –Can only be used with constant integer expressions. –Remember, a single character is a small positive integer. –The expression appears in ( ) The case is a "label" –The label must be followed by a " : " –Braces, { }, not required around statements

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 5Winter Quarter Switch-Case Structures Unlike if-else if-else structures, when the value in a case matches the test value, all of the actions in the rest of the structure take place. This is shown in the following program where the user enters a value that matches the first case and every action in the structure is executed.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 6Winter Quarter A Sample Program to Illustrate Switch-Case Problem: Write a program to ask the user to enter his/her letter grade and then respond with an appropriate message regarding his/her academic status.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 7Winter Quarter A Sample Program to Illustrate Switch-Case Algorithm: 1. Set up the environment 2. Prompt user to enter his/her letter grade 3. Get user’s response 4. If grade is a or A say “Good Job” and go to 9 5. If grade is b or B say “Pretty good” and go to 9 6. If grade is c or C say “Better get to work” and go to 9 7 If grade is d or D say “You are in trouble” and go to 9 8. Say “You are failing” 9. Terminate program

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 8Winter Quarter A Sample Program to Illustrate Switch-Case /* This program associates a letter grade with a message appropriate to the score. */ #include int main ( ) { char grade ; printf ("Enter your current letter grade\n") ; grade = getchar ( ) ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 9Winter Quarter A Sample Program to Illustrate Switch-Case switch (grade) { case ('a') : case ('A') : printf ("Good Job!\n") ; case ('b') : case ('B') : printf ("Pretty good.\n") ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 10Winter Quarter A Sample Program to Illustrate Switch-Case case ('c') : case ('C') : printf ("Better get to work.\n") ; case ('d') : case ('D') : printf ("You are in trouble.\n") ; default : printf ("You are failing!!\n") ; } /* End of switch-case structure */ } /* End of main program */

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 11Winter Quarter Switch-Case Structures Resultant Output from Grade Program /* The following results are produced when the user enters an "A" as input to the program prompt. */ Good Job! Pretty good. Better get to work. You are in trouble. You are failing!

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 12Winter Quarter Switch-Case Structures break ; The problems with the previous program can be corrected by use of the break statement. It can be used in either a repetition structure or a selection structure to break out of (that is, to exit from) the structure. The syntax is: break ; The following program is the previous one with the addition of the break statements.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 13Winter Quarter Fixed Program using Switch-Case Structures #include int main ( ) { int grade ; printf ("Enter your current letter grade\n") ; while ( ( grade = getchar ( ) ) != EOF) { switch (grade) { case ('a') : case ('A') : printf ("Good Job!\n") ; break ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 14Winter Quarter Fixed Program using Switch-Case Structures case ('b') :case ('B') : printf ("Pretty good.\n") ; break ; case ('c') :case ('C') : printf ("Better get to work.\n") ; break ; case ('d') : case ('D') : printf ("You are in trouble.\n") ; break ;

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 15Winter Quarter Fixed Program using Switch-Case Structures case ('f') :case ('F'): printf ("You are failing!!\n") ; break ; case (' ') :case ('\n') : //< See note pg 17 break ; default : printf ("Invalid grade. Try again.\n") ; }/* End of switch/case*/ }/* End of while loop */ }/* End of "main" function*/

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 16Winter Quarter Comments on Last Example Program Use of the while repetition structure -- more discussion on repetition structures later this week. Use of the end-of-file, or EOF, test. Note that EOF (a DEFINED constant) is a negative integral value, usually a -1 on most (but not all) systems. (EOF is actually defined in the header file.) Use of ints (instead of chars). Why? From the keyboard, a generates an EOF signal on most UNIX systems.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 17Winter Quarter Comments on Last Example Program The statements: case (' ') :case ('\n') : break ; were used to clear the keyboard input buffer. Another way to clear it is with the statement: fgets(input_flush,256,stdin); where fgets(char_strg, len_char_strg, file_pointer); This fgets statement can prove very useful in today’s daily assignment. The two sample programs which follow show why flushing the input stream is important

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 18Winter Quarter Flawed Sample Character Input Program #include Int main() { char ans2; while(ans2 != ‘E’) // Here ‘E’ is a character constant { printf(“\n Input a character followed by ”); ans2 = getchar(); // getchar takes one char from input buffer // and leaves the in the buffer printf(“ ans2 >> %c <<\n”,ans2); } This program runs until you put in an E. This program does not handle multiple inputs correctly.

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 19Winter Quarter Output from Flawed Sample Program input a char followed by e ans2 = >> e << input a char followed by ans2 = >> << In this case a newline character which is produced by the or is read by getchar on the second time through the loop

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 20Winter Quarter Correct Sample Input Program #include Int main() { char ans2,input_flush[256]; while(ans2 != ‘E’) { printf(“\n Input a character followed by ”); ans2 = getchar(); fgets(input_flush,256,stdin); // Input buffer is flushed // The is not in buffer printf(“ ans2 >> %c <<\n”,ans2); }

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 21Winter Quarter Output from Correct Input Program input a char followed by e ans2 = >> e << input a char followed by f ans2 = >> f <<

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 22Winter Quarter Comments on Last Example Program Example of use of fgets(input_flush,256,stdin); char figure,input_flush[256] ; float size ; printf ("Enter figure type>") ; scanf ("%c", &figure) ; //or figure=getchar( ) ; fgets(input_flush,256,stdin); printf ("Enter size of figure>") ; scanf ("%f", &size) ; fgets(input_flush,256,stdin);

Engineering H192 - Computer Programming The Ohio State University Gateway Engineering Education Coalition Lect 9P. 23Winter Quarter Assignment G08 Use a switch-case structure to select from among the shapes for which calculations are to be made. May use just first character of shape name to select which calculation to make. Program only does one shape, and then exits. No looping required for today's assignment.