Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2.

Slides:



Advertisements
Similar presentations
3 Decision Making: Equality and Relational Operators A condition is an expression that can be either true or false. Conditions can be formed using the.
Advertisements

Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
If Statements & Relational Operators Programming.
True or false A variable of type char can hold the value 301. ( F )
Computer Science 1620 Loops.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Introduction to Computers and Programming Lecture 9: For Loops New York University.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Boolean Expressions Conditional Statements & Expressions CSC 1401: Introduction to Programming with Java Lecture 4 – Part 2 Wanda M. Kunkle.
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
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.
School of Computing Science CMT1000 Ed Currie © Middlesex University 1 CMT1000: Introduction to Programming Ed Currie Lecture 5B: Branch Statements - Making.
Copyright © 2009 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Java Software Solutions Foundations of Program Design Sixth Edition by Lewis.
General Computer Science for Engineers CISC 106 Lecture 33 Dr. John Cavazos Computer and Information Sciences 05/11/2009.
CS 1400 Jan 2007 Chapter 4, sections Relational operators Less than< Greater than> Less than or equal= Equal== Not equal!=
1 Midterm Review COMP 102. Tips l Eat a light meal before the exam l NO electronic devices (including calculators, dictionaries, phones, pagers, etc.)
Computer Science 1620 Selection Structures. Write a program that accepts the speed of a vehicle, and determines whether that person is speeding assume.
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 Program Control Statements
True or False: Boolean Expression Boolean expression are expressions which evaluate to "true" or "false“ Primary operators to combine expressions: && (and),
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.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
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.
Flow of Control Part 1: Selection
Chapter 04 Control Statements: Part II. OBJECTIVES In this part you will learn: if…else Double-Selection Statement. while Repetition Statement.
Making Decisions (True or False) Relational Operators >greater than =greater than or equal to
CSIS 113A Lecture 3 Conditional & Switch Glenn Stevenson CSIS 113A MSJC.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
1 Chapter 4: Basic Control Flow ► Chapter Goals  To be able to implement decisions using if statements  To understand statement blocks  To learn how.
Lecture 7: Making Decisions Professor: Dr. Miguel Alonso Jr. Fall 2008 CGS2423/COP1220.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Computing and Statistical Data Analysis Lecture 2 Glen Cowan RHUL Physics Computing and Statistical Data Analysis Variables, types: int, float, double,
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.
Java Programming Fifth Edition Chapter 5 Making Decisions.
CSC Programming for Science Lecture 10: Boolean Expressions & More If ­ Else Statements.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
CHAPTER 2 PROBLEM SOLVING USING C++ 1 C++ Programming PEG200/Saidatul Rahah.
Decision Statements, Short- Circuit Evaluation, Errors.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Selection in C++ If statements. Control Structures Sequence Selection Repetition Module.
1 Flow of Control Chapter 5. 2 Objectives You will be able to: Use the Java "if" statement to control flow of control within your program.  Use the Java.
CSE202: Lecture 5The Ohio State University1 Selection Structures.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
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,
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Copyright © 2014 Pearson Addison-Wesley. All rights reserved. 4 Simple Flow of Control.
Branching statements.
Chapter 3 Control 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.
CMPT 201 if-else statement
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Conditionals & Boolean Expressions
Conditionals & Boolean Expressions
Chapter 4: Control Structures I (Selection)
Bools & Ifs.
Summary Two basic concepts: variables and assignments Basic types:
Control Structure Chapter 3.
SE1H421 Procedural Programming LECTURE 4 Operators & Conditionals (1)
Logic of an if statement
Life is Full of Alternatives
Presentation transcript:

Glenn Stevenson CSIS 113A MSJC CSIS 113A Lecture 2

Glenn Stevenson CSIS 113A MSJC The bool type Can have only hold two separate values –true, false bool empty = true; bool full; full = false; Watch your case! –False and false are different

Glenn Stevenson CSIS 113A MSJC Relational Operators Used to create Boolean expressions –A statement that evaluates to true or false

Glenn Stevenson CSIS 113A MSJC Relational Operators II <Less than >Greater than <=Greater than or equals >=Less than or equals ==Equals !=Not Equals int x = 3, y = 4; bool z = x > y; z = x < y; z = x == y; z = x !=y; What is the value of z on each line?

Glenn Stevenson CSIS 113A MSJC Primitive Relations Each relational operator require 2 primitive operands –The result is a Boolean value Only works with comparable primitive types –Most types are comparable »Normally don’t need to worry about mixed type comparisons VariablesComparisons int b = 2; int sh = 3300; double d = 2.34; float f = 2.34F; char c = 'A'; b > sh is false c = d is true

Glenn Stevenson CSIS 113A MSJC Floating Point Relations General Rule –Never compare floating point operands using == or != (.1 * 10.0) == 1.0; // C++ considers this to be true What about this: ( ) == 1.0

Glenn Stevenson CSIS 113A MSJC Introduction to selection The relational operators, and the Boolean values that they produce, are important –They allow us to implement selection. Acts like a “highway divider” in your code.

Glenn Stevenson CSIS 113A MSJC If Statement If is considered a block of code. –So why doesn’t it have braces? If you only want to execute one statement as a result of the if you don’t need braces Condition should be derived from relational operators

Glenn Stevenson CSIS 113A MSJC An Example #include using namespace std; int main() { int number; cout > number; if(number == 50) cout << "50 is a big number to square! " << endl; cout << number << " squared is " << number * number << endl; return 0; }

Glenn Stevenson CSIS 113A MSJC If / else If has an optional else –It cannot stand alone Must be preceded by an if statement

Glenn Stevenson CSIS 113A MSJC Multiple statements When you want to execute multiple statements as a result of the if condition –Must surround code to execute by braces

Glenn Stevenson CSIS 113A MSJC Indentation Styles I 3 acceptable styles –1. opening brace on same line as if Can be difficult to spot missing braces with this style if (amountSold <= 35000) { bonusPct =.035; bonusAmt = amountSold * bonusPct; } else { bonusPct =.075; bonusAmt = amountSold * bonusPct + 100; }

Glenn Stevenson CSIS 113A MSJC Indentation Style II Style I prefer –Braces are lined up on top of each other with code indented Make seeing missing braces easy if (amountSold <= 35000) { bonusPct =.035; bonusAmt = amountSold * bonusPct; } else { bonusPct =.075; bonusAmt = amountSold * bonusPct + 100; }

Glenn Stevenson CSIS 113A MSJC Indentation Styles 3 Variation of number 2 –Again, braces don’t stand so finding a missing one could again be a problem if (amountSold <= 35000) { bonusPct =.035; bonusAmt = amountSold * bonusPct; } else { bonusPct =.075; bonusAmt = amountSold * bonusPct + 100; }

Glenn Stevenson CSIS 113A MSJC Why use braces? Required if multiple lines of code are used within and if or an if / else –What is wrong with the following code? bonusAmt = 0; if (amountSold <= 35000) bonusPct =.035; else bonusPct =.075; bonusAmt+= 100; bonusAmt += amountSold * bonusPct;

Glenn Stevenson CSIS 113A MSJC Rule of thumb Beginning programmers should always use braces –Even if there is only one statement to execute It is ok to omit them if you are putting everything on a single line: if (amt < 100) cost =.23;

Glenn Stevenson CSIS 113A MSJC Nested ifs What is a nested if ? –One if (or if-else) appears as the "body" of another if ( x == 3 ) if ( z == 4 ) y = 3; else y = 4; else if ( z == 4 ) y = 5; else y = 6;

Glenn Stevenson CSIS 113A MSJC Nested Ifs II What does this print when... Amount is 9? When Amount is 100? if (Amount > 10) if (Amount < 100) cout << "Between 1 and 99\n"); else cout << "Less than 10\n"); // Is it? Rule: every else is matched with the last unmatched if statement “Dangling else” Use braces to correct

Glenn Stevenson CSIS 113A MSJC Selecting one of several I Selecting one of n if (x == 1) { // action for 1 } else { if (x == 2) { // action for 2 } else { if (x == 3) { … }

Glenn Stevenson CSIS 113A MSJC Selecting one of several II Same problem using if-else-if –if (x == 1) { // action for 1 } else if (x == 2) { // action for 2 } else if (x == 3) { … } –Same code, just reformatted

Glenn Stevenson CSIS 113A MSJC Use of Boolean Expression Nesting also combines boolean expressions if statement can appear in body of another if This statement uses && if ( balance > 1000 && age > 21 ) statement; This uses a nested if if ( balance > 1000 ) if ( age > 21 ) statement;

Glenn Stevenson CSIS 113A MSJC Short Circuit Evaluation Precedence –Logical AND (&&) is higher than OR (||) –What is ( 10 8 && 3 > 5 ) ? if ( (a != 0) && ( b / a > 12 )) if ( (a > 10) || (b++ > 7)) Evaluation stops when outcome is determined

Glenn Stevenson CSIS 113A MSJC Ladder-Style if...else Called "ladders" [ if-else-if ] –More easily understood than traditional nesting Note if-else-if statements are interdependentMust often be careful to place in correct order if (age <= 7) fee = 8.00; else if (age <= 12) fee = 10.50; else fee = 21.75;

Glenn Stevenson CSIS 113A MSJC Phantom Semicolon Common error, –Remember, decisions are blocks They are terminated by an ending brace –That is unless you have one statement to execute if (employeesInBuilding == 0); { demolishBuilding(); } UhOh, this is a big problem!!

Glenn Stevenson CSIS 113A MSJC Two Logical Problems The impossible condition if (age > 65 && age < 21) … Solution?Change order if you are trying to bound valueChange AND to ORThe unavoidable condition if (age > 21 || age < 65) Solution? Change OR to AND