Presentation is loading. Please wait.

Presentation is loading. Please wait.

Switch Statements, Do While, Break, Continue, Goto, Comma Operator

Similar presentations


Presentation on theme: "Switch Statements, Do While, Break, Continue, Goto, Comma Operator"— Presentation transcript:

1 Switch Statements, Do While, Break, Continue, Goto, Comma Operator
CS 240 – Lecture 12 Switch Statements, Do While, Break, Continue, Goto, Comma Operator

2 Control Flow – break statement
The break statement indicates that the current control flow block should be left or jumped out of. break; When break is executed, the program moves to the next instruction after the block. If there are nested control-flow blocks, it only leaves the deepest one that it is in. The break statement will only exit while, for, switch, and do statements. We'll talk about do and switch statements shortly.

3 Control Flow – break statement
/* Print if there is a negative number, only once */ int array[10] = { … }; int i; for (i = 0; i < 10; i++) { if (array[i] < 0) { printf("Found a negative number!"); break; } printf("Loop done!"); /* break skips to here. */

4 Control Flow – continue statement
The continue statement indicates that the program should jump over the remaining contents of the loop and go to the end. continue; After the continue is executed, since the program is now at the end of the loop, it will perform its increment (in the case of the for-loop) and check its condition expression to decide if it should run again. The continue statement only affects the deepest loop scope that it is inside of. The continue statement is only meaningful in loop blocks.

5 Control Flow – continue statement
/* Do some things with positive numbers. */ int array[10] = { … }; int i; for (i = 0; i < 10; i++) { if (array[i] < 0) continue; doSomething(array[i]); doSomethingElse(array[i]); doSomeOtherThing(array[i]); /* continue skips to here */ }

6 Control Flow – goto statement and labels
The break and continue statements have their limitations. They can only change the control flow of the current control flow scope. In C, there are times when you'll want to jump to a very specific point in the code. In many cases, there is a better way to write your code than to add such a jump, but in some cases, there is no better choice. The goto statement facilitates this. goto label; The label must correspond to a labeled location somewhere else in the same function. printlabel: printf("Labeled Statement.");

7 Control Flow – goto statement and labels
int count = 0; for (i = 0; i < 10; i++) for (j = 0; j < 10; j++) for (k = 0; k < 10; k++) { if (i + j + k == 10) count++; if (count == 10) { goto exitlabel; /* Leaves every loop */ } exitlabel: printf("Found 10 triples that added up to 10!");

8 Word of Caution – goto statement
The goto statement is considered to be very bad style when used unnecessarily or incorrectly. They are not a problematic statement in-and-of themselves. Whenever you add a label to code, you give the option to any other part of the code to jump to that location with goto. This makes determining the state of the program at a current point in the code mode difficult and in some cases impossible. When you use goto, you can end up in parts of the code that have not been initialized. You need to make sure that the state of the program after executing a goto is appropriate for the lines of code that are to follow.

9 Word of Caution – goto statement
int i; goto inside; for (i = 0; i < 10; i++) { inside: printf("%d", i); } Here, we enter the for-loop before the loop-control variable i is initialized. It therefore contains junk and has a good chance of leading to unintended behavior.

10 Operators – comma operator ,
The comma (,) operator allows a single expression to consist of multiple expressions and evaluate to a single value. expression1, expression2, … , expressionN The comma expression takes the type and value of the last expression in the list. int i = func(10), "Hello", 10; As you can see above, there's no requirement that the expressions in the comma expression all have the same type. Any side-effects that the expressions in the comma expression have will persist even if their values are not used.

11 Operators – comma operator ,
int a, b; for (a=0,b = strlen(buffer)-1; a < b; a++,b--) { swap(&buffer[a], &buffer[b]); } Above we have code for a basic string reversal. Note the comma expressions for the initialization and the increment portions of the for-loop. Do not confuse the arguments for a function with a comma expression!

12 Control Flow – switch Statement
The switch statement is a type of control flow which takes a single value and uses that value to decide on which group of statements to execute. Inside the block of a switch statement, there are multiple case labels to which the switch jumps based on it's condition value. switch (n) { case 1: doSomething(); break; default: doSomethingElse(); } There must be a default case for every switch statement. The inclusion of a break should happen at the end of each case otherwise the program will fall through into the following cases. Sometimes, this is a valuable feature.

13 Control Flow – switch Statement
char ascii; switch (n) { case 0: case 1: case 2: case 3: case 4: case 5: case 6: case 7: case 8: case 9: ascii = n + '0'; break; case 10: case 11: case 12: case 13: case 14: case 15: ascii = n + 'a' – 10; default: break; } This is some (overcomplicated) code for encoding a single number between 0 and 15 as a hexadecimal digit.

14 Control Flow – do-while statement
The do while statement is very similar to the while statement except for one major difference: the body of the loop always happens at least once. do { /* gets done at least once */ } while (condition); In software engineering, there's this concept of DRY (Don't Repeat Yourself). By using a do while, instead of a while, you can implement Once-Or-More behavior without repeating yourself.

15 Control Flow – do-while statement
int i = 10; int i = 10; do { doSomething(); doSomething(); while (func(i--)) { } while (func(i--)); doSomething(); } See that in the second case, when you wanted Once-Or-More behavior, you needed to write doSomething(); more than once. It doesn't much matter in this example, but consider the case when doSomething(); is instead a larger block of multiple lines of code.


Download ppt "Switch Statements, Do While, Break, Continue, Goto, Comma Operator"

Similar presentations


Ads by Google