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

Slides:



Advertisements
Similar presentations
Introduction to C Programming
Advertisements

ICE1341 Programming Languages Spring 2005 Lecture #13 Lecture #13 In-Young Ko iko.AT. icu.ac.kr iko.AT. icu.ac.kr Information and Communications University.
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Aalborg Media Lab 23-Jun-15 Software Design Lecture 6 “Conditionals and Loops”
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
© 2004 Pearson Addison-Wesley. All rights reserved5-1 Iterations/ Loops The while Statement Other Repetition Statements.
CONTROL STATEMENTS Lakhbir Singh(Lect.IT) S.R.S.G.P.C.G. Ludhiana.
The switch Statement, DecimalFormat, and Introduction to Looping
 Decision making statements Decision making statements if statement if...else statement Nested if...else statement (if...elseif....else Statement) 
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Copyright © 2012 Pearson Education, Inc. Chapter 6 More Conditionals and Loops Java Software Solutions Foundations of Program Design Seventh Edition John.
Chapter 4 Loops Write code that prints out the numbers Very often, we want to repeat a (group of) statement(s). In C++, we have 3 major ways of.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
Week 3 - Wednesday.  What did we talk about last time?  Other C features  sizeof, const  ASCII table  printf() format strings  Bitwise operations.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
1 1 Additional Control Structures Chapter 9 2 New and Improved... Ways to branch Ways to write loops Understanding the break and continue statements.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
Control structures in C by Dr P.Padmanabham Professor (CSE)&Director Bharat Institute of Engineering &Technology Hyderabad Mobile
Week 3 - Friday.  What did we talk about last time?  Preprocessor directives  Other C features  sizeof, const  ASCII table  printf() format strings.
Dr. Sajib Datta Jan 23,  A precedence for each operator ◦ Multiplication and division have a higher precedence than addition and subtraction.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
CS 106 Introduction to Computer Science I 02 / 15 / 2008 Instructor: Michael Eckmann.
Information and Computer Sciences University of Hawaii, Manoa
Chapter 6: Loops.
Week 3 - Friday CS222.
Introduction To Repetition The for loop
Repetition Structures Chapter 9
The switch Statement, and Introduction to Looping
Chapter 8: Control Structures
Week 4 - Monday CS222.
The switch Statement The switch statement provides another way to decide which statement to execute next The switch statement evaluates an expression,
Control Structures.
Strings, Line-by-line I/O, Functions, Call-by-Reference, Call-by-Value
Arrays, For loop While loop Do while loop
Chapter 6 More Conditionals and Loops
Chapter 4 - Program Control
Loops in C.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Module 4 Loops.
Statement-Level Control Structures
Homework Any Questions?.
Control Structures Part 3
By Hector M Lugo-Cordero September 3, 2008
CprE 185: Intro to Problem Solving (using C)
CHAPTER 21 LOOPS 1.
Homework Finishing Chapter 2 of K&R. We will go through Chapter 3 very quickly. Not a lot is new. Questions?
Control Statements Paritosh Srivastava.
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Based on slides created by Bjarne Stroustrup & Tony Gaddis
Comparing Data & the ‘switch’ Statement
Comparing Data & the ‘switch’ Statement
CprE 185: Intro to Problem Solving (using C)
Chap 7. Advanced Control Statements in Java
CSC215 Lecture Control Flow.
Controlling Program Flow
CprE 185: Intro to Problem Solving (using C)
Module 4 Loops and Repetition 9/19/2019 CSE 1321 Module 4.
Chapter 13 Control Structures
Presentation transcript:

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

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.

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. */

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.

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 */ }

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.");

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!");

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.

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.

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.

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!

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.

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.

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.

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.