Conditions & Boolean Expressions



Similar documents
Chapter 5. Selection 5-1

Name: Class: Date: 9. The compiler ignores all comments they are there strictly for the convenience of anyone reading the program.

Chapter 8 Selection 8-1

While Loop. 6. Iteration

Boolean Expressions, Conditions, Loops, and Enumerations. Precedence Rules (from highest to lowest priority)

What is a Loop? Pretest Loops in C++ Types of Loop Testing. Count-controlled loops. Loops can be...

Keywords are identifiers having predefined meanings in C programming language. The list of keywords used in standard C are : unsigned void

9 Control Statements. 9.1 Introduction. 9.2 Objectives. 9.3 Statements

Outline. Conditional Statements. Logical Data in C. Logical Expressions. Relational Examples. Relational Operators

The C++ Language. Loops. ! Recall that a loop is another of the four basic programming language structures

Moving from C++ to VBA

Lecture 2 Notes: Flow of Control

if and if-else: Part 1

Selection Statements

Embedded Systems. Review of ANSI C Topics. A Review of ANSI C and Considerations for Embedded C Programming. Basic features of C

MS Visual C++ Introduction. Quick Introduction. A1 Visual C++

COMP 110 Prasun Dewan 1

Comp151. Definitions & Declarations

VB.NET Programming Fundamentals

Conditional Statements Summer 2010 Margaret Reid-Miller

PROBLEM SOLVING SEVENTH EDITION WALTER SAVITCH UNIVERSITY OF CALIFORNIA, SAN DIEGO CONTRIBUTOR KENRICK MOCK UNIVERSITY OF ALASKA, ANCHORAGE PEARSON

13 Classes & Objects with Constructors/Destructors

QUIZ-II QUIZ-II. Chapter 5: Control Structures II (Repetition) Objectives. Objectives (cont d.) 20/11/2015. EEE 117 Computer Programming Fall

The if Statement and Practice Problems

C++ Outline. cout << "Enter two integers: "; int x, y; cin >> x >> y; cout << "The sum is: " << x + y << \n ;

Example of a Java program

6. Control Structures

Boolean Expressions 1. In C++, the number 0 (zero) is considered to be false, all other numbers are true.

Operator Overloading. Lecture 8. Operator Overloading. Running Example: Complex Numbers. Syntax. What can be overloaded. Syntax -- First Example

Object Oriented Software Design

Statements and Control Flow

PIC 10A. Lecture 7: Graphics II and intro to the if statement

CS 141: Introduction to (Java) Programming: Exam 1 Jenny Orr Willamette University Fall 2013

Chapter One Introduction to Programming

Passing 1D arrays to functions.

JavaScript: Control Statements I

C++FA 5.1 PRACTICE MID-TERM EXAM

Calling the Function. Two Function Declarations Here is a function declared as pass by value. Why use Pass By Reference?

Ch 7-1. Object-Oriented Programming and Classes

Storage Classes CS 110B - Rule Storage Classes Page 18-1 \handouts\storclas

Basic Programming and PC Skills: Basic Programming and PC Skills:

Computer Programming C++ Classes and Objects 15 th Lecture

Common Beginner C++ Programming Mistakes

El Dorado Union High School District Educational Services

J a v a Quiz (Unit 3, Test 0 Practice)

The While Loop. Objectives. Textbook. WHILE Loops

C++ Input/Output: Streams

Chapter 3 Operators and Control Flow

CISC 181 Project 3 Designing Classes for Bank Accounts

Pemrograman Dasar. Basic Elements Of Java

PART-A Questions. 2. How does an enumerated statement differ from a typedef statement?

Introduction to Java Applications Pearson Education, Inc. All rights reserved.

Appendix K Introduction to Microsoft Visual C++ 6.0

7.7 Case Study: Calculating Depreciation

COMPUTER SCIENCE 1999 (Delhi Board)

Introduction to Programming (in C++) Loops. Jordi Cortadella, Ricard Gavaldà, Fernando Orejas Dept. of Computer Science, UPC

Example. Introduction to Programming (in C++) Loops. The while statement. Write the numbers 1 N. Assume the following specification:

Sources: On the Web: Slides will be available on:

AP Computer Science Java Subset

ESCI 386 IDL Programming for Advanced Earth Science Applications Lesson 6 Program Control

Phys4051: C Lecture 2 & 3. Comment Statements. C Data Types. Functions (Review) Comment Statements Variables & Operators Branching Instructions

JavaScript: Introduction to Scripting Pearson Education, Inc. All rights reserved.

UIL Computer Science for Dummies by Jake Warren and works from Mr. Fleming

First Java Programs. V. Paúl Pauca. CSC 111D Fall, Department of Computer Science Wake Forest University. Introduction to Computer Science

Conditionals (with solutions)

Part I. Multiple Choice Questions (2 points each):

CS 111 Classes I 1. Software Organization View to this point:

CompSci 125 Lecture 08. Chapter 5: Conditional Statements Chapter 4: return Statement

IS0020 Program Design and Software Tools Midterm, Feb 24, Instruction

Moving from CS 61A Scheme to CS 61B Java

Informatica e Sistemi in Tempo Reale

Chapter 5 Functions. Introducing Functions

Translating to Java. Translation. Input. Many Level Translations. read, get, input, ask, request. Requirements Design Algorithm Java Machine Language

CS 241 Data Organization Coding Standards

JAVA - QUICK GUIDE. Java SE is freely available from the link Download Java. So you download a version based on your operating system.

Sequential Program Execution

Selection: Boolean Expressions (2) Precedence chart:

Boolean Expressions & the if Statement

CSCI 123 INTRODUCTION TO PROGRAMMING CONCEPTS IN C++

Summit Public Schools Summit, New Jersey Grade Level / Content Area: Mathematics Length of Course: 1 Academic Year Curriculum: AP Computer Science A

Lecture 3. Arrays. Name of array. c[0] c[1] c[2] c[3] c[4] c[5] c[6] c[7] c[8] c[9] c[10] c[11] Position number of the element within array c

Answers to Review Questions Chapter 7

Introduction to C++ Introduction to C++ Week 7 Dr Alex Martin 2013 Slide 1

C++ Language Tutorial

The Payroll Program. Payroll

Software Testing. Definition: Testing is a process of executing a program with data, with the sole intention of finding errors in the program.

1 Description of The Simpletron

MULTIPLE CHOICE. Choose the one alternative that best completes the statement or answers the question.

I PUC - Computer Science. Practical s Syllabus. Contents

COS 217: Introduction to Programming Systems

Object Oriented Software Design II

Boolean Data Outline

C++ INTERVIEW QUESTIONS

Object Oriented Software Design

CpSc212 Goddard Notes Chapter 6. Yet More on Classes. We discuss the problems of comparing, copying, passing, outputting, and destructing

Transcription:

Conditions & Boolean Expressions 1 In C++, in order to ask a question, a program makes an assertion which is evaluated to either true (nonzero) or false (zero) by the computer at run time. Example: In order to ask the question "Is the student s age above or equal to 21?", in C++ this might be written: const int LEGALAGE = 21 ; bool islegalage; islegalage = (stuage >= LEGALAGE ); The value of islegalage can now be tested to see if it is true or false. The question can now be stated: if (islegalage) cout << OK ; else cout << Nope ;

Boolean Variables in C++ 2 Recall Standard C++ supports a simple data type specialized for representing logical values. The bool type was finalized fairly late in the evolution of the C++ Standard and is not yet supported by all compilers. bool type variables can have either of two values: true or false The names true and false are C++ keywords. Internally, true and false are represented by the values 1 and 0, respectively. While not recommended here, the values true and false may be converted (assigned) to int variables: int ismoreinput = true; // ismoreinput == 1 int doneyet = false; // doneyet == 0

C++ versus C Booleans 3 In C++, bool is a simple data type built into the language. C++ variables declared as type bool can be used in the natural and obvious way. In C, there is no Boolean type variable. Instead, integer values are used to represent the concepts of true and false. The convention is that 0 (zero) represents false, and that any nonzero value (typically 1) is interpreted as representing true. Thus, in C, one might write the following (compare to slide 5.1): const int LEGALAGE = 21 ; int islegalage; // Can have any int value. islegalage = (stuage >= LEGALAGE ); Now, the variable islegalage will have an integer value, interpreted as described. So we could then still write: if (islegalage) cout << OK ; else cout << Nope ;

Forms of Boolean Expressions 4 Boolean expressions can, generally, take one of two forms: 1) an (arithmetic) expression followed by a relational operator followed by an (arithmetic) expression; for example: ( (b * b) - 4 * a * c ) > 0 C++ s six standard relational operators: = =! = > >= < <= Do not try to compare two floats for exact equality (= =).

Boolean Expression Examples 5 Given the following declarations and assignments: char mi = L, mi2 = g ; int i1 = 23, i2 = 17; int grade1 = 76, grade2 = 87; Evaluate: i1 == i2 (grade1 >= grade2) mi == mi2 mi < mi2 grade1 > mi grade1 + i1 <= grade2 - i2 Z < a

Forms of Boolean Exprs (cont) 6 2) a Boolean expression followed by a Boolean operator followed by a Boolean expression (with negation being an exception) Boolean (or logical) operators: Logical Operation C++ Notation Not! And && Or Given: int age = 17, fredsage = 65, legal = 21, billsage = 19; Evaluate: 13 <= age && age <= 19 (billsage >= legal) (fredsage >= legal)! ( age <= legal ) fredsage < billsage

Truth Tables 7 Elementary logic is used to evaluate Boolean expressions involving Boolean operators: X Y Not X X AND Y X OR Y T T F T T T F F F T F T T F T F F T F F A B C (NOT A) OR B A OR (B AND (NOT C)) T T T T T F T F T T F F F T T F T F F F T F F F

Short Circuiting 8 C++ is very economical (maybe too economical) when evaluating Boolean expressions. If in the evaluation of a compound Boolean expression, the computer can determine the value of the entire expression without any further evaluation, it does so. This is called short circuiting. What does this mean for us? Given: int age = 17, fredsage = 65, legal = 21, billsage = 19; (13 <= age) (age <= 19) (billsage >= legal) && (fredsage >= legal)!( legal <= age )!(billsage <= legal) (fredsage < billsage) &&!(fredsage > 36)

C++ Operator Hierarchy 9 Boolean expressions can involve both arithmetic & Boolean operators. Therefore C++ defines a complete operator evaluation hierarchy: First, expressions in parentheses are evaluated. Then: (1) - (unary)! (2) * / % (3) + - (4) <= >= < > (5) ==!= (6) && (7) (8) = Operators in the groups (2) thru (7) are evaluated left to right. Operators in the groups (1) and (8) are evaluated right to left.

C-style Boolean Examples 10 Given: Evaluate: int i = 3, k = 5, j = 0, m = -2 ; i && j i j!k i!m && k 3*i - 4/k < 2 i + j < k (i > 0) && (j < 7) (i < k) (j < 7) (m > 5) (j > 0)

Control Flow 11 Program Flow of Control: the order in which the computer executes statements in a program. Default Control Flow is sequential execution. statement 1 statement 2 statement 3 Control Structure - a statement that is used to alter the normal sequential flow of control Selection or alternation - a control structure that allows a choice of two or more actions

Selection: if...else 12 if (grade = = A ) cout << "Good Job!"; if (grade = = A ) cout << "Good Job!"; else cout << "Grades aren t everything. "; if <boolean expression> False True <stmt> if (grade = = A ) cout << "Good Job!"; else { cout << "Grades aren t everything." << endl; cout << "But they help."; } if else <boolean expression> False True <stmt> <stmt>

Nested if Statements 13 The statement under control of an if/else can be an if statement: cout << "Your semester grade is "; if (average >= 90) cout << "A" << endl; else if (average >= 80) cout << "B" << endl; else if (average >= 70) cout << "C" << endl; else if (average >= 60) cout << "D" << endl; else cout << "F" << endl; Conditions that are "mutually exclusive", (one condition being true excludes all others from being true), should be tested for with nested ifs, (as opposed to disjoint ifs), for efficiency. Note the layout and indenting style.

Nested if Examples 14 Given 3 int vars (a,b,c), having unique values, output the values in descending order: if (a > b) { // Get order of a and b; // if clause if a is larger if (a > c) // a is largest; now // sort out b and c if (b > c) cout << a << b << c; // c is smallest else cout << a << c << b; // c is middle else cout << c << a << b; // c is largest } else { // else clause if b is larger } if (b > c) // b is largest; now // sort out a and c if (a > c) cout << b << a << c; // c is smallest else cout << b << c << a; // c is middle else cout << c << b << a; // c is largest

Nested if Examples 15 For readability, the Boolean condition in an if statement should indicate as clearly as possible the purpose of the statement. When if statements are nested, we may be able to improve the clarity of the code: Mediocre Style: if ( midterm >= 80 ) if ( exam >= 80 ) cout << "Great! You earned two Bs." << endl; Improved Style: if (( midterm >= 80 ) && ( exam >= 80 )) cout << " Great! You earned two Bs. " << endl;

Dangling else 16 Sometimes, it seems unclear to which if statement an else clause belongs. if ( grade == A ) if ( rank <= 5 ) cout << "fantastic" << endl; else cout << "still good" << endl; Rule: an else is associated with the most recent, uncompleted if statement. Using proper indentation to make the clauses line up properly, if ( grade == A ) if ( rank <= 5 ) cout << "fantastic" << endl; else cout << "still good" << endl; aids readability, but does not alter the meaning. Judicious use of brackets ({}) can also clarify your intent greatly (even to yourself!).

Execution Trace Program 17 #include <iostream.h> void main( ) { const int Gregorian = 1752 ; int year ; bool yeardivisibleby4, yeardivisibleby100, yeardivisibleby400 ; 1 2 3 4 5 6 7 8 9 10 11 12 13 14 } cout << This program determines if a year of a Gregorian calendar is a leap year << endl; cout << Enter the possible leap year ; cin >> year ; if (year < Gregorian) { cout << endl << "The year tested must be on the Gregorian calendar." << endl ; cout << "Reenter the possible leap year " ; cin >> year ; } // end of if (year < Gregorian ) yeardivisibleby4 = (( year % 4 ) == 0) ; yeardivisibleby100 = (( year % 100 ) == 0) ; yeardivisibleby400 = (( year % 400 ) == 0) ; if ( ((yeardivisibleby4) && (! yeardivisibleby100)) (yeardivisibleby400) ) cout << The year << year << is a leap year. << endl; else cout << The year << year << is NOT a leap year. << endl ;

Execution Trace 18 Execution Trace (Desk-Checking) - hand calculating the output of a program with test data by mimicking the actions of the computer. statement 1 2 3 4 5 6 7 variables year yeardivisibleby4 yeardivisibleby100 yeardivisibleby400 8 9 10 11 12 13 14

Switch Statement 19 Multiple selection control statement. switch (selector) { case <label1>: <statements 1>; break; case <label2>: <statements 2>; break;.. case <labeln>: <statements n>; break; default: <statements d> } <label i> is a single constant value; each label must be different from the others. The selector must be an ordinal type (int or char) variable. When the switch statement is executed, the selector is evaluated and the statement corresponding to the matching constant in the unique label list is executed. If no match occurs, the default clause is selected, if present. The type of selector must match the type of the constants in the label lists.

Switch Example 20 If the result of the selector does not match any of the constants in the label lists, the action that results is the default (if any) according to the language standard. If there is no default case, then no action is taken. switch ( LetterGrade ) { case A : case B : cout << "Good job!"; break; case C : cout << "Average"; break; case D : cout << "In danger;"<<endl; cout << "be careful!"; break; case F : cout << "Failing."; countf = countf + 1; break; default: cout << "Error: invalid grade"; } Important: the break statement is required after the body of a case to prevent execution of the following case body.

Switch Limitations 21 A switch statement can only be used in cases involving an equality comparison for a variable that is of integral type (i.e., char or int). Therefore, a switch cannot be used when checking values of a float or double variable. Note: the nested if else on slide 5.13 cannot be replaced with an equivalent switch statement because the decisions are based on inequality comparisons.

C Language assert() Function 22 By including the <assert.h> header at the beginning of a program one can call the assert function to perform error checking: #include <assert.h>.. assert(year > 0); //check for leap year If the result of the evaluation of the Boolean expression, (that is the parameter given to assert0, is true execution proceeds to the next statement and no action is taken. However, if the expression is false the program execution is halted and an error message is output. Assertion failed: year > 0, file date.cpp, line 84

Assert Debugging Control 23 Calls to assert allows programmers to state preconditions and postconditions explicitly in their code instead of simply including them as comments. This utilizes the power of the language to do error checking. Assertions should only be used for testing and debugging. They should be removed before a program is given to others to use. (Non-programmers will not be able to understand assert output if they encounter it.) Asserts do not need to be physically removed. C provides a mechanism to instruct the compiler to ignore the assert statements through the inclusion of a language directive. To logically remove asserts one must use the statement: #define NDEBUG prior to including the assert header file. #define NDEBUG #include <assert.h>.. assert(year > 0); //check for leap year