Programmation impérative - Prof. Béat Hirsbrunner

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 * (
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.
CSCI 330: Programming Language Concepts Instructor: Pranava K. Jha Control Flow-II: Execution Order.
Control Structures Any mechanism that departs from straight-line execution: –Selection: if-statements –Multiway-selection: case statements –Unbounded iteration:
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
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.
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.
COMP1180 Review Date: 4 March, 2009 Time: 10:30am - 12:20pm Venue: –CS students -- FSC801C and FSC801D –IS and other students -- OEE1017 Remarks: – 1)
Control Flow C and Data Structures Baojian Hua
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
 2006 Pearson Education, Inc. All rights reserved Control Statements: Part 2.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Chapter 8 (Control Structure) Slide 1 Control Structures Control structures are used by the programmer to incorporate the desired sequence of execution.
1 Chapter 3 Flow of Control. 2 Outline  How to specify conditions?  Relational, Equality and Logical Operators  Statements  Statements: compound statement.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 Lecture 5  More flow control structures  for  do  continue  break  switch  Structured programming  Common programming errors and tips  Readings:
CSM-Java Programming-I Spring,2005 Control Flow Lesson - 3.
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.
Algorithms and Computing Lecture 3 Control Statements By Dr. M. Tahir Khaleeq.
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
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”
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.
Chapter 3 Functions, Events, and Control Structures JavaScript, Third Edition.
Control Structures sequence of execution of high-level statements.
Java™ How to Program, Early Objects Version, 8/e © by Pearson Education, Inc. All Rights Reserved.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
 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.
CS113 Introduction to C Instructor: Ioannis A. Vetsikas Lecture 2 : August 28 webpage:
Chapter 8 Statement-Level Control Structures. Copyright © 2012 Addison-Wesley. All rights reserved.1-2 Chapter 8 Topics Introduction Selection Statements.
Chapter 3 of Programming Languages by Ravi Sethi
Def: A control structure is a control statement and
Control Flow (Chapter 3)
8.1 Introduction - Levels of Control Flow: 1. Within expressions
Flow of Control.
Chapter 3 Loops Section 3.3 Slides prepared by Rose Williams, Binghamton University Kenrick Mock, University of Alaska Anchorage.
CiS 260: App Dev I Chapter 4: Control Structures II.
Java Programming: Guided Learning with Early Objects
Chapter 8: Control Structures
Flow of Control.
Flow of Control.
Conditional Statements
CS1100 Computational Engineering
CSCI 3328 Object Oriented Programming in C# Chapter 4: C# Control Statement – Part I – Exercises UTPA – Fall 2012 This set of slides is revised from lecture.
The University of Texas – Pan American
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:

Programmation impérative - Prof. Béat Hirsbrunner Chap. 3 Control Flow 3.1 Statements and Blocks 3.2 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 System-oriented Programming, B. Hirsbrunner, diuf.unifr.ch/pai/sp, Lecture 3 – 8 March 2016 KR – Chap. 3: ~30’; Tutorials: AST for control flow structures: 10'; UNIT Testing: ~25’ + gdb: ~25' +; Exercises: ~90’

Programmation impérative - Prof. Béat Hirsbrunner 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.

Programmation impérative - Prof. Béat Hirsbrunner 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 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; This is resolved by associating the else with the closest previous else-less if if (expression1) statement1 else if (expression2) statement2 else if (expression3) statement3 else statement4

Programmation impérative - Prof. Béat Hirsbrunner 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; }

Programmation impérative - Prof. Béat Hirsbrunner 3.5 Loops – while, for Syntax while (expression) statement for (e1_opt; e2_opt; e3_opt) Equivalence for (expr1; expr2; expr3) statement is equivalent to (except if statement contains a continue statement): expr1; while (expr2) { expr3; } The postfix _opt indicates that the expression is optional Infinite loop (sic!) for (;;) ; Typical use while ((c = getchar()) == ‘ ‘ || c == ‘\n’ || c == ‘\t’) … for (i = 0; i < n; i++)

Programmation impérative - Prof. Béat Hirsbrunner 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 ones of the last evaluated expression, e.g. in the above example the ones of the expression s[j] = c

3.7 break and continue, 3.8 goto and labels Programmation impérative - Prof. Béat Hirsbrunner été 2002 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. Note: break and continue operate on the closest enclosing loop or switch 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.

Programmation impérative - Prof. Béat Hirsbrunner Goto’s Typical Use int foo () { ... for ( ... ) { if (disaster) goto error; } return ... error: /* clean up the mess */

Programmation impérative - Prof. Béat Hirsbrunner 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] */

Programmation impérative - Prof. Béat Hirsbrunner 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 */