Presentation is loading. Please wait.

Presentation is loading. Please wait.

CS1061 C Programming Lecture 9: switch Statement and Variable Scope A. O’Riordan, 2004.

Similar presentations


Presentation on theme: "CS1061 C Programming Lecture 9: switch Statement and Variable Scope A. O’Riordan, 2004."— Presentation transcript:

1 CS1061 C Programming Lecture 9: switch Statement and Variable Scope A. O’Riordan, 2004

2 Nested if statements Conditional statements can be nested. Here is an example: if (mark >= 80){ printf(“You achieved distinction\n”); } else{ if (mark >= 60){ printf(You achieved merit\n”); } else{ printf(“You fail\n”); printf(“Hard luck, try again\n”); } } /* end outer else block */

3 switch statement C has a special statement ideal for such examples. It is a multi-way conditional statement called the switch statement. The alternative outcomes are labelled with the case keyword. A conditional expression is evaluated and execution continues from the case label with a value that matches. The expresssion and the label value must be integer-valued. If none match execution continues from the default label. A break statement is often used to exit the switch statement before execution flows into the next alternative. The default part of the switch is optional. If it is omitted and none of the cases are chosen, the flow of control continues after the switch statement.

4 Form of switch statement The syntax is now given: switch(integer-expr){ case value1: case value2: … case valuen: [default: ] } A is a squence of statements. E.g. a=a+2; printf(“%d”,a); The square brackets indicate that the default part is optional.

5 break and continue Break statement: When a break statement is encountered the currently executing loop exits and execution continues after the loop. For example: for ( ; ; ){ printf("Executed only once\n"); break; printf("Never ever executed\n"); } Continue statement: The continue statement is similar to the break statement, but instead of exiting the loop, execution resumes back at the next iteration of the loop.

6 switch Example char letter; printf("Enter a character: "); scanf("%c", &letter); switch(letter){ case 'a': printf("\nYou have selected \'a\'\n"); printf("Have a nice day\n"); break; case 'b': printf("\nYou have selected \'b\'\n"); printf("Enjoy your day\n"); break; default: printf("\nYou did not enter a valid letter\n"); printf("Goodbye!\n"); }

7 goto Statement Another instruction supported is the much-maligned goto statement. This transfers control to a labelled instruction. It is rare for the use of goto to be necessary and the desired effect will nearly always be achieved by correctly subdividing the program into functions and using the break and continue statements. label: ; /*jumping to here*/. goto label;

8 Scope of a variable Every identifier in a program has a scope, the region of the program where it is defined. It is the declaration that dictates the scope of an identifier. Specifically the enclosing block ({}) is the scope of all variables except so- called global variables, which are defined at the top-level and are visible everywhere in the file where declared. Here is a simple example, which illustrates variable scope: int c = 123; int main(){ char c = ‘b’; } All references to c in main() refer to the char c, whereas all references to c outside main refer to the int c.

9 Scope of a variable (2) This can be shown schematically with a diagram: Local variables have, in C parlance, the storage class automatic. Local variables may be prefixed with the keyword auto, e.g. auto int i; This rarely appears in C programs because it is the default.

10 Global Variables Globally declared variables and functions can be seen everywhere in the program, i.e. by all functions. But it is considered good software engineering practice to have as few global variables as possible. If you find that your code has a lot of global variables consider re-designing the program. #include char global_char = ‘G’; int main(){ printf("A global character %c\n",global_char); }


Download ppt "CS1061 C Programming Lecture 9: switch Statement and Variable Scope A. O’Riordan, 2004."

Similar presentations


Ads by Google