Relational Operators and if. Class 10

Similar documents
Lecture 2 Tao Wang 1

CSc 10200! Introduction to Computing. Lecture 2-3 Edgardo Molina Fall 2013 City College of New York

Computer Programming : C++

Exercise: Using Numbers

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

LECTURE 02 INTRODUCTION TO C++

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

Chapter 2: Basic Elements of C++

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

3. Except for strings, double quotes, identifiers, and keywords, C++ ignores all white space.

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

Add Subtract Multiply Divide

Chapter 2: Introduction to C++

CHAPTER 3 BASIC INSTRUCTION OF C++

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

Increment and the While. Class 15

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

CSc Introduction to Computing

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

Selection Statements. Pseudocode

Chapter 4 - Notes Control Structures I (Selection)

! A literal represents a constant value used in a. ! Numbers: 0, 34, , -1.8e12, etc. ! Characters: 'A', 'z', '!', '5', etc.

Maciej Sobieraj. Lecture 1

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

Lab # 02. Basic Elements of C++ _ Part1

Starting Out with C++: Early Objects, 9 th ed. (Gaddis, Walters & Muganda) Chapter 2 Introduction to C++ Chapter 2 Test 1 Key

4. Structure of a C++ program

Introduction to Bioinformatics

Integer Data Types. Data Type. Data Types. int, short int, long int

BTE2313. Chapter 2: Introduction to C++ Programming

5. Control Statements

Key Differences Between Python and Java

In Java, data type boolean is used to represent Boolean data. Each boolean constant or variable can contain one of two values: true or false.

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

DECISION STRUCTURES: USING IF STATEMENTS IN JAVA

The following expression causes a divide by zero error:

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

2 nd Week Lecture Notes

If Control Construct

THE INTEGER DATA TYPES. Laura Marik Spring 2012 C++ Course Notes (Provided by Jason Minski)

Chapter 5 Selection Statements. Mr. Dave Clausen La Cañada High School

ARG! Language Reference Manual

Objectives. In this chapter, you will:

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

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

CS242 COMPUTER PROGRAMMING

C++ Data Types. 1 Simple C++ Data Types 2. 3 Numeric Types Integers (whole numbers) Decimal Numbers... 5

Main Memory Organization

More Complex Versions of the if Statement. Class 13

Boolean Algebra Boolean Algebra

CSI33 Data Structures

Numbers. John Perry. Spring 2017

CS101: Fundamentals of Computer Programming. Dr. Tejada www-bcf.usc.edu/~stejada Week 1 Basic Elements of C++

a data type is Types

Outline. Review of Last Week II. Review of Last Week. Computer Memory. Review Variables and Memory. February 7, Data Types

On a 64-bit CPU. Size/Range vary by CPU model and Word size.

CS 112 Introduction to Computing II. Wayne Snyder Computer Science Department Boston University

Arithmetic Operators. Binary Arithmetic Operators. Arithmetic Operators. A Closer Look at the / Operator. A Closer Look at the % Operator

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

Chapter 2. C++ Basics

Variables. Data Types.

LECTURE 04 MAKING DECISIONS

Topic 2: C++ Programming fundamentals

c++ keywords: ( all lowercase ) Note: cin and cout are NOT keywords.

Basic program The following is a basic program in C++; Basic C++ Source Code Compiler Object Code Linker (with libraries) Executable

CAMBRIDGE SCHOOL, NOIDA ASSIGNMENT 1, TOPIC: C++ PROGRAMMING CLASS VIII, COMPUTER SCIENCE

Chapter 7. Additional Control Structures

BITG 1233: Introduction to C++

Conditional Statement

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

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

Creating a C++ Program

C++ Basics. Lecture 2 COP 3014 Spring January 8, 2018

7/8/10 KEY CONCEPTS. Problem COMP 10 EXPLORING COMPUTER SCIENCE. Algorithm. Lecture 2 Variables, Types, and Programs. Program PROBLEM SOLVING

Review for COSC 120 8/31/2017. Review for COSC 120 Computer Systems. Review for COSC 120 Computer Structure

Getting started with C++ (Part 2)

Chapter 2: Using Data

REVIEW. The C++ Programming Language. CS 151 Review #2

Control Structures. A program can proceed: Sequentially Selectively (branch) - making a choice Repetitively (iteratively) - looping

Computer Skills (2) for Science and Engineering Students

Expressions and Data Types CSC 121 Spring 2015 Howard Rosenthal

Information Science 1

Information Science 1

Software Design & Programming I

CSCE 121 ENGR 112 List of Topics for Exam 1

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

UEE1302(1102) F10: Introduction to Computers and Programming

Introduction to Scientific Programming with C++

CSI33 Data Structures

Lecture 3 Tao Wang 1

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

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

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

Review of Important Topics in CS1600. Functions Arrays C-strings

UNIT- 3 Introduction to C++

CS31 Discussion 1E Spring 17 : week 08

Boolean evaluation and if statements. Making decisions in programs

Introduction to Programming

Exam 1 Practice CSE 232 Summer 2018 (1) DO NOT OPEN YOUR EXAM BOOKLET UNTIL YOU HAVE BEEN TOLD TO BEGIN.

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

Transcription:

Relational Operators and if Class 10

Data Type a data type consists of two things:

Data Type a data type consists of two things: a set of values

Data Type a data type consists of two things: a set of values a set of operations defined on those values the numeric types have the operations negation additive + and multiplicative * / and % assignment = *= += etc.

Relops there is another whole family of operators for the numeric types they are the relational operators, or relops for short they allow us to compare one value to another the relops are given in table 4-1 on page 152, and are: Operator Meaning > greater than < less then >= greater than or equal to <= less than or equal to == equal to types!= not equal to

Relops and Types just like arithmetic operators relops have rules about mixing types you can compare integer types of the same size and kind (signed and unsigned) integer types of different sizes but the same kind floating point types of the same size using the four lt and gt relops a floating point type and an integer type using the four lt and gt relops you must beware comparing floating point types using the two equality relops you cannot compare floating point types of different sizes signed and unsigned integer types

Return Type when you add two ints, you get an int int = int + int; when you compare two ints, you get a bool bool = int < int;

Return Type when you add two ints, you get an int int = int + int; when you compare two ints, you get a bool bool = int < int; in other words, the returned value as a result of comparing two ints is a Boolean value the statement 5 < 10 is an assertion it makes the claim, 5 is less than 10 the claim is either true or false thus, its type is Boolean

Assertions the relops are used to make assertions that evaluate to either true or false 5 < 10 is a true assertion 10 < 5 is a false assertion an expression formed using a relop is a Boolean expression a Boolean expression is either true or false assume x has the value 10 and y has the value 7 Expression x < y x > y x >= y x == y x!= y Value false because x is not less than y true because x is greater than y true because x is greater than or equal to y false because x is not equal to y true because x is not equal to y

Returned Values because a relop returns a value of true or a value of false we can store that returned value in a variable what kind of variable can store the values true and false?

Returned Values because a relop returns a value of true or a value of false we can store that returned value in a variable what kind of variable can store the values true and false? variables of type bool bool value1; bool value2; unsigned int x = 10; unsigned int y = 7; value1 = x < 5; // value1 is now false value2 = x!= y; // value2 is now true

Outputting Boolean Values by default, cout displays Boolean values in the old-fashioned C way true is printed on screen as 1, while false is printed as 0 this is very poor form instead, we use the boolalpha io manipulator (sticky) to print the actual Boolean values see program print boolean.cpp, based on Checkpoint 4.1 on page 155

Comparing Floating Point Values int main() { double a = 1.5; double b = a + 0.00000000000000001; } bool equal = (a == b); cout << boolalpha << equal << endl; return 0; what appears on the screen?

Comparing Floating Point Values int main() { double a = 1.5; double b = a + 0.00000000000000001; } bool equal = (a == b); cout << boolalpha << equal << endl; return 0; what appears on the screen? true!

Comparing Floating Point Values in fact, the program does not compile correctly: compare_floating.cpp:10:19: warning: comparing floating point with == or!= is unsafe the four lt and gt relops are fine with floating point values but you cannot test floating point values with == or!=

Comparing Floating Point Values I weigh a sample of chemical and find its weight to be 3.306587 grams you weigh a sample and find it weighs 3.306421 grams are the two weights the same?

Comparing Floating Point Values I weigh a sample of chemical and find its weight to be 3.306587 grams you weigh a sample and find it weighs 3.306421 grams are the two weights the same? all measurement on a floating-point scale is approximate and, many floating-point values cannot be stored exactly in the computer s floating-point format you the programmer must decide how close is close enough we use the term EPSILON to express close enough in the weighing problem above, maybe close enough is within 0.001 grams

Comparing Floating Point Values int main() { const double EPSILON = 0.001; double weight1 = 3.306587; double weight2 = 3.306421; } bool equal = abs(weight1 - weight2) <= EPSILON; cout << boolalpha << equal << endl; return 0; no warnings correct answer for each situation, you decide an appropriate EPSILON

Bit Patterns every value in a computer is stored as some pattern of bits for an unsigned short, the value 25 is stored as the bit pattern 0000 0000 0001 1001 for a char, the value Q is stored as the bit pattern 0101 0001 for a bool, the value true is stored as the bit pattern 0000 0001, and the value false, as 0000 0000

Truth Values going the other direction, regardless of what it represents in its true data type every bit pattern can be interpreted as a simple unsigned integer so the bit pattern 0101 0001 can be interpreted as 81: 2 6 + 2 4 + 2 0 so the bit pattern 0101 0001 means two different things: Q as a char, and 81 as an unsigned int indeed, in the ASCII chart, you will find the decimal value of Q is 81 in exactly the same way, true is stored internally as 0000 0001, which is decimal 1 and false is stored internally as 0000 0000

Truth Values BUT! false is not the same thing as 0, as Gaddis implies false is a bool value, while 0 is an integer value the two are different even worse, Gaddis says you can use any non-zero value, such as 123, as true UGH! how on Earth does 123 reasonably equal true? it doesn t but Gaddis says it does use true to represent true, and use boolalpha to cause true to be printed as true, not as 1

Program Flow all of the programs we have seen so far are sequential the statements are executed one after another, from beginning to end they start with the first statement after the opening curly brace of main and end with the return 0; just before the closing curly brace of main one statement follows another, without exception

Conditional Execution the real purpose of the relops is to allow conditional execution of statements instead of deterministic execution in which one statement follows another without exception in conditional execution, a statement might be executed or it might not be executed whether it is executed or not depends on a Boolean expression formed from a relop

The if Statement the mechanism that C++ (and most other languages) uses for conditional execution is the if statement the if statement is used to interrupt the sequential flow of program statements the if statement is used to conditionally execute a specific set of program statements maybe yes, maybe no, depending... see program 4 2.cpp, from Gaddis page 158

if (expression) { statement; statement;... } The if Statement Structure if is a reserved word expression is a Boolean expression using relops enclosed in parentheses the braces delimit the body of the if statement multiple statements (one or more) form the body of the if statement; they are indented thus the if statement is a compound statement there is no semicolon after the closing brace the closing brace itself ends the if statement

Problems Gaddis lists a bunch of things that you can do wrong with an if statement if (a = b); foo; bar; missing braces no semicolon assignment