If Statements Sections 1.25, 2.4-2.6. Control Structures o All code thus far executes every line of code sequentially o We want to be able to repeat,

Slides:



Advertisements
Similar presentations
CS0007: Introduction to Computer Programming
Advertisements

Control Structures Control structures are used to manage the order in which statements in computer programs will be executed Three different approaches.
If Statements & Relational Operators Programming.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 CS 105 Lecture 4 Selection Statements Wed, Jan 26, 2011, 6:05 pm.
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
The If/Else Statement, Boolean Flags, and Menus Page 180
If statements Chapter 3. Selection Want to be able to do a statement sometimes, but not others if it is raining, wear a raincoat. Start first with how.
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
True or False Unit 3 Lesson 7 Building Blocks of Decision Making With Additions & Modifications by Mr. Dave Clausen True or False.
EGR 2261 Unit 4 Control Structures I: Selection  Read Malik, Chapter 4.  Homework #4 and Lab #4 due next week.  Quiz next week.
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.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Decision II. CSCE 1062 Outline  Boolean expressions  switch statement (section 4.8)
5-1 Repetition Statements Repetition statements allow us to execute a statement multiple times Often they are referred to as loops Like conditional statements,
CONTROLLING PROGRAM FLOW
Making Decisions. 4.1 Relational Operators Used to compare numbers to determine relative order Operators: > Greater than < Less than >= Greater than.
CPS120: Introduction to Computer Science Decision Making in Programs.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
CSCI 161 Lecture 7 Martin van Bommel. Control Statements Statements that affect the sequence of execution of other statements Normal is sequential May.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Statements, Short- Circuit Evaluation, Errors.
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.
CONTROL STRUCTURES (SELECTION). PROGRAM COMPONENTS  SEQUENCE  Groups Of Sequential Steps  SELECTION  Making Choices  IF-THEN (one way)  IF-THEN-ELSE.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
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 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
IST 210: PHP Logic IST 210: Organization of Data IST2101.
The Ohio State University
Chapter 3 Selection Statements
Chapter 4: Making Decisions.
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.
Chapter 4: Making Decisions.
EGR 2261 Unit 4 Control Structures I: Selection
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Topics The if Statement The if-else Statement Comparing Strings
Bools & Ifs.
Topics The if Statement The if-else Statement Comparing Strings
Chapter 4: Control Structures I (Selection)
Bools & Ifs.
3 Control Statements:.
Logical Operations In Matlab.
Selection Control Structure
Summary Two basic concepts: variables and assignments Basic types:
Computer Science Core Concepts
Chapter 4: Control Structures I (Selection)
Understanding Conditions
Using Decision Structures
Decisions, decisions, decisions
Presentation transcript:

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, to choose some lines of code o Three types of control: sequence, conditional, repetition/iteration

Types of control structures Sequence – default in C++, execute each instruction sequentially as it reached Conditional – choose whether to execute some C++ statement (if, if - else switch) Iteration – loop. Repeat some set of statements multiple times

Conditional Choose which statement to execute. Form: if (some condition is true), then do some action If (comparison) then (action) Many examples in English: If raining, wear raincoat. If cold, wear winter coat. When in Rome, do as the Romans (if in Rome, act like a Roman)

Conditionals in C++ If (comparison) then (action) Need a way to write the comparison and the action Comparison is boolean expression (evaluates to be true or false) Action is any (series of) C++ statements

Comparisons Boolean expressions can have many different forms: Using bool variables bool done=true; if (done) // then do some action Relational expression (compares values) Logical Expression (manipulates values with and, or, not) Can be combined

Relational Operators int x, y; x < y x <= y x > y x >= y x != y x == y // NOT x=y

Evaluating Boolean Expressions 0 is false Non-zero is true true, false int x=3; x is true. sometimes useful, but can cause problems x=3 is NOT comparing x and 3.

int x=3; int y = 4; int z=5; x < y x < y < z x = y y == 4 z >= x x != 3 (x + 4) < (y-1) Evaluating Boolean Expressions

int x=3; int y = 4; int z=5; x < y x < y < z x = y y == 4 z >= x x != 3 (x + 4) < (y-1) true true. x is now 4, 4 is nonzero, so this is true true false 7 < 3 false

Logical Operators and (&&, single & very different) both values must be true for the expression to be true if it is cold and rainy, wear your winter raincoat (if either isn't true, don't) or (|| - on keyboard, called pipe symbol) either value can be true if it is cold or rainy, wear a coat (if one is true, do) not (!) changes the truth value of the expression if it is not cold, do not wear a winter coat

Logical Operators int x=3; int y=10; (x < y) && (y < 20) (x == 3) || (y == 3) x < y; 3 < 10; true y < 20; 10 < 20; true true && true is true x == 3 true. short circuit evaluation (y==3 false true || false is true)

More logical operators int x=3; int y=10; !(y=10) (x != 3) || (y != 3) trick question y=10 is 10 (true); !true is false false x != 3 false y != 3 true false || true is true

Yet more logical operators int x=3; int y=10; !((x+1 < 4) || (y <= 10)) !((x+1 < 4) && (y <= 10)) x+1 = 4 4 < 4 false.keep going y <= 10 true false || true true ! true is false 4 < 4 false. DONE with &&. Do not look at y <=10. !false true

if statements if statement form: if (boolean expression) c++ statement if (x < y) cout << "x < y" << endl;

if statements cautions MUST have ()s around boolean expression no syntax error for non-boolean like expressions only ONE statement in an if statement no ';' after if condition Make sure you account for values that are equal

Your turn Write an if statement to assign x to y if x is greater than y Talk to your neighbor. See if you have the same answer Keep this paper.

if-else If you want to do one thing if a condition is true and something else if not, use if-else. form: if (condition) C++ statement else C++ statement if (x < y) cout << x << " is less than the other number"; else cout << y << " is less than the other number";

> one statement in an if If you want to have more than one statement inside an if or an else, use {}s: if (x < y) { cout << x << " is less than the other number"; x = 0; } else { cout << y << " is less than the other number"; y = 0; }

If-else cautions either if clause or else clause or both may have {}s. After statements inside if and else clause are executed, control passes back to next sequential statement no ';' after else Make sure you account for values that are equal

Watch Out if (3 < 4) x = 3; else cout << "3 < 4 is false" << endl; x = 0; cout << "the value of x is " << x << endl;

Embedded ifs If statements and if-else statements may be embedded (if within if). simply evaluate them as the C++ code is executed: if-else example is most common. sets up a table of conditions

Embedded if-else AverageGrade >90A 80-89B 70-79C 60-69D <60F

Embedded if-else for table if (ave >= 90) grade = 'A'; else if ((ave = 80)) // note: need ()s around entire condition grade = 'B'; else if ((ave =70)) grade = 'C'; else if ((ave =60)) grade = 'D'; else if ((ave < 70) && (ave < 60)) grade = 'F';

Tracing through the embeded if

Fixing the embedded if if (ave >= 90) grade = 'A'; else if (ave >= 80) // We know (ave < 90) or we wouldn't be here grade = 'B'; else if (ave >=70) // we know ave < 80 grade = 'C'; else if (ave >=60) grade = 'D'; else // if ((ave < 70) && (ave < 60)) grade = 'F';

Cautions for embedded ifs Don't use redundant comparisons Make sure you check for values that are equal Account for out of range values

Program style Put {}s on a line by themselves indent {}s 2-3 spaces, statements one more than that All code outside if statements should line up All code inside of if statements should line up.

More complicated embedded ifs if (x < 3) if (y < 6) cout << "x and y between 3 and 6"; else cout = 6"; else if (y > 6) cout << "x and y not in 3-6 range"; else cout = 3; y <= 6";

You do it Writing if statements (on the same paper you used earlier)