Flow Control Statements

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

Control Structures.
If Statements & Relational Operators Programming.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
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,
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 4 Selection Structures: Making Decisions.
CONDITIONAL STATEMENTS OVERVIEW.  Many times we want programs to make decisions  What drink should we dispense from the vending machine?  Should we.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
 Learn about control structures  Examine relational and logical operators  Explore how to form and evaluate logical (Boolean) expressions  Learn how.
1 2. Program Construction in Java. 2.4 Selection (decisions)
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
Chapter 4 Selection: the if-else and switch-case instructions.
1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.
© M. Gross, ETH Zürich, 2014 Informatik I für D-MAVT (FS 2014) Exercise 4 – Logical Operators & Branching.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Decisions. Three Forms of Decision Making in Java if statements (test a boolean expression) switch statements (test an integer expression) conditional.
Chapter#3 Part1 Structured Program Development in C++
Programming Fundamentals1 Chapter 4 SELECTION STRUCTURES.
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
Fundamental Programming Fundamental Programming More Expressions and Data Types.
BOOLEAN OPERATIONS AND CONDITIONALS CHAPTER 20 1.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
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.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Chapter 3 Selection Statements
Chapter 4: Control Structures I
Chapter 3 Control Statements
Selection (also known as Branching) Jumail Bin Taliba by
Chapter 4: Control Structures I
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.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
Variables A piece of memory set aside to store data
Programming Fundamentals
Expressions and Control Flow in JavaScript
Chapter 4: Control Structures I
Expression Review what is the result and type of these expressions?
Chapter 7 Conditional Statements
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Control Structure Chapter 3.
Chapter 3: Selection Structures: Making Decisions
Fundamental Programming
Life is Full of Alternatives
Chapter 3: Selection Structures: Making Decisions
Decisions, decisions, decisions
Life is Full of Alternatives
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Control Structure.
Presentation transcript:

Flow Control Statements Change the natural flow of a program from statement to statement. Two types: Decision-making: make a decision to either execute or not execute a set of code. Repetition: make a decision to execute a set of code again.

Boolean Expressions A program makes a decision using a Boolean expression that evaluates to either true or false. Boolean expressions are formed with relational and logical operators.

Relational Operators == Equal to != Not equal to > Greater than < Less than >= Greater than or equal to <= Less than or equal to Make sure they understand relational expressions involving chars.

Logical Operators Allow the combination of simple boolean expressions to make complex expressions. && Boolean And || Boolean Or ! Boolean Not And and Or truth tables Examples on board

if ( boolean expression ) statement; double hours, otHours = 0; cout << "Enter number of hours worked: "; cin >> hours; if( hours > 40 ) otHours = hours – 40; cout << "You worked " << otHours << " overtime hours.";

if ( boolean expression ) { statement1; statement2; statement3; etc… } double hours, otHours = 0; cout << "Enter number of hours worked: "; cin >> hours; if( hours > 40 ) { otHours = hours – 40; cout << "You worked " << otHours << " overtime hours." << endl; } HANDOUT – ifDemo.cpp

if( boolean expression ) statement(s) else int years; cout << "Enter your age in whole years: "; cin >> years; if( years >= 65 ) { cout << "You’re qualified for Social Security."; cout << "My, you’re old." << endl; } else cout << "You’re not that old." << endl; cout << "Have a nice day." << endl; HANDOUT – paycalc.cpp

if(action == 'd') if(amount > 0) balance += amount; else cout << "You have to deposit something." << endl; if(balance >= amount) balance -= amount; cout << "Account is OVERDRAWN." << endl; if( x == 7 ) if( y == 10 ) cout << "Both conditions are true." << endl; if( x == 7 && y == 10 )

cout << "Both conditions are true."; else if( x == 7 && y == 10 ) cout << "Both conditions are true."; else cout << "One or both of the conditions are false."; if( x == 7 ) if( y == 10 ) cout << "Second condition is false."; cout << "First condition is false."); HANDOUT - lettergrade.cpp

bool eligible, survivor; int age; . . . eligible = false; if(survivor == false) if(age > 65) eligible = true; else bool eligible, survivor; int age; . . . eligible = false; if(survivor == false) if(age > 65) eligible = true; else The left is what I want The right is what the compiler sees. How to fix it?

bool eligible, survivor; int age; . . . eligible = false; if(survivor == false) { if(age > 65) eligible = true; } else An if in one statement block can't be matched to an else in another statement block. So enclose the offending if in it's own block

switch( integral expression ) { case <literal1>: statement1; break; case <literal2>: statement3; statement4; default: statement5; statement6; } switch is the only flow control statement that doesn't use Boolean expression to make decision. Can't switch on a floating point value, or a string. If there is no default case, and the value of the switch expression doesn't match one of the cases, nothing executes. HANDOUT - calculator.cpp calculator_fixed.cpp payroll.cpp