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

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

9/18/11. Type casting, logical operators and if/else statement. Explicit Type Conversion. Example of Type Casting

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

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

Chapter 4: Making Decisions

Chapter 4: Making Decisions

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

LECTURE 04 MAKING DECISIONS

Score score < score < score < 65 Score < 50

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

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

CSCE 206: Structured Programming in C++

C/C++ Programming Lecture 7 Name:

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

The American University in Cairo Department of Computer Science & Engineering CSCI &09 Dr. KHALIL Exam-I Fall 2011

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

Add Subtract Multiply Divide

bool bool - either true or false

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

Programming Language. Control Structures: Selection (switch) Eng. Anis Nazer First Semester

Computer Programming. Basic Control Flow - Loops. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

More Complex Versions of the if Statement. Class 13

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

Chapter 2. C++ Basics

CSI33 Data Structures

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

CSc Introduc/on to Compu/ng. Lecture 8 Edgardo Molina Fall 2011 City College of New York

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

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

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.

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

Structured Programming. Flowchart Symbols. Structured Programming. Selection. Sequence. Control Structures ELEC 330 1

Loops and Files. of do-while loop

Increment and the While. Class 15

5. Selection: If and Switch Controls

Decisions. Arizona State University 1

Chapter Four: Loops II

Getting started with C++ (Part 2)

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

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

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

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

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

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

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

Chapter Four: Loops. Slides by Evan Gallagher. C++ for Everyone by Cay Horstmann Copyright 2012 by John Wiley & Sons. All rights reserved

Numerical Computing in C and C++ Jamie Griffin. Semester A 2017 Lecture 2

Chapter 4 - Notes Control Structures I (Selection)

Introduction to Computer Science Midterm 3 Fall, Points

LAB 4.1 Relational Operators and the if Statement

Chapter 4: Basic C Operators

CS 141, Introduction to Computer Science Fall Midterm Exam

5. Control Statements

CS242 COMPUTER PROGRAMMING

CS106X Handout 03 Autumn 2012 September 24 th, 2012 Getting Started

Programming Fundamentals. With C++ Variable Declaration, Evaluation and Assignment 1

Class 2: Variables and Memory. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Control Statements. If Statement if statement tests a particular condition

Chapter 2 - Control Structures

Multiple Choice (Questions 1 13) 26 Points Select all correct answers (multiple correct answers are possible)

Starting Out With C++: Early Objects, Seventh Edition Solutions to End-of-Chapter Review Questions

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

CSCI 111 First Midterm Exam Fall Solutions 09.00am 09.50am, Wednesday, October 18, 2017

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

CSCE 206: Structured Programming in C++

CSCE 206: Structured Programming in C++

Total 100. The American University in Cairo Computer Science & Engineering Department CSCE 106. Dr. Khalil Exam II Fall 2011

9/10/10. Arithmetic Operators. Today. Assigning floats to ints. Arithmetic Operators & Expressions. What do you think is the output?

Reviewing all Topics this term

Week 3. Function Definitions. Example: Function. Function Call, Return Statement. Functions & Arrays. Gaddis: Chapters 6 and 7. CS 5301 Spring 2018

CS 151 Review #3. // More than one variable can be defined // in a statement. Multiple variables are // separated by a comma.

CHAPTER 2.1 CONTROL STRUCTURES (SELECTION) Dr. Shady Yehia Elmashad

COMP Primitive and Class Types. Yi Hong May 14, 2015

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

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

CS 1428 Review. CS 2308 :: Spring 2016 Molly O Neil

Fundamental of Programming (C)

Discussion 1H Notes (Week 3, April 14) TA: Brian Choi Section Webpage:

CS 007A Midterm 1 Practice Chapters 1 5

Checking Multiple Conditions

Computer Programming. Basic Control Flow - Decisions. Adapted from C++ for Everyone and Big C++ by Cay Horstmann, John Wiley & Sons

VARIABLES & ASSIGNMENTS

Introduction to Programming

Fundamentals of Programming

C++ Program Flow Control: Selection

Relational Expressions. Boolean Expressions. Boolean Expressions. ICOM 4036 Programming Languages. Boolean Expressions

If Control Construct

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

To become familiar with array manipulation, searching, and sorting.

The following expression causes a divide by zero error:

Lecture 3 Tao Wang 1

PIC 10A. Final Review: Part I

Lecture Programming in C++ PART 1. By Assistant Professor Dr. Ali Kattan

CS 103 Lecture 3 Slides

Programming. C++ Basics

Final exam. Final exam will be 12 problems, drop any 2. Cumulative up to and including week 14 (emphasis on weeks 9-14: classes & pointers)

Introduction to Computer Programming CSCI-UA 2. Review Midterm Exam 1

PDS Lab Section 16 Autumn Tutorial 3. C Programming Constructs

Programming. We will be introducing various new elements of Python and using them to solve increasingly interesting and complex problems.

Exam 1. CSI 201: Computer Science 1 Fall 2018 Professors: Shaun Ramsey

Transcription:

1 Logical Operators and if/else statement

2 If Statement We may want to execute some code if an expression is true, and execute some other code when the expression is false. This can be done with two if statements if (value >= LIMIT) // do something if (value < LIMIT) // do something else

If/Else (4.3) C++ provides a shortcut to combine two if statements: The statements in the else clause are executed only when the expression is false. if (expression) // do stuff else // do other stuff CS150 Introduction to Computer Science 1 3

4 Example int number; cout << Enter a number, I ll tell you ; cout << if it is odd or even: ; cin >> number; // use an if/else statement here

5 If/Else: Commenting // the expression I m using here // checks for... // so that I can... if (expression) else

6 if/else/if statements (4.4) What if there are more than two alternatives? if (RAINY == currentweather) cout << I need a rain jacket ; else if (SUNNY == currentweather) cout << I need a my shades ; else cout << I better look outside! ;

7 if/else/if statements (4.4) What if there are more than two alternatives? if (RAINY == currentweather) cout << I need a rain jacket ; else if (SUNNY == currentweather) Why is SUNNY == currentweather cout << I need a my shades ; better than currentweather == SUNNY? else cout << I better look outside! ;

8 Logical Operators (4.7) There are three logical operators && And Or! Not

9 Precedence Precedence Operators (Highest to Lowest) - (negation)! (Logical NOT) * / % - + <= => > < ==!= && = += -= *= /= %=

10 Evaluating Expressions: And && expr1 && expr2 For the complete expression to be true, both expr1 and expr2 have to be true Example: temp > HOT && humidity > STICKY o These are unbearable heat and humidity conditions o Both must be true for the entire expression to be true

11 Evaluating Expressions: Or expr1 expr2 The complete expression is true if either expr1 or expr2 is true Examples: salary < MIN_SALARY MARRIED == status o To qualify for financial aid, salary has to be less than some minimum salary or you must be married o Only one condition has to be true

12 Evaluating Expressions: Not!!expr Unary operator: Negation Examples:!(salary < MIN_SALARY) o What makes this true? False?

13 Expression Evaluation According to the operator precedence and associativity rules given on the previous slide, how will the following expressions be evaluated? x < min + max min <= x && x <= max!x == y + 2 x = a + b % 7 * 2

14 Practice Are these two code snippets equivalent? int x, y; if(x > y) x += y; if(y < x) y += x; int x, y; if(x > y) x += y; else y += x;

15 Problem Write a C++ program segment that allows the user the ability to input an integer from the keyboard. If the integer is positive, increment a variable poscount by 1. If the integer is negative, increment a variable negcount by 1. If neither, increment zerocount by 1 int poscount=0, negcount=0, zerocount=0;

16 Problem Write a program that displays a letter grade corresponding to an exam score 90-100 A double examgrade; 80-89 B cin >> exangrade; 70-79 C 60-69 D 0-59 F

17 Nested if Statements (4.6) Note the indentation of the inner if if (actual > expected) if (MAX == actual) else else

18 Example Write nested if statements that set the correct value in the wage variable: If your status is full time, and you worked more than 10 years, your wage is $25. All other full time workers have a wage of $15. If your status is part time, you have a wage of $10. const int FULLTIME=0, PARTTIME=1; double wage; int yearsworked, status;

19 Example Your local bookstore has asked you to write a program to help them determine the cost of shipping of customers orders. If the order is $30 or less then shipping will cost $5, if the order is over $30 then shipping will be $3

20 Problem The bookstore has now changed it s shipping policy so that o If the order is $30 or less, shipping is $5 o If the order is over $30 but less than $50, shipping is $3 o If the order is over $50 then shipping is $2

21 exit() To terminate a program we can use the exit(int status) function o This is a function, not part of the language #include<stdlib.h> o The status is returned to the operating system to denote program success or failure Success: 0 Failure: non-zero

22 Practice Write a complete program that will ask the user for two integers. Display both integers to the screen only if they are each greater than 1000 and terminate the program with exit() otherwise. Use exactly one if/else

Floating Point and Relational Operators CS150 Introduction to Computer Science 1 23 Floating point math may not work out as you expect because of round off errors. In Math o 6 * 2/3 = 4 In C++, where 0.66666 is equivalent to 2/3 o 6.0 * 0.66666 = o 6.0 * 0.66667 = o 6.0 * 0.666666 = o 6.0 * ( 2.0 / 3.0 ) =

Example double result; result = 6.0 * 0.666666; if(result == 4.0) cout << result == 4.0 << endl; cout << setprecision(6) << fixed << result << endl; cout << setprecision(2) << fixed << result << endl; CS150 Introduction to Computer Science 1 24

Example double result; result = 6.0 * 0.666666; if(result == 4.0) cout << result == 4.0 << endl; cout << setprecision(6) << fixed << result << endl; cout << setprecision(2) << fixed << result << endl; CS150 Introduction to Computer Science 1 25