CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4.

Slides:



Advertisements
Similar presentations
Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
Advertisements

Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
Chapter 5 Selection Statements. Topics Controlling program flow selection –if –switch Boolean expressions –boolean primitive type –comparison operators.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
5-1 Flow of Control Recitation-01/25/2008  CS 180  Department of Computer Science  Purdue University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
true (any other value but zero) false (zero) expression Statement 2
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,
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Slide 4- 1.
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.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
Logical Operators and Conditional statements
Tutorial 4 Decision Making with Control Structures and Statements Section A - Decision Making JavaScript Tutorial 4 -Decision Making with Control.
Chapter 4 Making Decisions
Liang, Introduction to Java Programming, Sixth Edition, (c) 2007 Pearson Education, Inc. All rights reserved Control Statements.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
The switch Statement, DecimalFormat, and Introduction to Looping
CSci 125 Lecture 10 Martin van Bommel. Simple Statements Expression followed by semicolon Assignments total = n1 + n2; Function calls printf(”Hello.\n”);
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
Decision Structures and Boolean Logic
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
Flow of Control Part 1: Selection
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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.
© 2000 Scott S Albert Selection Structures Structured Programming 256 Chapter 4.
ㅎㅎ logical operator if if else switch while do while for Third step for Learning C++ Programming Repetition Control Structures.
Decisions in Python Bools and simple if statements.
Alternate Version of STARTING OUT WITH C++ 4 th Edition Chapter 4 Making Decisions.
Review the following: if-else One branch if Conditional operators Logical operators Switch statement Conditional expression operator Nested ifs if –else.
4.1 Object Operations operators Dot (. ) and new operate on objects The assignment operator ( = ) Arithmetic operators + - * / % Unary operators.
Copyright 2003 Scott/Jones Publishing Making Decisions.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
3. Controlling Program Flow Methods, parameters, and return values Boolean expressions Conditional branching Loops.
Control Structures - Selections - Repetitions/iterations (part 2) 1 -Based on slides from Deitel & Associates, Inc. - Revised by T. A. Yang.
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.
Chapter Making Decisions 4. Relational Operators 4.1.
Chapter 4 Control Structures I. Chapter Objectives Learn about control structures Examine relational and logical operators Explore how to form and evaluate.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Control statements Mostafa Abdallah
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 2: Fundamental Programming Structures in Java Adapted from MIT AITI Slides Control Structures.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Rational Expressions relational operators logical operators order of precedence.
Lesson #4 Logical Operators and Selection Statements.
Lesson #4 Logical Operators and Selection Statements.
Chapter 3 Control Statements
Selections Java.
The switch Statement, and Introduction to Looping
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
Topics The if Statement The if-else Statement Comparing Strings
Relational Operators Operator Meaning < Less than > Greater than
Flow Control Statements
Chapter 4: Boolean Expressions, Making Decisions, and Disk Input and Output Prof. Salim Arfaoui.
Computer Programming Basics
CprE 185: Intro to Problem Solving (using C)
Presentation transcript:

CS 117 Spring 2002 Decision Making Hanly Chapter 3 Friedman-Koffman Chapter 4

Flow control Three types of program flow sequential (what we’ve done so far) selection (Chapter 3) –if - else –switch repetition (Chapter 4) –while –do - while –for

Boolean variables bool type - variables that can be either true or false For regular variables, a zero value is considered to be false, anything else is true

Boolean operators unary !logical NOT binary operators &&logical and ||logical or p!p TF FT

Truth Table pqp && qp || q TTTT TFFT FTFT FFFF

Short-circuit evaluation -if the result is uniquely determined by the value of the first operand, the second won’t be evaluated. –p && qhas to be false if p is false –p || qis always true if p is true

Boolean Operators comparison operators <less than <=less than or equal >=greater than or equal > greater than ==equal !=not equal

bitwise operators (we won’t use these) ^complement &and |or

Precedence revisited function calls ( )use to force the desired order !unary - (negation) */ % +-(binary) => ==!= && || =(also+=-=*=/=etc)

Selection Statements These provide a way to select different paths through the code –if - else –switch

Examples of Boolean Expressions p !p p || q !(p && q) && p||q x < y 0 <=x && x <= 100 a + b == c

Cautions a==b is different from a=b using == with double variables is not recommended - test the magnitude of the difference 0 <= x <= 100 is not what you'd expect from math

When do you need selection some operations need only be done under certain conditions –you can't withdraw more money than your bank account holds - it doesn’t make sense to have a negative balance sometimes things are done differently in different ranges of a variable –you are taxed differently in different ranges of income –piecewise functions are calculated differently in different regions a program with a menu has to do different things depending on what is selected

execute code sometimes

if statement if (condition) thenDoThis; thisAlwaysDone; condition is a boolean expression First statement after if is done if condition is true to execute multiple statements, surround them with { }

different code at different times

if..else if (condition) thenDoThis; else doThat; thisAlwaysDone; body of if and else one statement unless { } used

Nested if statements if (condition) { if (condition2) thenDoThis; else doThat; } else doTheOther; thisAlwaysDone;

Nested if caution in the absence of { }, an else always goes with the nearest if. if (cond1) if (cond2) thenDoThis; else doTheOther; // done if cond1 true && cond2 false thisAlwaysDone;

Multiple if statements Sometimes there are more than two cases. if (condition) thenDoThis; else if (condition2) doThat; else if (condition3) … else doInAllOtherCases; thisAlwaysDone;

switch statement another selection statement allows you to select between several discrete values of a variable Use this only with enumerable types (int, char)

format of switch switch (variable) { case value1: action1: break; case value2: action2; break; default:// if no other case holds default action; }

switch statement The first case for which variable has the value given in the case is executed break forces exit from the switch statement; without it, execution falls through to next case

if vs switch ranges of values any type –boolean expressions –double –char –int discrete values enumerable types –int –char