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

1 CIS Jan Overview Selection Statements –If Statement –Else –Nested If-Else –Switch Repetition Statements –While statement –For Statement.
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.
1 Objectives You should be able to describe: Relational Expressions The if-else Statement Nested if Statements The switch Statement Common Programming.
Selection Statements choice of one among several blocks of code Java supports 3 kinds of selection statements: if statement – selects one block or leaves.
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 Lecture 14 Chapter 6 Looping Dale/Weems/Headington.
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
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
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.
2 Objectives You should be able to describe: Relational Expressions Relational Expressions The if-else Statement The if-else Statement Nested if Statements.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Simple Control Structures IF, IF-ELSE statements in C.
CONTROLLING PROGRAM FLOW
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.
COMPUTER PROGRAMMING. Iteration structures (loops) There may be a situation when you need to execute a block of code several number of times. In general,
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
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.
Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.
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.
Control Flow Statements
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.
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.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Conditional Statements A conditional statement lets us choose which statement will be executed next Conditional statements give us the power to make basic.
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.
Topic : While, For, Do-While Loop Guided By : Branch : Batch :
Branching statements.
Chapter 4: Control Structures I (Selection)
Topic 4: Looping Statements
Chapter 4 Repetition Statements (loops)
REPETITION CONTROL STRUCTURE
Selection (also known as Branching) Jumail Bin Taliba by
Bill Tucker Austin Community College COSC 1315
EGR 2261 Unit 4 Control Structures I: Selection
Variables A piece of memory set aside to store data
ITM 352 Flow-Control: if and switch
Bools & Ifs.
Control Structures.
Chapter 4 LOOPS © Bobby Hoggard, Department of Computer Science, East Carolina University / These slides may not be used or duplicated without permission.
Chapter 4: Control Structures I (Selection)
3 Control Statements:.
Chapter 4 Selection.
Summary Two basic concepts: variables and assignments Basic types:
If Statements.
Chapter 4: Control Structures I (Selection)
Control Statements Paritosh Srivastava.
CS 101 First Exam Review.
Branching statements Kingdom of Saudi Arabia
Presentation transcript:

Bools & Ifs

True/False C++ bool type Two values: true & false

True/False C++ bool type Two values: true & false Printed as 1 and 0

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:

Relational Operators Relational operators : Binary operators – compare two values Evaluate to true or 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)

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

Conditional If syntax: if( expression ) statement;

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

Evil Error 1 if(grade < 60); cout << "F";

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: if( expression ) statement1; else statement2; Exactly one statement executes

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

Evil Error 3 if(grade >= 60); cout << "Pass"; else cout << "You shall not pass"; cout << "try again";

Evil Error 3 Always prints "try again" if(grade >= 60); cout << "Pass"; else cout << "You shall not pass"; cout << "try again"; Always prints "try again" Else only applies to next statement!

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

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

Example Use { } with if/else to group statements: if(grade < 60) creditsEarned = 0; cout << "F"; //always executes Need { } to group instructions: if(grade < 60) { credits += 0; cout << "F"; }

Block Guides Never hurts to put { } around if/else body: if(grade < 60) { cout << "F"; //this is fine }