6_C_Part_3_Control_Structures.pdf

Expression 0 switch variable case value1 note use not

Info icon This preview shows pages 18–28. Sign up to view the full content.

View Full Document Right Arrow Icon
expression == 0 */ } switch ( <variable> ) { case value1: /* Note - use : not ; */ Code to execute if <variable> == value1; break; case value2: Code to execute if <variable> == value2; break; ... default: Code to execute if <variable> does not equal the value following any of the cases… break; } SWITCH NOTES: Notice, no {} blocks within each case! Notice the colon for each case and value. value1, value2, etc. in a switch statement must be constant values; variable should be some integer type The default case is optional, but it is wise to include it as it handles any unexpected cases. Fall-throughs are allowed (if break is omitted) Chooses first value that matches…
Image of page 18

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
/* Suppose this code, with scores from 0 to 100 */ int score = 80; if (score >= 60) if (score >= 90) printf(“Congratulations, you got an A!\n”); else printf(“Unfortunately, that’s not passing!\n”); /* Output? */
Image of page 19
Output: Unfortunately, that’s not passing! What happened? We only wanted this message for scores < 60.
Image of page 20

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` The problem is that the compiler does not pay attention to formatting, so even though the else is aligned with the first if, the compiler pairs it with the second if. ` Rule: The compiler pairs any else with the closest (most recent) unmatched if which is not enclosed in a different block. ` “Unmatched” here means that the if has not already been paired with an else.
Image of page 21
int score = 80; if (score >= 60) { if (score >= 90) printf(“Congratulations, you got an A!\n”); } else printf(“Unfortunately, that’s not passing!\n”); /* Now, the else will be paired with the first if, because the second one is enclosed in a different block. */
Image of page 22

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
#include <stdio.h> int main() { int age; printf( "Please enter your age" ); scanf( "%d", &age ); if ( age < 100 ) { printf ("You are young!\n" ); } else if ( age == 100 ) { printf("You are old\n" ); } else { printf("You are really old\n" ); } return 0; }
Image of page 23
switch ( x ) { case 'a': /* Do stuff when x == 'a' */ break; case 'b': case 'c': case 'd': /* Fall-through technique... cases b,c,d all use this code */ break; default: /* Handle cases when x is not a,b,c or d. ALWAYS have a default case*/ break; /* this break is not necessary, but legal */ }
Image of page 24

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` Every boolean test is an implicit comparison against zero (0). ` However, zero is not a simple concept. It represents: the integer 0 for all integer types the floating point 0.0 (positive or negative) the null character (‘\0’) the null pointer ` In order to make your intentions clear, explicitly show the comparison with zero for all scalars, floating-point numbers, and characters.
Image of page 25
` int i; if (i) is better represented as if (i != 0) ` float x; if (!x) is better represented as if (x == 0.0) ` char c; if (c) is better represented as if (c != '\0') ` An exception is made for pointers, since 0 is the only language-level representation for the null pointer. ` The symbol NULL is not part of the core language - you have to include a special header file to get it defined (stdlib.h or stddef.h). More on this later.
Image of page 26

Info icon This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document Right Arrow Icon
` To write an INFINITE LOOP for (;;) ...
Image of page 27
Image of page 28
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern