Bools & Ifs.

Slides:



Advertisements
Similar presentations
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Advertisements

A loop is a repetition control structure. it causes a single statement or block to be executed repeatedly What is a loop?
1 9/24/07CS150 Introduction to Computer Science 1 Relational Operators and the If Statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Announcements Quiz 1 Posted on blackboard Handed Back (and Gone Over)Next Monday “Only a Quiz” Counts 5% of Final Grade Exam 1 Counts 10%
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,
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
1 CS150 Introduction to Computer Science 1 Relational Operators and the If Statement 9/22/08.
1 Arithmetic in C. 2 Type Casting: STOPPED You can change the data type of the variable in an expression by: (data_Type) Variable_Name Ex: int a = 15;
The If/Else Statement, Boolean Flags, and Menus Page 180
Administrative MUST GO TO CORRECT LAB SECTION! Homework due 11:59pm on Tuesday. 25 points off if late (up to 24 hours) Cannot submit after 11:59pm on Wednesday.
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.
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.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Chapter 5 Loops. Overview u Loop Statement Syntax  Loop Statement Structure: while, for, do-while u Count-Controlled Loops u Nested Loops u Loop Testing.
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.
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Introduction to Programming Prof. Rommel Anthony Palomino Department of Computer Science and Information Technology Spring 2011.
PROGRAM FLOW CHAPTER3 PART1. Objectives By the end of this section you should be able to: Differentiate between sequence, selection, and repetition structure.
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.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
Overview Go over parts of quiz? Another iteration structure for loop.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
1 For Loops l From Chapter 9 l A shorthand way of coding count loops.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
LECTURE # 8 : REPETITION STATEMENTS By Mr. Ali Edan.
STRUCTURED PROGRAMMING Selection Statements. Content 2  Control structures  Types of selection statements  if single-selection statement  if..else.
Program Flow Control Addis Ababa Institute of Technology Yared Semu April 2012.
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.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
LECTURE # 7 : STRUCTURED PROGRAMMING Selection Statements Tr.Hadeel.
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.
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
1 Chapter 4 - Control Statements: Part 1 Outline 4.1 Introduction 4.4 Control Structures 4.5 if Selection Structure 4.6 if/else Selection Structure 4.7.
The Ohio State University
Branching statements.
Chapter 3 Selection Statements
Chapter 4: Control Structures I (Selection)
REPETITION CONTROL STRUCTURE
Selection (also known as Branching) Jumail Bin Taliba by
Bill Tucker Austin Community College COSC 1315
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
ITM 352 Flow-Control: if and switch
Chapter 4: Making Decisions.
Chapter 4: Control Structures I (Selection)
Bools & Ifs.
3 Control Statements:.
Chapter 7 Conditional Statements
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Chapter 4: Control Structures I (Selection)
Understanding Conditions
CS2011 Introduction to Programming I Selections (I)
Control Statements Paritosh Srivastava.
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

Bools & Ifs

Control Structures Normal execution is sequential Control structures Selection (branching): making a choice Repetition (iteration): looping

Relational Operators Relational operators : Binary operators – compare two values Evaluate to true or false

Examples Samples: Notes: 8 < 15 evaluates to true 6 != 6 evaluates to false 2.5 > 5.8 evaluates to false 5.9 <= 7.5 evaluates to true Notes: ! means "not" < or > always before = (no => operator)

True/False C++ bool type Two values: true (1) & false (0) bool enrolled = true; //same as = 1 cout << enrolled;

True/False C has no bool C++ converts numbers to bools in same way: Uses integers : 0 = false, all others = true C++ converts numbers to bools in same way: bool enrolled = 3.2; //true bool enrolled = 23; //true bool enrolled = -4; //true bool enrolled = 2 - 2; //false

Relational Evaluation Can store result of relational operator: bool result = 5 < 9; cout << result; Outputs 1 (true) bool result = 5 == 9; cout << result; Outputs 0 (false)

Relational Operators Conditional statements: only executed if certain conditions are met if( score is greater than or equal to 90 ) print "Grade is A"

One-Way Selection One-way selection syntax: Expression true  execute statement Expression false  skip statement

If Example Indent statement! Example: int grade; //read grade if(grade < 60) cout << "F"; Indent statement!

Evil Error 1 if(grade < 60); cout << "F"; Always prints F If grade < 60 do empty statement (;) No matter what, print F

Evil Error 2 if (x = 5) cout << "Is 5" << endl;

Evil Error 2 if (x = 5) cout << "Is 5" << endl; Changes x to 5 Value of assignment is 5  true Linux Hack Attempt: if ((options == (__WCLONE|__WALL)) && (current->uid = 0))

Evil Error 2 Yoda style (constant first) prevents this error: if (5 == x) cout << "Is 5" << endl;

Two-Way Selection Two-way selection syntax: Expression true  execute statement1 Expression false  execute statement2 Only 1 of the 2 statements executes

If Example Example: int grade; //read grade if(grade < 60) cout << "F"; else cout << "Pass";

Compound (Block of) Statements Compound statement (block of statements): Surrounded by { } Acts as one statement

If Example Indent statements! Example: int grade; double credits; //read grade if(grade < 60) cout << "F"; else { credits += 4; cout << "Pass"; } Indent statements!

Bad Block Example: Need { } to group instructions: if(grade < 60) credits += 0; cout << "F"; //always executes Need { } to group instructions: if(grade < 60) { credits += 0; cout << "F"; }

Block Guides When in doubt, use { } Same line { prevents accidental ; if(grade < 60) { cout << "F"; //this is fine } Same line { prevents accidental ; if(grade < 60); { cout << "F"; //always happens }