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

Similar documents
Chapter 4 - Notes Control Structures I (Selection)

Week 2. Relational Operators. Block or compound statement. if/else. Branching & Looping. Gaddis: Chapters 4 & 5. CS 5301 Spring 2018.

C++ Programming: From Problem Analysis to Program Design, Third Edition

Flow of Control. Flow of control The order in which statements are executed. Transfer of control

V2 2/4/ Ch Programming in C. Flow of Control. Flow of Control. Flow of control The order in which statements are executed

Kingdom of Saudi Arabia Princes Nora bint Abdul Rahman University College of Computer Since and Information System CS240 BRANCHING STATEMENTS

while for do while ! set a counter variable to 0 ! increment it inside the loop (each iteration)

If Control Construct

Chapter 3. More Flow of Control. Copyright 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley

CS 101 Computer Programming and utilization. Dr Deepak B Phatak Subrao Nilekani Chair Professor Department of CSE, Kanwal Rekhi Building IIT Bombay

1- Write a single C++ statement that: A. Calculates the sum of the two integrates 11 and 12 and outputs the sum to the consol.

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures. Control Structures (cont d.) Relational Operators

Chapter 4: Control Structures I (Selection) Objectives. Objectives (cont d.) Control Structures. Control Structures (cont d.

Add Subtract Multiply Divide

CS150 Intro to CS I. Fall Fall 2017 CS150 - Intro to CS I 1

CSI33 Data Structures

5. Selection: If and Switch Controls

Lecture 5 Tao Wang 1

Logical Operators and if/else statement. If Statement. If/Else (4.3)

Control Structures. Lecture 4 COP 3014 Fall September 18, 2017

Introduction to Programming

o Counter and sentinel controlled loops o Formatting output o Type casting o Top-down, stepwise refinement

BITG 1223: Selection Control Structure by: ZARITA (FTMK) LECTURE 4 (Sem 1, 16/17)

Design and Debug: Essen.al Concepts CS 16: Solving Problems with Computers I Lecture #8

STUDENT OUTLINE. Lesson 8: Structured Programming, Control Structures, if-else Statements, Pseudocode

Lecture 3 Tao Wang 1

Review: Exam 1. Your First C++ Program. Declaration Statements. Tells the compiler. Examples of declaration statements

Chapter 4: Making Decisions

LECTURE 04 MAKING DECISIONS

Review. Relational Operators. The if Statement. CS 151 Review #4

Chapter 4: Making Decisions

Computer Programming : C++

Computer Programming

CSCI 1061U Programming Workshop 2. C++ Basics

Score score < score < score < 65 Score < 50

! A program is a set of instructions that the. ! It must be translated. ! Variable: portion of memory that stores a value. char

C++ Programming: From Problem Analysis to Program Design, Fourth Edition. Chapter 4: Control Structures I (Selection)

More Flow Control Functions in C++ CS 16: Solving Problems with Computers I Lecture #4

Chapter 2 Basic Elements of C++

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

BOOLEAN EXPRESSIONS CONTROL FLOW (IF-ELSE) INPUT/OUTPUT. Problem Solving with Computers-I

4.1. Chapter 4: Simple Program Scheme. Simple Program Scheme. Relational Operators. So far our programs follow a simple scheme

Lecture 5. Review from last week. Selection Statements. cin and cout directives escape sequences

Chapter 7. Additional Control Structures

Chapter 4: Making Decisions. Copyright 2012 Pearson Education, Inc. Sunday, September 7, 14

REPETITION CONTROL STRUCTURE LOGO

Solving Problems Flow Control in C++ CS 16: Solving Problems with Computers I Lecture #3

Statements execute in sequence, one after the other, such as the following solution for a quadratic equation:

Control Structures. Control Structures Conditional Statements COMPUTER PROGRAMMING. Electrical-Electronics Engineering Dept.

Control Flow. COMS W1007 Introduction to Computer Science. Christopher Conway 3 June 2003

CSCE 121 ENGR 112 List of Topics for Exam 1

Chapter 2. C++ Basics. Copyright 2014 Pearson Addison-Wesley. All rights reserved.

Chapter 2. C++ Basics

1. C++ Overview. C++ Program Structure. Data Types. Assignment Statements. Input/Output Operations. Arithmetic Expressions.

More Complex Versions of the if Statement. Class 13

Decision Making in C

Chapter Overview. More Flow of Control. Flow Of Control. Using Boolean Expressions. Using Boolean Expressions. Evaluating Boolean Expressions

J. P. Cohoon and J. W. Davidson 1999 McGraw-Hill, Inc. Control Constructs. Mechanisms for deciding when and how often an action should be taken

UEE1302(1102) F10: Introduction to Computers and Programming

Boolean Data-Type. Boolean Data Type (false, true) i.e. 3/6/2018. The type bool is also described as being an integer: bool bflag; bflag = true;

The following expression causes a divide by zero error:

Computer Science II Lecture 1 Introduction and Background

CSCE 206: Structured Programming in C++

Announcements. HW0 is posted on schedule, due next Friday at 9pm (pretty easy)

Introduction to Programming using C++

Quiz Determine the output of the following program:

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Fall 2016 Howard Rosenthal

By the end of this section you should: Understand what the variables are and why they are used. Use C++ built in data types to create program

CMSC201 Computer Science I for Majors

GE U111 Engineering Problem Solving & Computation Lecture 6 February 2, 2004

QUIZ: What value is stored in a after this

Flow Control. CSC215 Lecture

Chapter 4: Control Structures I (Selection)

Chapter 3. More Flow of Control

Laboratory 0 Week 0 Advanced Structured Programming An Introduction to Visual Studio and C++

Announcements. Homework 0: using cin with 10/3 is NOT the same as (directly)

ARG! Language Reference Manual

5. Control Statements

From Pseudcode Algorithms directly to C++ programs

Objectives. Chapter 2: Basic Elements of C++ Introduction. Objectives (cont d.) A C++ Program (cont d.) A C++ Program

Chapter 2: Basic Elements of C++

CHRIST THE KING BOYS MATRIC HR. SEC. SCHOOL, KUMBAKONAM CHAPTER 9 C++

Chapter 2: Basic Elements of C++ Objectives. Objectives (cont d.) A C++ Program. Introduction

Chapter Overview. C++ Basics. Variables and Assignments. Variables and Assignments. Keywords. Identifiers. 2.1 Variables and Assignments

Objectives. Chapter 4: Control Structures I (Selection) Objectives (cont d.) Control Structures

Selec%on and Decision Structures in Java: If Statements and Switch Statements CSC 121 Spring 2016 Howard Rosenthal

Flow Control in C++ Condi&onals & Loops CS 16: Solving Problems with Computers I Lecture #4

Computer Department. Question (1): State whether each of the following is true or false. Question (2): Select the correct answer from the following:

Designing Loops and General Debug Pre-Defined Functions in C++ CS 16: Solving Problems with Computers I Lecture #6

conditional statements

Introduction to C++ General Rules, Conventions and Styles CS 16: Solving Problems with Computers I Lecture #2

Decisions. Arizona State University 1

Lab Instructor : Jean Lai

Linear execution of statements. CISC 1600/1610 Computer Science I. Alternatives to linear execution. Linear execution of statements

7.1. Chapter 7: Arrays Hold Multiple Values. Array - Memory Layout. A single variable can only hold one value. Declared using [] operator:

Relational Operators and if. Class 10

Chapter 2. Flow of Control. Copyright 2016 Pearson, Inc. All rights reserved.

CS302: Self Check Quiz 2

CS150 Introduction to Computer Science 1. Logical Operators and if/else statement

Using Boolean Expressions. Multiway Branches. More about C++ Loop Statements. Designing Loops. In this chapter, you will learn about:

Transcription:

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

16 quizzes taken Average: 37.9 Median: 40.5 Quiz 1 Results 16 Scores: 45 45 44 43 43 42 41 41 40 36 36 36 34 31 28 21 Avg 37.9 Std dev 6.8 7 6 5 4 3 2 1 Grade Distribution 45-49 40-44 35-39 30-34 25-29 20-24 Below 20 2

If statement 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: ==,!= 3

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

if- Example if (mygrade >= 60) cout << You passed! << endl; cout << How about them Cubs? << endl; 5

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

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

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

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

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/ 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 10

Logical Operators 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) classrun = true; if (numstudents > MAX numinstructors == 0) classrun = false; 11

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

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) 13

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

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 15

Switch/Case Example int numpassengers; cout << Enter Passengers: ; cin >> numpassengers; switch(numpassengers) case 0: zeropassengers(); break; case 1: onepassenger(); break; default: manypassengers(); break; 16

Switch Case Example char menuitem; cout << "Enter Menu Selection: "; cin >> menuitem; switch (menuitem) case 'O': orderfunction(); break; case 'C': checkoutfunction(); break; default: errorfunction(); break; 17

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- Compound statements Relational operators Logical operators Switch/Case Statements 18