Boolean Expressions (Conditions)

Similar documents
Topics. Chapter 5. Equality Operators

Decision Making in C

Following is the general form of a typical decision making structure found in most of the programming languages:

Selection Control Structure CSC128: FUNDAMENTALS OF COMPUTER PROBLEM SOLVING

LESSON 3 CONTROL STRUCTURES

Key Differences Between Python and Java

Chapter 4: Making Decisions

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

Chapter 4: Making Decisions

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

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.

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

Java Bytecode (binary file)

Logic & program control part 2: Simple selection structures

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

LECTURE 04 MAKING DECISIONS

Lecture 5 Tao Wang 1

BRANCHING if-else statements

Contents. Jairo Pava COMS W4115 June 28, 2013 LEARN: Language Reference Manual

Control Structures in Java if-else and switch

Working with JavaScript

SELECTION. (Chapter 2)

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

CSI33 Data Structures

9/5/2018. Overview. The C Programming Language. Transitioning to C from Python. Why C? Hello, world! Programming in C

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

1007 Imperative Programming Part II

The C Programming Language. (with material from Dr. Bin Ren, William & Mary Computer Science)

Pace University. Fundamental Concepts of CS121 1

Chapter 3, Selection. Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved.

Conditional Statement

Your first C++ program

Introduction to Bioinformatics

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

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

4 Programming Fundamentals. Introduction to Programming 1 1

CONDITIONAL STATEMENTS AND FLOW CONTROL

Decision Structures. Lecture 3 MIT 12043, Fundamentals of Programming By: S. Sabraz Nawaz

Control Structures in Java if-else and switch

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

2.1. Chapter 2: Parts of a C++ Program. Parts of a C++ Program. Introduction to C++ Parts of a C++ Program

Movement using Shaft Encoders

Decisions. Arizona State University 1

Relational Operators and if. Class 10

Chapter 2.4: Common facilities of procedural languages

ARG! Language Reference Manual

EECS 183. Week 3 - Diana Gage. www-personal.umich.edu/ ~drgage

Variables and Functions. ROBOTC Software

Logical Operators and switch

MODULE 02: BASIC COMPUTATION IN JAVA

Outline. Overview. Control statements. Classes and methods. history and advantage how to: program, compile and execute 8 data types 3 types of errors

The C++ Language. Arizona State University 1

Relational & Logical Operators, if and switch Statements

CONDITIONAL EXECUTION

Intro to Programming. Unit 7. What is Programming? What is Programming? Intro to Programming

CpSc 1111 Lab 4 Part a Flow Control, Branching, and Formatting

Full file at

More Complex Versions of the if Statement. Class 13

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

Variables and data types

Decisions, Decisions, Decisions. GEEN163 Introduction to Computer Programming

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

Chapter Goals. 3.1 The if Statement. Contents 1/30/2013 DECISIONS

Flow of Control: Programs can control the order in which their instructions are executed. Four types of flow:

VENTURE. Section 1. Lexical Elements. 1.1 Identifiers. 1.2 Keywords. 1.3 Literals

Lecture 5: Making Decisions

Chapter. Solving Problems that Require Decisions. Objectives: At the end of the chapter students should be able to:

Software Design & Programming I

Conditional Programming

Interpreted vs Compiled. Java Compile. Classes, Objects, and Methods. Hello World 10/6/2016. Python Interpreted. Java Compiled

School of Computer Science CPS109 Course Notes 5 Alexander Ferworn Updated Fall 15

Microsoft Visual Basic 2005: Reloaded

Boolean Algebra Boolean Algebra

DATA TYPES AND EXPRESSIONS

CS 31: Intro to Systems Binary Arithmetic. Martin Gagné Swarthmore College January 24, 2016

cis20.1 design and implementation of software applications I fall 2007 lecture # I.2 topics: introduction to java, part 1

Text Input and Conditionals

Course Outline Introduction to C-Programming

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

Computational Expression

Chapter 2: Introduction to C++

Chapter 9: Dealing with Errors

SSOL Language Reference Manual

Introduction to C Programming

Chapter 2: Special Characters. Parts of a C++ Program. Introduction to C++ Displays output on the computer screen

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

1/11/2010 Topic 2: Introduction to Programming 1 1

if Statement Numeric Rela5onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Decision Structures. Selection. Selection options (in Java) Plain if s (3 variations) Each action could be any of: if else (3 variations)

Lecture 05 I/O statements Printf, Scanf Simple statements, Compound statements

CS 115 Lecture 8. Selection: the if statement. Neil Moore

Flow of Control. Chapter 3

if Statement Numeric Rela7onal Operators The if Statement Flow of Control: Branching (Savitch, Chapter 3)

Lists, loops and decisions

If Control Construct

Learning to Program with Haiku

Flow Control. CSC215 Lecture

Primitive Types. Four integer types: Two floating-point types: One character type: One boolean type: byte short int (most common) long

Transcription:

Boolean Expressions (Conditions) Boolean Expressions: Boolean expressions evaluate to either True or False. Boolean expressions are constructed using relational operators, which allow for comparison between values to determine whether the overall expression is True or False. When comparing values of type double, avoid using == because the computer may handle this is mathematically incorrect due to round off error. Use < or > instead. When comparing values of type char or string, uppercase and lowercase letters are not equivalent. Relational Operators: Operator What it means = = is equal to? < is less than? > is greater than? <= is less than or equal to? >= is greater than or equal to?!= is not equal to? Example: Boolean Expression Evaluates To hello = = HELLO false 5= = 5 true 3 < 10 true 3 > 2 true 5< = 5 true 5>=6 false 2! = 2 false Logical Operators: Logical operators allow you to combine two or more Boolean expressions together in order to evaluate more complex conditions. Logical operators include And, Or, and Not conditions. Operator Symbol in C++ And && Or Not!

Truth Tables for Each Logical Operator: Boolean Exp #1 Boolean Exp #2 Exp #1 && Exp #2 True True True True False False AND False True False False False False Boolean Exp #1 Boolean Exp #2 Exp #1 Exp #2 True True True True False True OR False True True False False False Boolean Exp True False! Exp False True NOT Examples: Expression Evaluates To hello = = HELLO && A == A false 5= = 5 3<1 true 3 < 10 &&!(4>9) true!(3 > 2) false 5< = 5 && 6<10 && 9==9 true 5>=6 && a == a 2>3 false 2! = 2 && 3!=3 false

Selection Statements: If Selection Statements In programming, a Selection Statement refers to some block of code that makes a decision in order to control the flow of a program. The decision is based on some condition(s) being true, which the program will evaluate during runtime. General idea: Example 1: 1. Ask the user for his/her age. 2. Collect that information from the user. 3. If the user s age is greater than or equal to 16, state that the user may be eligible to receive a driver s license. 4. Otherwise, state that the user is not eligible to receive a driver s license. Example 2: 1. Ask the user to enter a letter grade. 2. Collect the information from the user. 3. If the user has entered any of the letters: A, B, C, or D, state that the user has passed. 4. Otherwise, state that the user has failed. If Statement: Format: if( Boolean expression) The code executes such that IF the Boolean expression evaluates to TRUE, then the statement executes. If the Boolean expression evaluates to FALSE, then the statement does NOT execute. Note: if you want to execute MORE THAN 1 STATEMENT based on your condition, then you need to wrap those statements in curly brackets. Also note that because there will now be a whole lot more curly brackets, you should start labeling the closing curlies with some sort of indicator of which bracket started them (as shown in Example 2).

Example 1: Code: int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license.";

Example 2: int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license."<<endl; cout<<"however, if you do get a license, Ms. Petr will get more white hairs!";

If Else Statement: Format: if( Boolean expression) else Example: Code: When else is added to an if statement, it means in all other cases. So If the Boolean expression belonging to the If clause is False, then the statements following the Else clause will execute. int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license."<<endl; cout<<"however, if you do get a license, Ms. Petr will get more white hairs!"; else cout<<"better find a ride kiddo."; Careful: dangling else if there are no curly braces, the compiler will assume that the else goes with the if directly before it (regardless of your indentation).

If Else If Else Statement: Format: if(expression) else if(expression) else if(expression) else Example: Code: Using if..else if else statements will ensure ONLY one of the choices executes. Sometimes a sequence of if s will accomplish the same result, but when you use a ladder of separate if statements, if more than one of them has a condition that evaluates to TRUE, then MORE THAN ONE of them will execute. int age=0; cout<<"enter your age"; cin>>age; if(age>=16) cout<<"you are old enough to get a license."<<endl; cout<<"however, if you do get a license, Ms. Petr will get more white hairs!"; else if(age==15) cout<<"you are old enough to get a learner's permit."; else cout<<"better find a ride kiddo.";

Video: Getting Started with If Statements in C++: http://www.youtube.com/watch?v=smw_1usvbds&feature=youtu.be