1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.

Slides:



Advertisements
Similar presentations
1 Conditional Statement. 2 Conditional Statements Allow different sets of instructions to be executed depending on truth or falsity of a logical condition.
Advertisements

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)
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.
1 Conditionals In many cases we want our program to make a decision about whether a piece of code should be executed or not, based on the truth of a condition.
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 CS 105 Lecture 6 While Loops Wed, Feb 16, 2011, 5:13 pm.
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.)
The If/Else Statement, Boolean Flags, and Menus Page 180
C++ for Engineers and Scientists Third Edition
Visual C++ Programming: Concepts and Projects
Starting Out with C++: Early Objects 5/e © 2006 Pearson Education. All Rights Reserved Starting Out with C++: Early Objects 5 th Edition Chapter 4 Making.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
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.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
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.
CPS120: Introduction to Computer Science Decision Making in Programs.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Selection Structures (if & switch statements) (CS1123)
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
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.
Announcements Exam 1 Tuesday July minutes 10 % of Total Grade Covers Everything through Lecture 05. –Includes Quiz 1 Topics (terminology, variables,
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
1 09/15/04CS150 Introduction to Computer Science 1 Life is Full of Alternatives Part 2.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
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.
PEG200/Saidatul Rahah 1.  Selection Criteria › if..else statement › relational operators › logical operators  The if-then-else Statement  Nested if.
Quiz 3 is due Friday September 18 th Lab 6 is going to be lab practical hursSept_10/exampleLabFinal/
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,
CPS120: Introduction to Computer Science Decision Making in Programs.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Conditions - compare the values of variables, constants and literals using one or more relational.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
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.
Lecture 6 – Selection FTMK, UTeM – Sem /2014.
Instructor: Alexander Stoytchev CprE 185: Intro to Problem Solving (using C)
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):
Quiz 1 Exam 1 Next Monday. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) System.out.println(“You have an A!” ); else System.out.println(“You.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Branching statements.
Selection (also known as Branching) Jumail Bin Taliba by
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.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
CSC113: Computer Programming (Theory = 03, Lab = 01)
Programming Fundamentals
Chapter 4: Making Decisions.
Selection (if-then-else)
Chapter 7 Conditional Statements
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
Structured Program Development in C++
Selection Control Structure
Announcements Exercise Sets 2 and 3 Due Thursday.
Presentation transcript:

1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm

2 Quiz 1 Results 16 quizzes taken Average: 37.9 Median: Scores: Avg 37.9Std dev 6.8

3 Syntax  if (condition) ← Must have ()!! { statement(s); ← Body of if stmt/True Branch of if stmt } Condition must evaluate to true or false. Two types of operators for conditionals:Relational:, >=, <=Equality: ==, != If statement

4 Syntax  if (condition) ← Must have ()!! { statement(s); ← True Branch } else { statement(s); ← False Branch } Depending on condition, either do the true branch or the false branch (but not both). If-else statement

5 if-else Example if (myGrade >= 60) { cout << “You passed!” << endl; } else { cout << “How about them Cubs?” << endl; }

6  if (myGrade >= 80) if (myGrade >= 90) cout << “You have an A!” << endl; else cout << “You have a B!” << endl;else cout << “We’ll give you a C.” << endl; You can use an if-else statement as the statement for a true branch or false branch. The result is a nested if-else statement. The inner if-else is indented relative to the outer if-else. Nested if-else Statements

7 Here's the same code with explicit curly braces.  if (myGrade >= 80)  { if (myGrade >= 90) { cout << “You have an A!” << endl; } else { cout << “You have a B!” << endl; } }else  { cout << “We’ll give you a C.” << endl;  } Nested if-else with Braces

8 Cascaded if-else: Use nested if-else in false branch. Easier to understand than when we nest within the true branch. Examples  if (myGrade > 90) cout 80) cout 70) cout << “C!” << endl; else cout << “Oh-oh!” << endl; Cascaded if-else  if (myGrade > 90) cout 80) cout 70) cout << “C!” << endl; else cout << “Oh-oh!” << endl;

9 Decision Tree A nested if-else statement implements a decision tree — start at the top and follow the path determined by answers to questions.

10 Boolean Type A Boolean value is either true or false In C++, type bool Example: bool done = false;... if (currentLetter == 'Z') done = true;... Conditions are Boolean if/else conditionals must evaluate to true or false, and are therefore called boolean expressions In C++, any non-zero value is considered true; any expression evaluating to zero is considered false. But we usually write true and false

11 A logical operator connects two or boolean more expressions together.&& is logical “and”|| is logical ‘or”&&: Both operands must be true for entire expression to be true||: One or both of the operands must be true for entire expression to be true.  if (numStudents > MIN && numStudents MAX || numInstructors == 0) classRun = false; Logical Operators

12 Operator Precedence () ! (not) *, /, % +, - (as in x-y), >= (Relational Operators) ==, != (Equality Operators) && (Logical AND) || (Logical OR) = (ASSIGNMENT)

13 Order of Operations Precedence: Level of importance of operations Multiplicative operators have higher precedence than additive operators: *, /, % Higher +, - Lower Associativity: Order of operation for equal level precedence Most operators have left-to-right associativity Use parentheses to force different precedence of operations (or to emphasize actual precedence)

14 Multiple Logical Operators if ( num1 > MAX || num2 == 0 && num3 == 0) cout MAX, or, both num2 and num3 are zero”; else cout << “num1 is <= MAX, " << "and, one or both of num2 and num3 are nonzero” << endl;

15 Switch Statements Also Called switch/case statement Just Case in other languages Selects among several different actions Can only select from integer or character If an integer value is matched, statements under control of that case block are executed

16 Switch/Case Example int numPassengers; cout << “Enter Passengers: “; cin >> numPassengers; switch(numPassengers) { case 0: zeroPassengers(); break; case 1: onePassenger(); break; default: manyPassengers(); break; }

17 Switch Case Example char menuItem; cout << "Enter Menu Selection: "; cin >> menuItem; switch (menuItem) { case 'O': orderFunction(); break; case 'C': checkoutFunction(); break; default: errorFunction(); break; }

18 Announcements Exam 1 Next Week in Lecture 50 minutes 10 % of Total Grade Covers Everything through Lecture this Week Quiz 1 Topics (terminology, variables, constants, basics) Type casting if-then-else Compound statements Relational operators Logical operators Switch/Case Statements