[go: up one dir, main page]

0% found this document useful (0 votes)
11 views11 pages

Lecture 3

test

Uploaded by

MrHS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views11 pages

Lecture 3

test

Uploaded by

MrHS
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

9/1/2022

OMAR AL-MUKHTAR UNIVERSITY


COLLEGE OF ENGINEERING

C++ PROGRAMMING LANGUAGE

Lecture #3
Control Structures I (Selection)

Dr. Ala Alaker


1

Outline
1. Learn about control structures
2. Examine relational operators
3. Examine relational operators and simple data type
4. Explore how to form and evaluate logical (Boolean)
expressions
5. Discover how to use the selection control structures if,
if...else in a program
6. Discover how to use the multiple selection: Nested if
7. Learn how the conditional operator ?: works
8. Discover how to use the selection control structures
switch in a program 2

Dr. Ala Alaker 1


9/1/2022

Control Structures
• A computer can proceed:
 In sequence
 Selectively (branch) - making a choice
 Repetitively (iteratively) – looping

• Some statements are executed only if certain


conditions are met.

• A condition is met if it evaluates to true

Control Structures (continued)

Flow of execution
4

Dr. Ala Alaker 2


9/1/2022

Relational Operators
• To make decisions, you must be able to express
conditions and make comparisons.
• A condition is represented by a logical (Boolean)
expression that has a value of either true or false
• true and false are logical (Boolean) values.
• Consider the expression:
i>j
If this expression is a logical expression, it will have the
value true if the value of i is greater than the value of j,
otherwise, it will have the value false.
• The symbol > is called a relational operator. A relational
operator allows you to make comparisons in a program.

• Relational operators:
 Allow comparisons
 Require two operands (binary)
 Evaluate to true or false
• Relational Operators in C++:
Operator Description
== equal to
!= not equal to
< less than
<= less than or equal to
> greater than
>= greater than or equal to
6

Dr. Ala Alaker 3


9/1/2022

Relational Operators and


Simple Data Types

• You can use the relational operators with all


three simple data types:
– 8 < 15 evaluates to true
– 6 != 6 evaluates to false
– 2.5 > 5.8 evaluates to false
– 5.9 <= 7.5 evaluates to true

Comparing Characters
Value
Expression Explanation
Expression
The ASCII value of ' ' is 32 and the
ASCII value of 'a' is 97, because
' ' < 'a' true
32 < 97 is true, it follows that '
' < 'a' is true.

The ASCII value of 'R' is 82 and the


ASCII value of 'T' is 84, because
'R' > 'T' False
82 > 84 is false, it follows that
'R' > 'T' is false.

The ASCII value of '+' is 43 and the


ASCII value of '*' is 42, because
'+' < '*' False 43 < 42 is false, it follows that
'+' < '*'
is false.
8

Dr. Ala Alaker 4


9/1/2022

Logical (Boolean) Operators


and Logical Expressions
• Logical (Boolean) operators enable you to combine
logical expressions
• C++ has three logical (Boolean) operators, as shown in
table below:
Operator Description
! not
&& and
|| or

• The operator ! is unary, so it has only one operand. The


operators && and || are binary operators.
9

• The && (And) Operator


Expression Value Explanation
Because (14 >= 5) is true, ('A' < 'B')
(14 >= 5) &&
true is true, and true && true is true, the
('A' < 'B')
expression evaluates to true.
Because (24 >= 35) is false, ('A' < 'B')
(24 >= 35) &&
false is true, and false && true is false, the
('A' < 'B')
expression evaluates to false.
• The || (Or) Operator
Expression Value Explanation
Because (14 >= 5) is true, ('A' > 'B')
(14 >= 5) &&
true is false, and true || false is true, the
('A' > 'B')
expression evaluates to true.
Because (24 >= 35) is false, ('A' > 'B')
(24 >= 35) &&
false is false, and false && false is false, the
('A' > 'B')
expression evaluates to false. 10

Dr. Ala Alaker 5


9/1/2022

• Table below defines the operator ! (not). When you use the
! operator, !true is false and !false is true. Putting ! in front
of a logical expression reverses the value of that logical
expression

Expression Value Explanation


Because 'A' > 'B' is false, !('A' > 'B')is
!('A' > 'B') true
true.
!(6 <= 7) false Because 6 <= 7 is true, !(6 <= 7)is false.

11

#include <iostream>
using namespace std;
int main() What is the
{ output?
bool found = true;
double hours = 45.30;
double overTime = 15.00;
int count = 20;
char ch = 'B';
cout << !found << endl;
cout << (hours > 40.00) << endl;
cout << (!found && (hours >= 0)) << endl;
cout << (!(found && (hours >= 0))) << endl;
cout << (hours + overTime <= 75.00) << endl;
cout << ((count >= 0) && (count <= 100)) << endl;
cout << ('A' <= ch && ch <= 'Z') << endl;
return 0;
} 12

Dr. Ala Alaker 6


9/1/2022

Selection: if and if...else


One-Way Selection: - if (expression) {
statement(s);
}
Two-Way Selection:- if (expression) {
statement(s)1;
}
else {
statement(s)2;
}

Multiple Selections:- if (expression 1) {


statement(s)1;
}
else if (expression 2){
statement(s)2;
}
else if (expression3){
statement(s)3;}
else {
statement(s)4;
} 13
Selection: if and if...else

One-Way Selection: -
if (suitcaseDimension > 108 || suitcaseWeight > 50){
additionalCharges = 50.00;
}
Two-Way Selection:-
if (age >= 18) {
cout << "Eligible to vote." << endl;
}
Else {
cout << "Not eligible to vote." << endl;
}
Multiple Selections:-
if (balance > 50000.00) {
interestRate = 0.07 }
else if (balance >= 25000.00) {
interestRate = 0.05; }
else if (balance >= 1000.00){
interestRate = 0.03; }
else {
interestRate = 0.00; } 14

Dr. Ala Alaker 7


9/1/2022

Multiple Selections: Nested if

if (temperature >= 50) {


if (temperature >= 80) {
cout << "Good day for swimming." << endl;
}
else {
cout << "Good day for golfing." << endl;
}}
Else {
cout << "Good day to play tennis." << endl;
}

15

Associativity of Relational Operators:


A Precaution
• The following program, which determines if a number is
between 0 and 10 :
#include <iostream> What is
using namespace std; wrong
int main() with it?
{
int num;
cout << "Enter an integer: ";
cin >> num;
cout << endl;
if (0 <= num <= 10)
cout << num<<"is within 0 and 10."<< endl;
else
cout << num<<"is not within 0 and 10."<< endl;
return 0;
16
}

Dr. Ala Alaker 8


9/1/2022

Sample Runs:
 Sample Run 1:
Enter an integer: 5
5 is within 0 and 10.
 Sample Run 2:
Enter an integer: 20
20 is within 0 and 10.
 Sample Run 3:
Enter an integer: -10
-10 is within 0 and 10.

The expression 0 <= num <= 10? It is missing the logical operator
&&.
A correct way to write this expression in is:
0 <= num && num <= 10
17

Conditional Operator (?:)


• ?: is a ternary operator, which means that it takes three
arguments. The syntax for using the conditional operator is:
expression1 ? expression2 : expression3
• This type of statement is called a conditional expression. The
conditional expression is evaluated as follows:
• If expression1 evaluates to a nonzero integer (that is, to true),
the result of the conditional expression is expression2.
Otherwise, the result of the conditional expression is
expression3.
• Consider the following statements:
if (a >= b)
max = a; max = (a >= b) ? a : b;
else
max = b; 18

Dr. Ala Alaker 9


9/1/2022

switch Structures
• switch structure gives the computer the power to choose
from among many alternatives.
• A general syntax of the switch statement is:
switch (expression)
{
case value1:
The switch statement executes according to the following
statements1 rules:
break; 1. When the value of the expression is matched against a
case value2: case value (also called a label), the statements execute
statements2 until either a break statement is found or the end of the
break; switch structure is reached.
. 2. If the value of the expression does not match any of the
. case values, the statements following the default label
case valuen: execute. If the switch structure has no default label and if
the value of the expression does not match any of the case
statementsn
values, the entire switch statement is skipped.
break; 3. A break statement causes an immediate exit from the
default: switch structure.
statements
19
}

Consider the following statements, in which grade is a


variable of type char.
switch (grade)
{
case 'A':
cout << "The grade point is 4.0.";
break;
case 'B':
cout << "The grade point is 3.0.";
break;
case 'C':
cout << "The grade point is 2.0.";
break;
case 'D':
cout << "The grade point is 1.0.";
break;
case 'F':
cout << "The grade point is 0.0.";
break;
default:
cout << "The grade is invalid.";
20
}

Dr. Ala Alaker 10


9/1/2022

Summary
• Control structures alter normal control flow • All opera
• Most common control structures are selection and integers a
repetition expression

• Relational operators: ==, <, <=, >, >=, != • Mixed ex


decimal nu
• Logical expressions evaluate to 1 (true) or 0
(false) • Use the ca
from one d
• Logical operators: ! (not), && (and), || (or)
• A named c
• All variabl
21

Summary

• selection structures: one-way selection, two-way


selection and multiple selections.
• No stand-alone else statement in C++ ( Every else
has a related if)
• A sequence of statements enclosed between
braces, { and }, is called a compound statement or
block of statements
• switch structure handles multiway selection
• break statement ends switch statement
22

Dr. Ala Alaker 11

You might also like