Chapter 4. Flow of Control

Similar documents
Chapter 2. Lexical Elements & Operators

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

Problem Solving and 'C' Programming

Flow Control. CSC215 Lecture

3 The L oop Control Structure

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

Statements. Control Flow Statements. Relational Operators. Logical Expressions. Relational Operators. Relational Operators 1/30/14

Introduction. C provides two styles of flow control:

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

A Look Back at Arithmetic Operators: the Increment and Decrement

B.V. Patel Institute of Business Management, Computer & Information Technology, Uka Tarsadia University

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

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

YOLOP Language Reference Manual

Lecture 10. Daily Puzzle

Programming for Engineers Iteration

Assoc. Prof. Dr. Marenglen Biba. (C) 2010 Pearson Education, Inc. All rights reserved.

Chapter 6. Loops. Iteration Statements. C s iteration statements are used to set up loops.

Chapter 3. Fundamental Data Types

Programming for Electrical and Computer Engineers. Loops

Decision Making -Branching. Class Incharge: S. Sasirekha

ECE 2400 Computer Systems Programming Fall 2018 Topic 1: Introduction to C

Chapter 4: Expressions. Chapter 4. Expressions. Copyright 2008 W. W. Norton & Company. All rights reserved.

CS1100 Introduction to Programming

Chapter 5: Control Structures

DECISION CONTROL AND LOOPING STATEMENTS

AN OVERVIEW OF C, PART 3. CSE 130: Introduction to Programming in C Stony Brook University

COP 2000 Introduction to Computer Programming Mid-Term Exam Review

COMP 208 Computers in Engineering

Dept. of CSE, IIT KGP

LESSON 6 FLOW OF CONTROL

Chapter 4 C Program Control

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

Loops / Repetition Statements

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

Lecture 6. Statements

Introduction to C Programming

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

REPETITION CONTROL STRUCTURE LOGO

Overview of C, Part 2. CSE 130: Introduction to Programming in C Stony Brook University

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

Sequence structure. The computer executes java statements one after the other in the order in which they are written. Total = total +grade;

Introduction to the Java Basics: Control Flow Statements

BBM 101 Introduc/on to Programming I Fall 2013, Lecture 4

MODULE 2: Branching and Looping

Chapter 4 C Program Control

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

ECE15: Introduction to Computer Programming Using the C Language. Lecture Unit 4: Flow of Control

Lesson #4. Logical Operators and Selection Statements. 4. Logical Operators and Selection Statements - Copyright Denis Hamelin - Ryerson University

Unit 3 Decision making, Looping and Arrays

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

\n is used in a string to indicate the newline character. An expression produces data. The simplest expression

DECISION MAKING STATEMENTS

3. EXPRESSIONS. It is a sequence of operands and operators that reduce to a single value.

Lecture 8. Daily Puzzle

Chapter 3 Structured Program Development

Theory of control structures

8. Control statements

(Not Quite) Minijava

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

Module 4: Decision-making and forming loops

Lecture 3. More About C

C Program Control. Chih-Wei Tang ( 唐之瑋 ) Department of Communication Engineering National Central University JhongLi, Taiwan

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Conditional Statement

Structured Program Development

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

Structured Programming. Dr. Mohamed Khedr Lecture 9

Computer System and programming in C

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Note: unless otherwise stated, the questions are with reference to the C Programming Language. You may use extra sheets if need be.

XQ: An XML Query Language Language Reference Manual

Functions. Computer System and programming in C Prentice Hall, Inc. All rights reserved.

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

Loops / Repetition Statements

CHAPTER : 9 FLOW OF CONTROL

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

DELHI PUBLIC SCHOOL TAPI

Chapter 5. Repetition. Contents. Introduction. Three Types of Program Control. Two Types of Repetition. Three Syntax Structures for Looping in C++

CSE 130 Introduction to Programming in C Control Flow Revisited

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

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

Conditionals and Loops

CSE101-lec#12. Designing Structured Programs Introduction to Functions. Created By: Amanpreet Kaur & Sanjeev Kumar SME (CSE) LPU

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

Chapter 5 Control Statements: Part 2 Section 5.2 Essentials of Counter-Controlled Repetition

5. Selection: If and Switch Controls

Iosif Ignat, Marius Joldoș Laboratory Guide 4. Statements. STATEMENTS in C

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

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

The Arithmetic Operators

Java Programming: Guided Learning with Early Objects Chapter 5 Control Structures II: Repetition

Topics. Chapter 5. Equality Operators

A complex expression to evaluate we need to reduce it to a series of simple expressions. E.g * 7 =>2+ 35 => 37. E.g.

15 FUNCTIONS IN C 15.1 INTRODUCTION

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

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

Lecture 9. Assignment. Logical Operations. Logical Operations - Motivation 2/8/18

Review of the C Programming Language for Principles of Operating Systems

Programming Basics and Practice GEDB029 Decision Making, Branching and Looping. Prof. Dr. Mannan Saeed Muhammad bit.ly/gedb029

Transcription:

Chapter 4. Flow of Control Byoung-Tak Zhang TA: Hanock Kwak Biointelligence Laboratory School of Computer Science and Engineering Seoul National Univertisy http://bi.snu.ac.kr

Sequential flow of control Flow of Control Statement in a program are normally executed one after another. Often it is desirable to alter the sequential flow of control to provide for a choice of action if, if-else, switch or a repetition of action while, for, do-while

True True and False Any non-zero integers are treated as true. False Zero means false. Logical and Relational Operations Results of the logical and relational operations are one(true) or zero(false) printf( %d, 2 == 2) ; // equal, 1 printf( %d,! 0) ; // negation, 1

Relational Operator expr < expr expr > expr expr <= expr expr >= expr <Examples> a < 3 a > b -1.3 >= (2.0 * x + 3.3) a < b If a is less than b, then 1 (true). If a is not less than b, then 0 (false).

Relational Operator

Relational Operator expr == expr expr!= expr a!= b If a is not equal to b, then 1 (true). If a is equal to b, then 0 (false). <Examples> c == A k!= -2 3 + z == x + y/4

Relational Operator!! A common programming error if (a = 1) if (a == 1)

Negation! expr negation If expr has value zero,! expr is 1 (true). If expr has non-zero value,! expr is 0 (false). <Examples>! a! (x + 7.7)! (a < b c < d)

Negation

Logical Operator expr expr (logical or) expr && expr (logical and) a b If a or b is 1, then 1 (true). If a and b are both 0, then 0 (false). a && b If a and b are both 1, then 1 (true). If a or b is 0, then 0 (false) <Examples> a && b a b 10 < a && a < 100

Logical Operator && has higher precedence than. Both of && and are of lower precedence than all unary, arithmetic, equality, and relational operators. Short-circuit Evaluation In evaluating the expr.s that are the operands of && and, the evaluation process stops as soon as the outcome true or false is known. expr1 && expr2, if expr1 has value zero, then expr2 is not evaluated expr1 expr2, if expr1 has nonzero value, then expr2 is not evaluated

Logical Operator

Statement Statement Statement is a smallest standalone element of a C source code. Expression Statement an expression followed by ; Empty statement written as a single semicolon useful where a statement is needed syntactically a = b; /* assignment statement */ a + b + c; /* legal, but no useful work gets done */ ; /* empty statement */ printf( %d\n, a); /* a function call */

Compound Statement Statement a series of declarations and statements surrounded by braces block for grouping statements into an executable unit It is itself a statement, thus it can be placed wherever a statement is placed. { } a = 1; { /* nested */ b = 2; c = 3; }

if and if-else Statement if ( expr ) statement If expr is nonzero, then statement is executed; otherwise, statement is skipped and control passes to the next statement. if (j < k) { min = j; printf( j is smaller than k\n ); }

if and if-else Statement if ( expr ) statement1 else statement2 if (c >= a && c <= z ) ++lc_cnt; else { ++other_cnt; printf( %c is not a lowercase letter\n, c); } If expr is nonzero, then statement1 is executed and then skip statement2; otherwise, statement1 is skipped and then statement2 is executed.

if and if-else Statement if (a ==1) if ( b == 2) /* if statement is itself a statement */ printf( ***\n ); dangling else problem (An else attaches to the nearest if.) if (a ==1) if ( b == 2) printf( ***\n ); else printf( ###\n ); if (a ==1) if ( b == 2) printf( ***\n ); else printf( ###\n );

Complex Example if and if-else Statement if (c == ) ++blank_cnt; else if (c >= 0 && c <= 9 ) ++digit_cnt; else if (c >= a && c <= z c >= a && c <= z ) ++letter_cnt; else if (c == \n ) ++nl_cnt; else ++other_cnt;

while Statement while ( expr ) statement First expr is evaluated. If it is nonzero, then statement is executed and control is passed back to expr. This repetition continues until expr is zero. // print i and increment it until it is less than 10 while ( i < 10 ) printf( %d, i++);

for ( expr1 ; expr2 ; expr3 ) statement for Statement First, expr1(initialization) is evaluated. expr1 ; while ( expr2 ) { } statement expr3; expr2 is evaluated. If it is nonzero, then statement is executed, expr3 is evaluated, and control is passed back to expr2. expr2 is a logical expression controlling the iteration. This process continues until expr2 is zero.

for Statement // print seven factorial value int i, factorial = 1; for ( i = 2; i <= 6; i++ ) factorial = factorial * i; printf( 7! is %d, factorial);

for Statement // print all cases of rolling two dices int i, j; for ( i = 1; i <= 6; i++ ) { for ( j = 1; j <= 6; j++) printf( (%d, %d), i, j); printf( \n ); }

Comma Operator expr1, expr2 expr1 is evaluated, and then expr2. for (sum = 0, i =1; i <= 10; ++i) sum += i;

do-while Statement do statement while ( expr ) First statement is executed and expr is evaluated. If the value of expr is nonzero, then control is passed back to statement. When expr is zero, it finishes the control.

do-while Statement // try to get positive integer do { printf( Input a positive integer: ); scanf( %d, &n); if (n < 0) printf( \nerror: Do it again!\n\n ); } while (n < 0);

break break; causes an exit from the inner most enclosing loop or switch statement while (1) { scanf( %lf, &x); if (x < 0.0) break; /* exit loop if x is negative */ printf( %f\n, sqrt(x)); } /* break jumps to here */

continue continue; causes the current iteration of a loop to stop and causes the next iteration of the loop to begin immediately /* print all even integer less than 100 */ for ( i = 0; i < 100; i++) { if ( i % 2 == 1) // if i is odd continue; // go to next iteration printf( %d\n, i); }

switch Statement A multiway conditional statement generalizing the if-else statement switch (c) { case a : printf( a\n ); break; case b : case B : printf( b or B\n ); break; default: printf( something else ); } (1) Evaluate the switch expression. (2) Go to the case label having a constant value that matches the value of the expression in (1), or, if a match is not found, go to the default label, or, if there is no default label, terminate the switch. (3) Terminate the switch when a break statement is encountered, or terminate the switch by falling off the end.

expr1? expr2 : expr3; Conditional Operator expr1 is evaluated. If expr1 is nonzero(true), then expr2 is evaluated, and that is the value of the conditional expression as a whole. If expr1 is zero(false), then expr3 is evaluated, and that is the value of the conditional expression as a whole. // x is 100 if y > 10, otherwise -5. x = (y > 10)? 100 : -5 ;