Flow of Control is Sequential unless a “control structure” is used to change that there are 2 general types of control structures: Selection (also called.

Slides:



Advertisements
Similar presentations
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Advertisements

ICS103: Programming in C 4: Selection Structures Muhamed F. Mudawar.
Chapter 4: Making Decisions.
Dale Roberts Department of Computer and Information Science, School of Science, IUPUI Dale Roberts, Lecturer Computer Science, IUPUI
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Control Structures 4 Control structures control the flow of execution of a program 4 The categories of control structures are: –Sequence –Selection –Repetition.
1 Chapter 5 Branching and Method Algorithm Design.
true (any other value but zero) false (zero) expression Statement 2
COMP 14 Introduction to Programming Miguel A. Otaduy May 18, 2004.
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 Lecture 7:Control Structures I (Selection) Introduction to Computer Science Spring 2006.
CS 117 Spring 2002 Review for Exam 2 March 6, 2002 open book, 1 page of notes.
Conditions, logical expressions, and selection Introduction to control structures.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
1 Chapter 4 Selection and Encapsulation. 2 Chapter 4 Topics l Java Control Structures l boolean Data Type l Using Relational and Logical Operators in.
CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems/Headington.
C++ Control Flow Mark Hennessy Dept. Computer Science NUI Maynooth C++ Workshop 18 th -22 nd Sept 2006.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures.
Conditions, Logical Expressions, and Selection Control Structures Sumber dari :
Chapter 4 Logical Expressions & If-Else. 2 Overview  More on Data Type bool u Using Relational & Logical Operators to Construct & Evaluate Logical Expressions.
1 Conditions, logical expressions, and selection Introduction to control structures.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
1 CSC 1401 S1 Computer Programming I Hamid Harroud School of Science and Engineering, Akhawayn University
1 Conditional statements Dept. of Computer Engineering Faculty of Engineering, Kasetsart University Bangkok, Thailand.
CSCI 3328 Object Oriented Programming in C# Chapter 5: C# Control Statement – Part II UTPA – Fall
Logical Operators Jumps Logical Operators The different logical operators found in Java Rational Operators The different rational operators found in Java.
Flow of Control There are 5 general types of Java control structures: Sequence (by default) Selection (also called branch or decision) Loop (also called.
Chapter 3 More Flow Of Control.
Lecture no 3 Control statements.
Selection Boolean What is Boolean ? Boolean is a set with only two values : –true –false true and false are standard identifiers in Pascal, called Boolean.
COSC175-Selection1 Decisions Given hours worked and pay rate, calculate total pay What if you work overtime? How do you indicate if your work overtime?
Computer Science 1620 boolean. Types so far: Integer char, short, int, long Floating Point float, double, long double String sequence of chars.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
1 Programming in C++ Dale/Weems/Headington Chapter 5 Conditions, Logical Expressions.
Chapter 4 Controlling Execution CSE Objectives Evaluate logical expressions –Boolean –Relational Change the flow of execution –Diagrams (e.g.,
Chapter 5 Conditions, Logical Expressions, and Selection Control Structures Dale/Weems.
Boolean Data Lesson CS1313 Fall Boolean Data Outline 1.Boolean Data Outline 2.Data Types 3.C Boolean Data Type: char or int 4.C Built-In Boolean.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
ICT Introduction to Programming Chapter 4 – Control Structures I.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
1 Expressions. 2 Variables and constants linked with operators  Arithmetic expressions Uses arithmetic operators Can evaluate to any value  Logical.
1 Conditions, Logical Expressions, and Selection Control Structures.
Control statements Mostafa Abdallah
TestScore < 80 testScore * 2 >= < w / (h * h) x + y != 2 * (a + b) 2 * Math.PI * radius
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
Control Statements: Part1  if, if…else, switch 1.
Dr. Sajib Datta Jan 21,  Declare a variable ◦ int height; [note that no value is still assigned]  Assign a variable a value ◦ height =
CC213 Programming Applications Week #2 2 Control Structures Control structures –control the flow of execution in a program or function. Three basic control.
Chapter 5 Topics Data Type bool
Decisions Given hours worked and pay rate, calculate total pay
Flow of Control.
Decisions Given hours worked and pay rate, calculate total pay
Flow of Control.
Topics Data Type bool Using Relational and Logical Operators to Construct and Evaluate Logical Expressions If-Then-Else Statements If-Then Statements Nested.
Flow of Control.
Summary Two basic concepts: variables and assignments Basic types:
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Relational Operators.
ENERGY 211 / CME 211 Lecture 5 October 1, 2008.
Algebra: Variables and Expressions
Control Structure.
The boolean type and boolean operators
Presentation transcript:

Flow of Control is Sequential unless a “control structure” is used to change that there are 2 general types of control structures: Selection (also called branching) Repetition (also called looping)

bool Data Type type bool is a built-in type consisting of just 2 values, the constants true and false we can declare variables of type bool bool hasFever; // true if has high temperature bool isSenior; // true if age is at least 55

C++ control structures Selection  Single selection if  double selection if... else  multiple selection switch

Control Structures use logical expressions which may include: 6 Relational Operators >= == != 3 Logical Operators !&&||

are used in expressions of form: ExpressionA Operator ExpressionB temperature > humidity B * B * A * C > 0.0 abs (number ) == 35 initial ! = ‘Q’ 6 Relational Operators

int x, y ; x = 4; y = 6; EXPRESSIONVALUE x < y true x + 2 < y false x != y true x + 3 >= y true y == x false y == x+2 true y = x (true)

In C++ the value 0 represents false ANY non-zero value represents true

OperatorMeaning Associativity ! NOTRight *, /, % Multiplication, Division, Modulus Left +, - Addition, SubtractionLeft < Less thanLeft <= Less than or equal toLeft > Greater thanLeft >= Greater than or equal toLeft == Is equal toLeft != Is not equal to Left && ANDLeft || OR Left = AssignmentRight 8

LOGICAL EXPRESSION MEANINGDESCRIPTION ! p NOT p ! p is false if p is true ! p is true if p is false p && q p AND q p && q is true if both p and q are true. It is false otherwise. p || q p OR qp || q is true if either p or q or both are true. It is false otherwise.

int age ; bool isSenior, hasFever ; float temperature ; age = 20; temperature = ; isSenior = (age >= 55) ; // isSenior is false hasFever = (temperature > 98.6) ; // hasFever is true EXPRESSIONVALUE isSenior && hasFever false isSenior || hasFever true ! isSeniortrue ! hasFeverfalse

What is the value? int age, height; age = 25; height = 70; EXPRESSIONVALUE ! (age < 10) ? ! (height > 60) ?

“Short-Circuit” Evaluation C++ uses short circuit evaluation of logical expressions this means logical expressions are evaluated left to right and evaluation stops as soon as the final truth value can be determined

Short-Circuit Example int age, height; age = 25; height = 70; EXPRESSION (age > 50) && (height > 60) false Evaluation can stop now because result of && is only true when both sides are true. It is already determined that the entire expression will be false.

More Short-Circuiting int age, height; age = 25; height = 70; EXPRESSION (height > 60) || (age > 40) true Evaluation can stop now because result of || is true if one side is true. It is already determined that the entire expression will be true.

What happens? int age, weight; age = 25; weight = 145; EXPRESSION (weight = 20) true Must still be evaluated because truth value of entire expression is not yet known. Why? Result of && is only true if both sides are true.

What happens? int age, height; age = 25; height = 70; EXPRESSION ! (height > 60) || (age > 50) true false Does this part need to be evaluated?

Write an expression for each taxRate is over 25% and income is less than $20000 temperature is less than or equal to 75 or humidity is less than 70% age is over 21 and age is less than 60 age is 21 or 22

Some Answers (taxRate >.25) && (income < 20000) (temperature <= 75) || (humidity <.70) (age > 21) && (age < 60) (age == 21) || (age == 22)