0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April 2007 3.2 if, if … else.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Chapter 3: Control Flow S. M. Farhad. Statements and Blocks An expression becomes a statement when it is followed by a semicolon Braces { and } are used.
Lectures on Numerical Methods1 Statements çExpressions, when terminated by a semicolon, become statements. çExamples X = 5; I++; IsPrime(c); c = 5 * (
Statement-Level Control Structures
Fundamental Programming Structures in Java: Control Flow, Arrays and Vectors.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
PHP Functions and Control Structures. 2 Defining Functions Functions are groups of statements that you can execute as a single unit Function definitions.
Homework Any Questions?. Statements / Blocks, Section 3.1 An expression becomes a statement when it is followed by a semicolon x = 0; Braces are used.
Control Flow C and Data Structures Baojian Hua
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
true (any other value but zero) false (zero) expression Statement 2
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
ISBN Chapter 8 Statement-Level Control Structures.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
C++ for Engineers and Scientists Third Edition
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
TODAY’S LECTURE Review Chapter 2 Go over exercises.
C Programming Lecture 12. The Compound Statement b A compound statement is a series of declarations and statements surrounded by braces. b A compound.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Chapter 4 Program Control Statements
ASP.NET Programming with C# and SQL Server First Edition Chapter 3 Using Functions, Methods, and Control Structures.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
Digital Computer Concept and Practice Copyright ©2012 by Jaejin Lee C Language Part 2.
Chapter 3 Control Flow Ku-Yaw Chang Assistant Professor, Department of Computer Science and Information Engineering Da-Yeh University.
1 Homework / Exam Turn in HW3 today Exam 1 next class –Open Book / Open Notes –Recommended Use of Book / Notes in Exam: Avoids reliance on “rote memorization”
C# Programming Fundamentals Control Flow Jim Warren, COMPSCI 280 S Enterprise Software Development.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 8 Chapter 8 Control Structures. Control Structures  A control structure is a control statement and the statements whose execution it controls.
Current Assignments Homework 2 is available and is due in three days (June 19th). Project 1 due in 6 days (June 23 rd ) Write a binomial root solver using.
sequence of execution of high-level statements
Expressions and Statements. Expressions Literals and identifiers are expressions More complex expressions are built from simple expressions by the application.
Controlling Execution Dong Shao, Nanjing Unviersity.
Chapter 8 Repetition Statements. Introduction Iteration - process of looping or the repetition of one or more statements Loop body - the statement, or.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Week 6 Branching. 2 What is “Flow of Control”? l Flow of Control is the execution order of instructions in a program l All programs can be written with.
Chapter 05 (Part III) Control Statements: Part II.
Chapter 8: Statement-Level Control Structures
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
JavaScript, Fourth Edition
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Flow of Control Chapter 3. Outline Branching Statements Java Loop Statements Programming with Loops The Type boolean.
CPS120 Introduction to Computer Science Iteration (Looping)
Loops cause a section of a program to be repeated a certain number of times. The repetition continues while a condition remains true. When a condition.
 Control Flow statements ◦ Selection statements ◦ Iteration statements ◦ Jump statements.
UMass Lowell Computer Science Java and Distributed Computing Prof. Karen Daniels Fall, 2000 Lecture 8 Java Fundamentals Control Structures Fri.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
Engineering Computing I Chapter 3 Control Flow. Chapter 3 - Control Flow The control-flow of a language specify the order in which computations are performed.
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Programmation impérative - Prof. Béat Hirsbrunner
Control Flow (Chapter 3)
Flow of Control.
Kontrola toka izvršenja
Flow of Control.
Flow of Control.
Conditional Statements
Flow of Control.
CSC215 Lecture Flow Control.
CSC215 Lecture Control Flow.
Statement-Level Control Structures
Flow of Control.
Homework Any Questions?.
2.6 The if/else Selection Structure
Program Flow.
Flow of Control.
CSC215 Lecture Control Flow.
Controlling Program Flow
Programming Language  C Control Flow
Presentation transcript:

0 Chap. 3 Control Flow 3.1 Statements and Blocks Imperative Programming, B. Hirsbrunner, diuf.unifr.ch/pai/ip Session 4, 3 April if, if … else … 3.3 if … else if … else … 3.4 switch 3.5 Loops: while and for 3.6 Loops: do 3.7 break and continue 3.8 goto and labels

1 3.1 Statements and Blocks Statement –An expression such as x=0 or i++ becomes a statement when it is followed by a semicolon, as in : x=0; i++; Compound statement or block –Braces { and } are used to group declarations and statements. –A block is syntactically equivalent to a single statement. Miscellaneous –There is no semicolon after the right brace that ends a block.

2 3.2 if [else], 3.3 else if Syntax if (expression) statement Shortcuts if (expression) is equivalent to if (expression != 0) if (expression) statement1 else statement2 if (expression1) statement1 else if (expression2) statement2 else if (expression3) statement3 else statement4 Nested if sequence –Because the else part is optional, there is an ambiguity when the else is omitted from a nested if sequence: if (n > 0) if (a > b) z = a; else z = b; if (n > 0) if (a > b) z = a; else z = b; –This is resolved by associating the else with the closest previous else -less if

3 /* binsearch: find x in v[0] <= v[1] <=... <= v[n-1] */ int binsearch(int x, int v[], int n) { int low, high, mid; low = 0; high = n - 1; while (low <= high) { mid = (low+high) / 2; if (x < v[mid]) high = mid - 1; else if (x > v[mid]) low = mid + 1; else/* found match */ return mid; } return -1;/* no match */ } An example lowmidhigh v x in v ?

4 3.4 Switch switch (expression) { case const-expr1: statements1 case const-expr2: statements2 case const-expr3: statements3 default : statements4 } Example while ((c = getchar()) != EOF) { switch (c) { case ‘0’: printf(“zero\n”); break; case ‘1’: printf(“one\n”); break; default : printf(“other\n”); break; }

5 3.5 Loops – while, for Syntax while (expression) statement for (expr1; expr2; expr3) statement Equivalence for (expr1; expr2; expr3) statement is equivalent to expr1; while (expr2) { statement expr3; } Typical use while ((c = getchar()) == ‘ ‘ || c == ‘\n’ || c == ‘\t’) … for (i = 0; i < n; i++) … Infinite loop (sic!) for (;;) ;

6 3.6 Loops – do Syntax do statement while (expression) Comma “,” operator for (i = 0, j = strlen(s) - 1; i < j; i++, j--) c = s[i], s[i]= s[j], s[j]= c; Remark A pair of expressions separated by a comma is evaluated left to right, and the type and value of the result are the type and value of the right operand.

7 3.7 break and continue, 3.8 goto and labels The break statement provides an early exit from for, while, do, and switch. The continue statement causes the next iteration of the enclosed for, while, or do loop. C provides the infinitely-abusable goto statement, and labels to branch to. A label has the same form as a variable name, and is followed by a colon. It can be attached to any statement in the same function as the goto. The scope of a label is the entire function.

8 Goto’s Typical Use int foo () {... for (... ) {... if (disaster) goto error; }... return... error: /* clean up the mess */ }

9 Goto’s Typical Use for (i = 0; i < n; i++) for (j = 0; j < m; j++) if (a[i] == b[j]) goto found; /* didn't find any common element */... found: /* got one: a[i] == b[j] */...

10 Goto is not necessary Code involving a goto can always be written without one, though perhaps at the price of some repeated tests or an extra variable. For example, the array search becomes: found = 0; for (i = 0; i < n && !found; i++) for (j = 0; j < m && !found; j++) if (a[i] == b[j]) found = 1; if (found) /* got one: a[i-1] == b[j-1] */... else /* didn't find any common element */...