Chapter 6. Section 6.4 Altering the Flow of Control. CS 50 Hathairat Rattanasook

Similar documents
Chapter 2. Section 2.3 if and if-else statements. CS 50 Hathairat Rattanasook

Repetition Structures II

Chapter 5. Section 5.1 Introduction to Strings. CS 50 Hathairat Rattanasook

Loops and Files. Chapter 04 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

ENGR 1181 MATLAB 09: For Loops 2

Chapter 6. Section 6.3 The switch Statement. CS 50 Hathairat Rattanasook

Loops! Loops! Loops! Lecture 5 COP 3014 Fall September 25, 2017

Chapter 2. Section 2.5 while Loop. CS 50 Hathairat Rattanasook

switch case Logic Syntax Basics Functionality Rules Nested switch switch case Comp Sci 1570 Introduction to C++

Fundamentals of Computer Science. Sentinel Based Repetition

LECTURE 5 Control Structures Part 2

There are algorithms, however, that need to execute statements in some other kind of ordering depending on certain conditions.

Flow Control. CSC215 Lecture

SELECTION STATEMENTS:

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Chapter 13 Control Structures

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

Programming for Experimental Research. Flow Control

INTRODUCTION TO C++ PROGRAM CONTROL. Dept. of Electronic Engineering, NCHU. Original slides are from

Module 4: Decision-making and forming loops

Bil 104 Intiroduction To Scientific And Engineering Computing. Lecture 7

Dept. of CSE, IIT KGP

ITC213: STRUCTURED PROGRAMMING. Bhaskar Shrestha National College of Computer Studies Tribhuvan University

Introduction. C provides two styles of flow control:

Chapter 3. Section 3.10 Type of Expressions and Automatic Conversion. CS 50 Hathairat Rattanasook

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

MODULE 2: Branching and Looping

Chapter 5. Section 5.4 The Common String Library Functions. CS 50 Hathairat Rattanasook

Exceptions, Case Study-Exception handling in C++.

Lecture 6. Statements

Control Statements. If Statement if statement tests a particular condition

COGS 119/219 MATLAB for Experimental Research. Fall 2016 Week 1 Built-in array functions, Data types.m files, begin Flow Control

Subject: PIC Chapter 2.

Prepared by: Shraddha Modi

Branching is deciding what actions to take and Looping is deciding how many times to take a certain action.

2/5/2018. Learn Four More Kinds of C Statements. ECE 220: Computer Systems & Programming. C s if Statement Enables Conditional Execution

Chapter 13 Control Structures

Example. CS 201 Selection Structures (2) and Repetition. Nested if Statements with More Than One Variable

Overview. CS 201 Repetition. Endfile-controlled loops. What is the Output? Nested Loops. Loops. Debzani Deb

Fundamentals of Computer Programming Using C

Decision Making -Branching. Class Incharge: S. Sasirekha

Informatics Ingeniería en Electrónica y Automática Industrial. Control flow

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

10/30/2010. Introduction to Control Statements. The if and if-else Statements (cont.) Principal forms: JAVA CONTROL STATEMENTS SELECTION STATEMENTS

Flow of Control. Selection. if statement. True and False in C False is represented by any zero value. switch

Chapter 4. Section 4.4 Passing Parameters to Functions. CS 50 Hathairat Rattanasook

n Group of statements that are executed repeatedly while some condition remains true

The for Loop, Accumulator Variables, Seninel Values, and The Random Class. CS0007: Introduction to Computer Programming

The C language. Introductory course #1

Floating-point lab deadline moved until Wednesday Today: characters, strings, scanf Characters, strings, scanf questions clicker questions

공학프로그래밍언어 (PROGRAMMING LANGUAGE FOR ENGINEERS) -CONTROL FLOW : LOOP- SPRING 2015, SEON-JU AHN, CNU EE

Main Program. C Programming Notes. #include <stdio.h> main() { printf( Hello ); } Comments: /* comment */ //comment. Dr. Karne Towson University

Chapter 4. Flow of Control

CSE 130 Introduction to Programming in C Control Flow Revisited

DECISION CONTROL AND LOOPING STATEMENTS

& Technology. Expression? Statement-x. void main() int no; scanf("%d", &no); if(no%2==0) if(no%2!=0) Syllabus for 1. of execution of statements.

Chapter 13. Control Structures

REPETITION CONTROL STRUCTURE LOGO

204111: Computer and Programming

C++ Programming: From Problem Analysis to Program Design, Third Edition

Lecture 10. Daily Puzzle

C Language Part 2 Digital Computer Concept and Practice Copyright 2012 by Jaejin Lee

Unit 3 Decision making, Looping and Arrays

Programming: Java. Chapter Objectives. Control Structures. Chapter 4: Control Structures I. Program Design Including Data Structures

Sudeshna Sarkar Dept. of Computer Science & Engineering. Indian Institute of Technology Kharagpur

8. Control statements

C++ Programming: From Problem Analysis to Program Design, Third Edition

Problem Solving and 'C' Programming

The data in the table are arranged into 12 rows and 12 columns. The process of printing them out can be expressed in a pseudocode algorithm as

Converting a Lowercase Letter Character to Uppercase (Or Vice Versa)

Chapter 4: Control structures. Repetition

Introduction to Programming (Java) 4/12

Introduction to Programming in C Department of Computer Science and Engineering. Lecture No. #16 Loops: Matrix Using Nested for Loop

CS107, Lecture 4 C Strings

Linked-List Basic Examples. A linked-list is Linear collection of self-referential class objects, called nodes Connected by pointer links

Chapter 4: Control structures

LESSON 6 FLOW OF CONTROL

Loops / Repetition Statements. There are three loop constructs in C. Example 2: Grade of several students. Example 1: Fixing Bad Keyboard Input

Loops / Repetition Statements

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

CS107 Spring 2019, Lecture 4 C Strings

CS240: Programming in C

Repetition and Loop Statements Chapter 5

Chapter 2. Introduction to C language. Together Towards A Green Environment

Lecture 7 Tao Wang 1

M3-R4: PROGRAMMING AND PROBLEM SOLVING THROUGH C LANGUAGE

Repetition CSC 121 Fall 2014 Howard Rosenthal

Computer Programming

The Arithmetic Operators

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

At the end of this lecture you should be able to have a basic overview of fundamental structures in C and be ready to go into details.

CSE 143 Au03 Midterm 2 Page 1 of 7

EECE.2160: ECE Application Programming Fall 2017

while (condition) { body_statements; for (initialization; condition; update) { body_statements;

OER on Loops in C Programming

Chapter 4: Making Decisions

In this lab, you will learn more about selection statements. You will get familiar to

"Hello" " This " + "is String " + "concatenation"

DECISION MAKING STATEMENTS

Transcription:

Chapter 6 Section 6.4 Altering the Flow of Control CS 50 Hathairat Rattanasook

continue; The continue statement will skip the current iteration of a loop and jump to the next iteration. // continue in a while loop while ( expression ) { continue; // or with a do-while loop (can also be used in: for loop) do { continue; while ( expression ); CS 50 - Hathairat Rattanasook 1

break; The break statement will terminate the current loop and jump to the end of it. // break in a while loop while ( expression ) { break; // or with a do-while loop (can also be used in: for loop, switch) do { break; while ( expression ); CS 50 - Hathairat Rattanasook 2

continue & break The continue; and break; statements are used in loops, such as for-loop, while-loop, and do-while-loop. The break; statement can also be used in a switch statement. Both statements alter the control flow of a loop. Since both statements alter the flow of the program, they should be used with caution! CS 50 - Hathairat Rattanasook 3

continue statement The continue; statement is used inside a loop. When it is encountered, it causes the control to jump to start the next iteration instead of executing the next statement as it would normally do. while ( expression ) { if (expression2) continue; In the example, the while loop executes normally until the if-condition becomes true and the continue; statement is encountered. Then the flow ends the current iteration and jumps to the end of the loop body. The loop will continue by evaluating the expression and if true start the next iteration CS 50 - Hathairat Rattanasook 4

break statement The break; statement is used inside a loop or switch statement. When it is encountered, it jumps to the end of the current loop or switch statement. while ( expression ) { if (expression2) break; In the example, the while loop executes normally until the if-condition becomes true and the break; statement is encountered. Then the flow ends the current loop and jumps to the code after. Unlike the continue; statement, the loop does NOT continue. CS 50 - Hathairat Rattanasook 5

break with switch 1 switch ( expression ) { 2 case constant1: statement1(s); break; 3 case constant2: statement2(s); break; 4 case constant3: statement3(s); break; 5 default: statementd(s); break; 6 7... If the expression inside the switch ( and ) matches expression constant2, statement2(s) is/are executed. When the break; statement is encountered, it will cause the flow to jump to the end of the switch statement in line 7 instead of continuing with statement3(s) of case constant3 as the usual flow would be. CS 50 - Hathairat Rattanasook 6

while Example 1 int i = 10; 2 while (i > 0) { 3 printf(" "); 4 if (i-- % 3 == 0) 5 continue; // jump to while (..) 6 printf("*"); 7 Output: * * * * * * * 1. i = 10 2. 10 > 0 => true 3. 10 % 3 == 0 => false 4. 10-- => 9 5. 9 > 0 => true 6. 9 % 3 == 0 => true 7. i-- => 8 8. continue // skips printf("*"); 9. jump to while (i > 0) 10. 9 > 0 => true 11.... CS 50 - Hathairat Rattanasook 7

for Example char str[256], ch; int i; printf("enter some text: "); fgets(str, 256, stdin); printf("enter a char to search: "); scanf("%c", &ch); for (i = 0; i < strlen(str); i++) { if (str[i] == ch) { printf("*"); continue; printf("%c", str[i]); The example searches for a char in a string. It prints out a * at the position where the char is found. The the char matches, it prints out * and then continues to the next iteration. CS 50 - Hathairat Rattanasook 8

for Example int indexof(char* str, char ch) { int i, found = -1; for (i = 0; i < strlen(str); i++) { if (*(str + i) == ch) { found = i; break; return found; The function indexof() searches for a char ch in a string str. When the first occurrence of ch is found, it stores the index i in the variable found. Then, the break statement terminates the loop and jumps to to the end of it. Then the return statement returns found. CS 50 - Hathairat Rattanasook 9

Questions? 1. What is the difference between continue and break? 2. Does continue execute the loop expression before starting the next iteration? 3. What happens if break/continue is used in a nested loop? CS 50 - Hathairat Rattanasook 10

Answers 1 and 2 1. continue skips the current iteration. break terminates the loop 2. Yes, for example: while (i < 10) { statement1; continue; statement2; statementn; When continue is encountered, the current iteration is skipped and statements 2 to N are not executed. Then (i < 10) is checked and if true, the next iteration starts. CS 50 - Hathairat Rattanasook 11

Answer 3 3. What happens if break/continue is used in a nested loop? while (i < 10) { // outer loop statement1; while (k < 5) { // inner loop continue; statement2; statementn; In the example above, break will only terminate the inner loop, and will continue the outer loop at statement2. Remember, break/continue will only affect the closest loop to where they are used. CS 50 - Hathairat Rattanasook 12