1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S.

Slides:



Advertisements
Similar presentations
1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc.
Advertisements

What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
If Statements & Relational Operators Programming.
Branching Constructs Review l what are branching constructs? what type of branching constructs have we studied? l what is nested if? l what is multiway.
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.
Slide 1 Summary Two basic concepts: variables and assignments Some C++ practical issues: division rule, operator precedence  Sequential structure of a.
Week 4 Selections This week shows how to use selection statements for more flexible programs. It also describes the various integral types that are available.
Conditions What if?. Flow of Control The order of statement execution is called the flow of control Unless specified otherwise, the order of statement.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
If Statements. COMP104 If / Slide 2 Three Program Structures * Sequence - executable statements which the computer processes in the given order * Choice.
ECE122 L7: Conditional Statements February 20, 2007 ECE 122 Engineering Problem Solving with Java Lecture 7 Conditional Statements.
Thursday, December 14, 2006 “Would you tell me, please, which way I ought to go from here?” “That depends a good deal on where you want to get to,” said.
Copyright © 2012 Pearson Education, Inc. Chapter 3 Control Structures: Selection.
1 CS 105 Lecture 5 Logical Operators; Switch Statement Wed, Feb 16, 2011, 5:11 pm.
Boolean Expressions and If Flow of Control / Conditional Statements The if Statement Logical Operators The else Clause Block statements Nested if statements.
Quiz 1 Exam 1 Next Week. Nested if Statements if (myGrade >= 80) if (myGrade >= 90) cout
Section 3 - Selection and Repetition Constructs. Control Structures 1. Sequence 2. Selection 3. Repetition.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 4 Decision Structures and Boolean Logic.
Decision Structures and Boolean Logic
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
More on Input Output Input Stream : A sequence of characters from an input device (like the keyboard) to the computer (the program running). Output Stream.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Conditions Logical Expressions Selection Control Structures Chapter 5.
Programming in Java (COP 2250) Lecture 11 Chengyong Yang Fall, 2005.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Flow Control (Switch, do-while, break) Outline 4.7The.
Basic Of Computer Science
Rational Expressions and selection structures Relational operators Logical operators Selection structures.
CHAPTER 8 CONTROL STRUCTURES Prepared by: Lec. Ghader R. Kurdi.
Chapter 3. Outline Relational Operators Loops Decisions Logical Operators Precedence Summary.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Chapter 5 Logic; Got Any?. Flow of Control The order in which the computer executes statements in a program Control Structure A statement used to alter.
Selection Relational Expressions A condition or logical expression is an expression that can only take the values true or false. A.
Conditional Structures UNIVERSITY OF THE PUNJAB (GUJRANWALA CAMPUS) ADNAN BABAR MT14028 CR
1 Compound Assignment C++ has a large set of operators for applying an operation to an object and then storing the result back into the object Examples.
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Programming 1 DCT 1033 Control Structures I (Selection) if selection statement If..else double selection statement Switch multiple selection statement.
COS120 Software Development Using C++ AUBG Fall semester 2010 Ref book: Problem Solving, Abstraction and Design Using C++ Authors: Frank Friedman, Elliot.
Flow of Control Unless indicated otherwise, the order of statement execution through a method is linear: one after the other in the order they are written.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
Boolean values Gateway to decision making. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements.
If Statements Programming. COMP104 Lecture 7 / Slide 2 Review: Rules for Division l C++ treats integers different than doubles. 100 is an int. l 100.0,
Control statements Mostafa Abdallah
CPS120: Introduction to Computer Science Decision Making in Programs.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decisions. Background Our problem-solving solutions so far have the straight-line property –They execute the same statements for every run of the program.
What is the Result and Type of the Following Expressions? int x=2, y=15;double u=2.0,v=15.0; -xx+yx-y x*vy / xx/yy%xx%y u*vu/vv/uu%v x * u(x+y)*uu /(x-x)
CSE202: Lecture 5The Ohio State University1 Selection Structures.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 15, 2004 Lecture Number: 11.
C++ Programming: From Problem Analysis to Program Design, Fifth Edition Chapter 2: Control Structures (Selection & Repetition)
Decisions Copyright © The McGraw-Hill Companies, Inc. Permission required for reproduction or display.
Chapter 7 Conditional Statements. 7.1 Conditional Expressions Condition – any expression that evaluates to true/false value Relational operators are BINARY.
Selection (if-then-else) Programming Has 3 Types of Control: Sequential (normal): Control of Execution Proceeds One after the Other Selection (if-then-else):
Decision Structures Michele Co CS 101-E. 2 UVa CS101E Spring 2007 Today if statements Logical expressions –Truth tables –Boolean algebra –DeMorgan’s laws.
Chapter 3 Selection Statements
Chapter 3 Control Statements
Chapter 4 : Control Construct.
Selection (also known as Branching) Jumail Bin Taliba by
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
A mechanism for deciding whether an action should be taken
EGR 2261 Unit 4 Control Structures I: Selection
COMS 261 Computer Science I
Expression Review what is the result and type of these expressions?
Relational Operators Operator Meaning < Less than > Greater than
Summary Two basic concepts: variables and assignments Basic types:
Chapter 4: Control Structures I (Selection)
COMS 261 Computer Science I
The switch Statement When we want to compare a variable against several values to see which one it has, we can use the switch statement: switch (status)
Module 3 Selection Structures 6/25/2019 CSE 1321 Module 3.
Controlling Program Flow
Presentation transcript:

1 Chapter 4, Part 1 If Control Construct A mechanism for deciding whether an action should be taken JPC and JWD © 2002 McGraw-Hill, Inc. Modified by S. Sudarshan

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 Output of operators defined by truth tables

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 !

AND/OR Operators Warning & and | are also operators (bit-wise and/or) so be careful what you type Conditional evaluation: E.g. if ((i >= 0) && (sqrt(i) < 5))  second operand (sqrt(i)) evaluated only if first operand (i>=0) is true similarly for ||, second operation evaluated only if first is false

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));

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

Operator Precedence Revisited Precedence of operators (from highest to lowest) Parentheses (brackets) Unary operators Multiplicative operators Additive operators Relational ordering Relational equality Logical and Logical or Assignment Do not assume you will remember this ordering, use parentheses extensively to make meaning very clear

Conditional Constructs Provide Ability to control whether a statement list is executed Two constructs If statement  if  if-else  if-else-if 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 flow charts Expression Action truefalse

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;

Semantics (flowchart)

What is the Output? int m = 5; int n = 10; if (m >= n) ++m; ++n; cout << " m = " << m << " n = " n << endl;

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;

Finding the Max

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

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; case ‘\0’: cout << “end of line” << endl; break; default: cout << ch << " is not a vowel" << endl; }

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; }

19 End of Chapter 4, Part 1

Boolean Algebra Truth tables Lists all combinations of operand values and the result of the operation for each combination Example PQ P and Q False FalseFalse False TrueFalse True FalseFalse True TrueTrue

Boolean Algebra Or truth table PQ P or Q False FalseFalse False TrueTrue True FalseTrue True TrueTrue

Boolean Algebra Not truth table Pnot P False True 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 PQ P and Qnot (P and Q) False FalseFalseTrue False TrueFalseTrue True FalseFalseTrue True TrueTrueFalse

Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24

Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 Yuck! Do not write expressions like this!

Operator Precedence Revisited Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24 However, for your information it is equivalent to ((((5 *15) + 4) == 13) && (12 < 19)) || ((!false) == (5 < 24)) Consider 5 * == 13 && 12 < 19 || !false == 5 < 24  However, for your information it is equivalent to

Example if (Value < 0) { Value = -Value; }

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 Action 1 Action 2 true false