Engineering 1020 Introduction to Programming Peter King Winter 2010.

Slides:



Advertisements
Similar presentations
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.
Advertisements

Selection (decision) control structure Learning objective
James Tam Making Decisions In Python In this section of notes you will learn how to have your Pascal programs choose between alternative courses of action.
July 13 th.  If/ Else if / Else  Variable Scope  Nested if/else's  Switch statements  Conditional Operator.
1 9/29/06CS150 Introduction to Computer Science 1 Loops Section Page 255.
1 9/26/08CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Iteration This week we will learn how to use iteration in C++ Iteration is the repetition of a statement or block of statements in a program. C++ has three.
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements Sections 1.25, Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
The If/Else Statement, Boolean Flags, and Menus Page 180
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
1 9/28/07CS150 Introduction to Computer Science 1 Logical Operators and if/else statement.
Programming: Simple Control Structures Alice. Control Statements We have been using Do in order and Do together to control the way instructions are executed.
CONTROL FLOW IN C++ Satish Mishra PGT CS KV Trimulgherry.
Engineering 1020 Introduction to Programming Peter King Winter 2010.
CHAPTER 4: CONDITIONAL STRUCTURES Introduction to Computer Science Using Ruby (c) 2012 Ophir Frieder et al.
Chapter 4: Decision Making with Control Structures and Statements JavaScript - Introductory.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
COMPUTER PROGRAMMING. Control Structures A program is usually not limited to a linear sequence of instructions. During its process it may repeat code.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Chapter 4 Selection Structures: Making Decisions.
1. We’ve learned that our programs are read by the compiler in order, from top to bottom, just as they are written The order of statement execution is.
CSE1222: Lecture 6The Ohio State University1. Common Mistakes with Conditions (1)  Consider the following code: int age(26); if (age = 18) { cout
6/3/2016 CSI Chapter 02 1 Introduction of Flow of Control There are times when you need to vary the way your program executes based on given input.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Decision Making CMSC 201. Overview Today we will learn about: Boolean expressions Decision making.
Chapter 3: Branching and Program Flow CSCI-UA 0002 – Introduction to Computer Programming Mr. Joel Kemp.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Lecture 2 Conditional Statement. chcslonline.org Conditional Statements in PHP Conditional Statements are used for decision making. Different actions.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Chapter 15 JavaScript: Part III The Web Warrior Guide to Web Design Technologies.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
1 CS161 Introduction to Computer Science Topic #8.
Decision Making and Branching (cont.)
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Lecture 2 Control Structures: Part 1 Selection: else / if and switch.
Variables  A piece of memory set aside to store data  When declared, the memory is given a name  by using the name, we can access the data that sits.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
Decision Making and Branching
Flow Control in Imperative Languages. Activity 1 What does the word: ‘Imperative’ mean? 5mins …having CONTROL and ORDER!
Chapter 3. Control Structure C++ Programing. Conditional structure C++ programming language provides following types of decision making statements. Click.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Branching statements.
More on the Selection Structure
Bill Tucker Austin Community College COSC 1315
Variables A piece of memory set aside to store data
Programming Fundamentals
Selection CIS 40 – Introduction to Programming in Python
Conditional Statements
IF if (condition) { Process… }
Scratch: selection / branching/ if / If…else / compound conditionals / error trapping by Mr. Clausen.
Control Structures: Selection Statement
Programming: Simple Control Structures
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
There many situations comes in real life when we need to make some decisions and based on these decisions, we decide what should we do next. Similar situations.
Control Structures: Selection Statement
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

Engineering 1020 Introduction to Programming Peter King Winter 2010

ENGI 1020: Update Midterm –Date: Feb 17 th (unchanged) –Time: 7pm to 8:15pm This is the same as all other core courses –Location: To Be Announced

ENGI 1020: Control Statements Up to now all our programming has followed the following flow: –Start program at main –Execute each instruction once –For instructions that are function calls, go to the function declaration and start at first instruction –When instruction is finished, go to next instruction

ENGI 1020: Control Statements How can we solve problems like –Given two numbers, return the largest one –Find the square root of a number, but only when it's greater than zero –Given a person's age, output whether they are a child, teenager, adult, or senior –Given the distance to a wall, tell a robot to stop when it is less than 4m from a wall

ENGI 1020: Control Statements We need to enable the program to make “decisions” Or more formally –Depending on some condition(s), execute alternative sections of code At some point in the code, we will choose to execute one block, instead of another

ENGI 1020: Control Statements How is this done you ask? The If statement –“if the door is locked, I will get a key” –“if traffic is bad, I will walk to work” –“if it's later than 9pm, I will go home”

ENGI 1020: Control Statements You've all probably seen this: Check Bank Account Have more than $10k? Buy CarGet Job

ENGI 1020: Control Statements This is an if statement Depending on some condition, we will take a particular path Have more than $10k?

ENGI 1020: Control Statements Let's see it in C++ If (some condition) do something if (some condition){ do something … do something }

ENGI 1020: Control Statements Example if (x > 0) cout << “x is positive.” << endl; if (x < 0){ cout << “ x is negative.” cout << endl; }

ENGI 1020: Control Statements The if is a keyword The ( condition ) is an expressions that is evaluated as either true or false –x > 0 –y != 5 –z == 2*y When the condition is true, the statement (or block of statements) are executed If not true, then the statements are ignored

ENGI 1020: Control Statements Lets look at the conditions –They are boolean expressions –They are evaluated to either true or false –We can utilize multiple conditions using the && → and operator || → or operator –If x is greater than 5 and y is less than 2, proceed If (x > 5 && y < 2) proceed();

ENGI 1020: Control Statements What if we want to select one or the other statements, based on a single condition?  “IF there is any 7-up, I'll have that, else I'll have a Sprite”

ENGI 1020: Control Statements The if-else statement  Picks between two alternatives if (x >0)‏ Cout << “x is positive” << endl; else Cout << “x is negative” << endl;

ENGI 1020: Control Statements Or  Since we know only one of the statements will get executed if (x >0)‏ Cout << “x is positive”; else Cout << “x is negative”; cout << endl;

ENGI 1020: Control Statements If the condition is true  We execute under the if If the condition is not true (false)‏  We execute under the else

ENGI 1020: Control Statements We can also nest our if statements  What does that mean? If time is later than 12pm and earlier than 1pm, eat lunch if (time > 12){ If (time < 13){ eatLunch(); }

ENGI 1020: Control Statements Statement blocks after the if can contain any valid code, even other if statements

ENGI 1020: Control Statements One more variation  Instead of doing this if (x < 1)‏ doThis()‏; else if(x <2)‏ DoThat()‏; else if(x <3)‏ DoSomething();

ENGI 1020: Control Statements We can do this if (x < 1)‏ dothis(); else if(x < 2)‏ doThat(); else if(x < 3)‏ doSomthing(); else doNothing();

ENGI 1020: Control Statements Grading Examples