Programming in c++ Conditions, Logical EXpressions and the Selection Control Structures Dale/eems/Headington
1 Conditions, Logical Expressions, and the Selection Control Structures
Programming in C++ Chapter 5 Topics Data Type bool % Using Relational and Logical Operators to Construct and Evaluate Logical Expressions oIfThen-Else Statements %If-Then Statements Nested If Statements for Multi-way Branching Testing the State of an I/O Stream Testing a C++ Program
2 Chapter 5 Topics ❖Data Type bool ❖Using Relational and Logical Operators to Construct and Evaluate Logical Expressions ❖If-Then-Else Statements ❖If-Then Statements ❖Nested If Statements for Multi-way Branching ❖Testing the State of an I/O Stream ❖Testing a C++ Program
Programming in C++ Flow of Control &the order in which program statements are executed WHAT ARE THE POSSIBILITIES
3 Flow of Control ❖the order in which program statements are executed WHAT ARE THE POSSIBILITIES. .
Programming in C++ Flow of Control is Sequential unless a control structure'is used to change that o there are 2 general types of control structures Selection(also called branching) Repetition (also called looping)
4 Flow of Control ❖is Sequential unless a “control structure” is used to change that ❖there are 2 general types of control structures: Selection (also called branching) Repetition (also called looping)
Programming in C++ C++ control structures Selection ff else switch Repetition for loop while loop do. while loop
5 C++ control structures ❖Selection if if . . . else switch ❖Repetition for loop while loop do . . . while loop
Programming in C++ Logic EXpression >Alogic expression(Boolean expression) is made up of logical values and operations )Every logical expression has one of two values: true or false. > Here are some examples i A Boolean variable or constant iAn expression followed by a relational operator followed by an expression ii. A logical expression followed by a logical operator followed by a logical expression
6 Logic Expression ➢A logic expression (Boolean expression) is made up of logical values and operations. ➢Every logical expression has one of two values: true or false. ➢Here are some examples: i. A Boolean variable or constant ii.An expression followed by a relational operator followed by an expression iii. A logical expression followed by a logical operator followed by a logical expression
Programming in C++ bool Data Type type bool is a built-in type consisting of just 2 values, the constants true and false True and false are reserved words in C++, not variable names or string owe can declare variables of type bool bool hasFever; // true if has high temperature bool isSenior; //true if age is at least 55
7 bool Data Type ❖type bool is a built-in type consisting of just 2 values, the constants true and false ❖True and false are reserved words in C++, not variable names or string ❖we can declare variables of type bool bool hasFever; // true if has high temperature bool isSenior; // true if age is at least 55
Programming in C++ The operators used in logical expressions )6 Relational Operators )3 Logical Operators &1
8 The operators used in logical expressions ➢6 Relational Operators >= == != ➢3 Logical Operators ! && ||
Programming in C++ 6 Relational Operators are used In expressions of form. ExpressionA Operator EXpression B For example: temperature humidity B*B-4.0*A*C>0.0 abs(number 35 initial Q
9 are used in expressions of form: ExpressionA Operator ExpressionB For example: temperature > humidity B * B - 4.0 * A * C > 0.0 abs (number ) == 35 initial != ‘Q’ 6 Relational Operators
Programming in C++ int x, y: X=4 y=6; EXPRESSION VALUE x=y true y==X false y==x+2 true y=x+3 7(true) aution: Don't confuse the assignment operator() and the relational operator (= 10
10 int x, y ; x = 4; y = 6; EXPRESSION VALUE x = y true y == x false y == x+2 true y = x + 3 7 (true) Caution: Don’t confuse the assignment operator (=) and the relational operator (==)