If Control Construct

Similar documents
Boolean Algebra Boolean Algebra

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

Iterative Constructs

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

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

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

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

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

C++ Final Exam 2017/2018

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

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

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

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

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

Chapter 4 - Notes Control Structures I (Selection)

Boolean Algebra A B A AND B = A*B A B A OR B = A+B

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

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

5. Selection: If and Switch Controls

CSI33 Data Structures

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.

PRG PROGRAMMING ESSENTIALS. Lecture 2 Program flow, Conditionals, Loops

Programming Language. Control Structures: Repetition (while) Eng. Anis Nazer Second Semester

Control Structures: The IF statement!

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

5. Control Statements

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

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

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

Relational Operators and if. Class 10

1.3b Type Conversion

Increment and the While. Class 15

The following expression causes a divide by zero error:

Introduction to Programming

Chapter 2: Overview of C++

Conditional Statement

Data Structures and Algorithms

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

Chapter 2. C++ Basics

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

CSCE 110 PROGRAMMING FUNDAMENTALS

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

JAVA OPERATORS GENERAL

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

Chapter 4: Basic C Operators

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

Recognize the correct ordering of decisions in multiple branches Program simple and complex decision

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

PIC 10A Flow control. Ernest Ryu UCLA Mathematics

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

CS2141 Software Development using C/C++ C++ Basics

Add Subtract Multiply Divide

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

Prof. Navrati Saxena TA: Rochak Sachan

The Arithmetic Operators. Unary Operators. Relational Operators. Examples of use of ++ and

The Arithmetic Operators

Programming Language. Functions. Eng. Anis Nazer First Semester

CSCE 121 ENGR 112 List of Topics for Exam 1

Programming Lecture 3

Computer Programming : C++

1) What of the following sets of values for A, B, C, and D would cause the string "one" to be printed?

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

Lecture 3 Tao Wang 1

Introduction to Programming using C++

Chapter 4: Control Structures I (Selection)

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

3/12/2018. Structures. Programming in C++ Sequential Branching Repeating. Loops (Repetition)

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

CS242 COMPUTER PROGRAMMING

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

Chapter 4. Computation. Bjarne Stroustrup.

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

Checking Multiple Conditions

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

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

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

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

REPETITION CONTROL STRUCTURE LOGO

CHAPTER 3 BASIC INSTRUCTION OF C++

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

VARIABLES & ASSIGNMENTS

Welcome Back. CSCI 262 Data Structures. Hello, Let s Review. Hello, Let s Review. How to Review 8/19/ Review. Here s a simple C++ program:

Operators and Expressions in C & C++ Mahesh Jangid Assistant Professor Manipal University, Jaipur

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

1 Lexical Considerations

Flow of Control. bool Data Type. Flow Of Control. Expressions. C++ Control Structures < <= > >= ==!= ! &&

FORM 1 (Please put your name and form # on the scantron!!!!) CS 161 Exam I: True (A)/False(B) (2 pts each):

5. Assuming gooddata is a Boolean variable, the following two tests are logically equivalent. if (gooddata == false) if (!

The C++ Language. Arizona State University 1

conditional statements

Lexical Considerations

Test Bank for Problem Solving with C++: The Object of Programming, 8/e Chapter 2 C++ Basics

Module 3 SELECTION STRUCTURES 2/15/19 CSE 1321 MODULE 3 1

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

Chapter 5. Conditions, Logical Expressions, and Selection Control Structures

Chapter 2: Basic Elements of C++

Chapter 5 Topics. Chapter 5 Topics. Flow of Control. bool Data Type. Flow of Control. Chapter 5

Outline. Why do we write functions? Introduction to Functions. How do we write functions? Using Functions. Introduction to Functions March 21, 2006

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

Chapter 3. More Flow of Control

Transcription:

If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD 2002 McGraw-Hill, Inc. 1 Boolean Algebra Logical expressions have the one of two values - true or false A rectangle has three sides The instructor has a pleasant smile The branch of mathematics is called Boolean algebra Developed by the British mathematician George Boole in the 19th century Three key logical operators And Or Not 1

Boolean Algebra Truth tables Lists all combinations of operand values and the result of the operation for each combination Example P Q P and Q False False False False True False True False False True True True Boolean Algebra Or truth table P Q P or Q False False False False True True True False True True True True 2

Boolean Algebra Not truth table P False True not P True False Boolean Algebra Can create complex logical expressions by combining simple logical expressions Example not (P and Q) A truth table can be used to determine when a logical expression is true P Q P and Q not (P and Q) False False False True False True False True True False False True True True True False 3

A Boolean Type C++ contains a type named bool Type bool has two symbolic constants true false Boolean operators The and operator is && The or operator is The not operator is! Warning & and are also operators so be careful what you type A Boolean Type Example logical expressions bool P = true; bool Q = false; bool R = true; bool S = (P && Q); bool T = ((!Q) R); bool U =!(R && (!Q)); 4

Relational Operators Equality operators ==!= Examples int i = 32; int k = 45; bool q = (i == k); bool r = (i!= k); Relational Operators Ordering operators < > >= <= Examples int i = 5; int k = 12; bool p = (i < 10); bool q = (k > i); bool r = (i >= k); bool s = (k <= 12); 5

Operator Precedence Revisited Precedence of operators (from highest to lowest) Parentheses Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19!false == 5 < 24 6

Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19!false == 5 < 24 Yuck! Do not write expressions like this! Operator Precedence Revisited Consider 5 * 15 + 4 == 13 && 12 < 19!false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) ((!false) == (5 < 24)) 7

Conditional Constructs Provide Ability to control whether a statement list is executed Two constructs If statement if if-else if-else-ef Switch statement Left for reading The Basic If Statement Syntax if (Expression) Action If the Expression is true then execute Action Action is either a single statement or a group of statements within braces Expression true Action false 8

Example if (Value < 0) { Value = -Value; If Value is less than zero then we need to update its value to that of its additive inverse Value < 0 WUXH Value = -Value Is our number negative? IDOVH If Value is not less than zero then our number is fine as is Our number is now definitely nonnegative Sorting Two Numbers cout << "Enter two integers: "; int Value1; int Value2; cin >> Value1 >> Value2; if (Value1 > Value2) { int RememberValue1 = Value1; Value1 = Value2; Value2 = RememberValue1; cout << "The input in sorted order: " << Value1 << " " << Value2 << endl; 9

Semantics Rearrange value1 and value2 to put their values in the proper order value2 < value1 WUXH Are the numbers out of order IDOVH int remembervalue1 = value1 value1 = value2 value2 = remembervalue1 The numbers were rearranged into the proper order The numbers are in order The numbers were initially in order What is the Output? int m = 5; int n = 10; if (m < n) ++m; ++n; cout << " m = " << m << " n = " n << endl; 10

The If-Else Statement Syntax if (Expression) Action 1 else Action 2 If Expression is true then execute Action 1 otherwise execute Action 2 if (v == 0) { cout << "v is 0"; else { cout << "v is not 0"; Expression true false Action 1 Action 2 Finding the Max cout << "Enter two integers: "; int Value1; int Value2; cin >> Value1 >> Value2; int Max; if (Value1 < Value2) { Max = Value2; else { Max = Value1; cout << "Maximum of inputs is: " << Max << endl; 11

Finding the Max Is Value2 larger than Value1 Yes, it is. So Value2 is larger than Value1. In this case, Max is set to Value2 WUXH Value1 < Value2 IDOVH No, its not. So Value1 is at least as large as Value2. In this case, Max is set to Value1 Max = Value2 Max = Value1 Either case, Max is set correctly Selection It is often the case that depending upon the value of an expression we want to perform a particular action Two major ways of accomplishing this choice if-else-if statement if-else statements glued together Switch statement An advanced construct 12

An If-Else-If Statement if ( nbr < 0 ){ cout << nbr << " is negative" << endl; else if ( nbr > 0 ) { cout << nbr << " is positive" << endl; else { cout << nbr << " is zero" << endl; A Switch Statement switch (ch) { case 'a': case 'A': case 'e': case 'E': case 'i': case 'I': case 'o': case 'O': case 'u': case 'U': cout << ch << " is a vowel" << endl; break; default: cout << ch << " is not a vowel" << endl; 13

cout << "Enter simple expression: "; int Left; int Right; char Operator; cin >> Left >> Operator >> Right; cout << Left << " " << Operator << " " << Right << " = "; switch (Operator) { case '+' : cout << Left + Right << endl; break; case '-' : cout << Left - Right << endl; break; case '*' : cout << Left * Right << endl; break; case '/' : cout << Left / Right << endl; break; default: cout << "Illegal operation" << endl; 14