Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session 10 21. October 2008.

Slides:



Advertisements
Similar presentations
Flow of Control Chapter 3.
Advertisements

Decision Structures - If / Else If / Else. Decisions Often we need to make decisions based on information that we receive. Often we need to make decisions.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Lecture 7: References and Assignment.
Pascal Programming Today Chapter 4 1 »Conditional statements allow the execution of one of a number of possible operations. »Conditional statements include:
Lecture 2 Introduction to C Programming
Introduction to C Programming
 2000 Prentice Hall, Inc. All rights reserved. Chapter 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Introduction to Computers and Programming Lecture 4: Mathematical Operators New York University.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
CS 106 Introduction to Computer Science I 02 / 12 / 2007 Instructor: Michael Eckmann.
Loops – While, Do, For Repetition Statements Introduction to Arrays
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
©The McGraw-Hill Companies, Inc. Permission required for reproduction or display. 4 th Ed Chapter Chapter 6 Repetition Statements.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 6.
Lecture Review (If-else Statement) if-else statement has the following syntax: if ( condition ) { statement1; } else { statement2; } The condition.
Copyright 2008 by Pearson Education 1 Building Java Programs Chapter 2 Lecture 2-1: Expressions and Variables reading:
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
UNIT II Decision Making And Branching Decision Making And Looping
Intro to Java Programming  A computer follows the instruction precisely and exactly.  Anything has to be declared and defined before it can be used.
© The McGraw-Hill Companies, 2006 Chapter 4 Implementing methods.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
1 Relational Expressions Relational expressions: –Expressions that compare operands –Sometimes called conditions –Evaluated to yield a result –Typically.
Sahar Mosleh California State University San MarcosPage 1 A for loop can contain multiple initialization actions separated with commas Caution must be.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. Chapter 2 Chapter 2 - Introduction to C Programming.
Making Decisions Chapter 5.  Thus far we have created classes and performed basic mathematical operations  Consider our ComputeArea.java program to.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Programming for Beginners Martin Nelson Elizabeth FitzGerald Lecture 2: Variables & Data Types.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 9.
© The McGraw-Hill Companies, 2006 Chapter 2 Selection.
CMPSC 16 Problem Solving with Computers I Spring 2014 Instructor: Lucas Bang Lecture 5: Introduction to C: More Control Flow.
CMP-MX21: Lecture 4 Selections Steve Hordley. Overview 1. The if-else selection in JAVA 2. More useful JAVA operators 4. Other selection constructs in.
Lecture #7 CONTROL STRUCTURE & FLOW CHARTS
CONTENTS Processing structures and commands Control structures – Sequence Sequence – Selection Selection – Iteration Iteration Naming conventions – File.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 5.
BEGINNING PROGRAMMING.  Literally – giving instructions to a computer so that it does what you want  Practically – using a programming language (such.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
School of Computer Science & Information Technology G6DICP - Lecture 4 Variables, data types & decision making.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Chair of Software Engineering Einführung in die Programmierung Introduction to Programming Prof. Dr. Bertrand Meyer Exercise Session 3.
Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008.
Lecture 4: Looping Tami Meredith. Back to Basics... Programming requires: 1. Identification of the problem. 2. Solving the problem: A. Selecting the data.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 2 - Introduction to C Programming Outline.
Midterm Exam Topics (Prof. Chang's section) CMSC 201.
Chapter 1 Java Programming Review. Introduction Java is platform-independent, meaning that you can write a program once and run it anywhere. Java programs.
Chad’s C++ Tutorial Demo Outline. 1. What is C++? C++ is an object-oriented programming (OOP) language that is viewed by many as the best language for.
CS100Lecture 21 Announcements For homework due Thursday, work alone -- do not work in pairs New class location: Olin 155 Office hour oops! Lyn: MW, 11:15-12:15.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture #8 SWITCH STATEMENT By Shahid Naseem (Lecturer)
1 Lecture 2 - Introduction to C Programming Outline 2.1Introduction 2.2A Simple C Program: Printing a Line of Text 2.3Another Simple C Program: Adding.
Bill Tucker Austin Community College COSC 1315
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Decisions Chapter 4.
Flow of Control.
User input We’ve seen how to use the standard output buffer
The following slides contain advanced material and are optional.
Control Structures: Selection Statement
Bools & Ifs.
3. Decision Structures Rocky K. C. Chang 19 September 2018
SELECTIONS STATEMENTS
Einführung in die Programmierung Introduction to Programming Prof. Dr
Control Structures: Selection Statement
Presentation transcript:

Chair of Software Engineering 1 Introduction to Programming Bertrand Meyer Exercise Session October 2008

Chair of Software Engineering Today’s learning goals  Control structures: conditional 2

Chair of Software Engineering Inside the routine body  The body of each routine consists of instructions (command calls, creations, assignments, etc.)  In the programs you’ve seen so far they were always executed in the order they were written create passenger.make_with_route (Route3, 1.5) passenger.go passenger. set_reiterate (True) Paris.put_passenger (passenger) create tram.make_with_line (Line1) tram.start Paris.put_tram (tram) 3  Programming languages have structures that allow you to change the order of execution

Chair of Software Engineering Structured programming  If the order of execution could be changed arbitrarily, it would be hard to understand programs  In structured programming instructions can be combined only in three ways: 4 i_1 c i_2 i_1i_2 c i compound conditionalloop TrueFalse True  Each of these blocks has a single entrance and exit and is itself an instruction Instruction Condition

Chair of Software Engineering Conditional  Basic syntax: if c then i_1 else i_2 end  c should be a boolean expression (e.g., entity, query call of type BOOLEAN)  else-part is optional: if c then i_1 end Condition Instruction

Chair of Software Engineering Compilation error? Runtime error? (1) f (x, y: INTEGER): INTEGER do if (x / y) then 1 else 0 end Hands-On Compilation error: integer expression instead of boolean Compilation error: expression instead of instruction

Chair of Software Engineering Compilation error? Runtime error? (2) f (x, y: INTEGER): INTEGER do if (False) then Result := x // y end if (x /= 0) then Result := y // x end Hands-On Everything is OK (during both compilation and runtime)

Chair of Software Engineering Calculating function’s value f (max: INTEGER; s: STRING): STRING do if s.is_equal (“Java”) then Result := “J**a” else if s.count > max then Result := “ ” end  Calculate the value of:  f (3, “Java”)  f (20, “Immatrikulationsbestätigung”)  f (6, “Eiffel”) → “J**a” → “ ” → Void Hands-On

Chair of Software Engineering What does this routine do? f (x: REAL): REAL do if (x >= 0) then Result := x else Result := -x end abs Hands-On

Chair of Software Engineering Write a routine... ... that computes the maximum of two integers: max (a, b: INTEGER): INTEGER ... that increases time by one second inside class TIME: class TIME hour, minute, second: INTEGER second_forth do... end... end Hands-On

Chair of Software Engineering Comb-like conditional  If there are more than two alternatives, you can use the syntax: if c1 then i_1 elseif c2 then i_2... elseif c_n then i_n else i_e end instead of: if c_1 then i_1 else if c_2 then i_2 else... if c_n then i_n else i_e end... end Condition Instruction

Chair of Software Engineering Multiple choice  If all the conditions have a specific structure, you can use the syntax: inspect expression when const_1 then i_1 when const_2 then i_2... when const_n1.. const_n2 then i_n else i_e end Integer or character expression Integer or character constant Instruction Interval

Chair of Software Engineering Lost in conditions  Rewrite the following multiple choice:  using a comb-like conditional  using nested conditionals inspect x \\ 2 when 0 then print (“x is even”) when 1 then print (“x is odd”) else print (“can never happen”) end Hands-On modulo (remainder after division)