Module 4 Lecture
Module 4 Lecture
In a sequential program
The computer starts at the beginning and follows the
statements in order.
No choices are made; there is no repetition.
Control structures provide alternatives to sequential program
execution and are used to alter the sequential flow of execution.
The two most common control structures are:
1) Selection
2) Repetition
Relational Operators
In a sequential program
The computer starts at the beginning and follows the statements in
order.
No choices are made; there is no repetition.
Control structures provide alternatives to sequential program
execution and are used to alter the sequential flow of execution.
The two most common control structures are:
1) Selection
2) Repetition
In selection, the program executes particular statements
depending on some condition(s).
In repetition, the program repeats particular statements a certain
number of times based on some condition(s)
Flow of Execution
Control structures
Relational operators:
Allow comparisons
Require two operands (binary)
Expressions such as 4 < 6 and 'R' > 'T' are examples of logical
(Boolean) expressions.
Return 1 if expression is true, 0 otherwise
Comparing values of different data types may produce
unpredictable results
For example, 8 < '5' should not be done
Any nonzero value is treated as true
Relational Operators in C++
Relational Operators and Simple Data Types
!('A' > 'B') true Because 'A' > 'B' is false, !('A' > 'B')is true.
!(6 <= 7) false Because 6 <= 7 is true, !(6 <= 7) is false.
The && (And) Operator
The || (Or) Operator
Order of Precedence
Expression Value
!found false
x > 4.0 true
!num false
!found && (x >= 0) false
!(found && (x >= 0)) false
x + y <= 20.5 true
(n >= 0) && (n <= 100) true
('A' <= ch && ch <= 'Z') true
(a + 2 <= b) && !flag true
Example 2
//Chapter 4: Logical operators
#include <iostream>
using namespace std;
int main()
{
bool found = true;
bool flag = false;
int num = 1;
double x = 5.2;
double y = 3.4;
int a = 5, b = 8;
int n = 20;
char ch = 'B';
cout<<"Line 1: !found evaluates to "
<<!found<<endl; //Line 1
cout<<"Line 2: x > 4.0 evaluates to "
<<(x > 4.0)<<endl; //Line 2
cout<<"Line 3: !num evaluates to "
<<!num<<endl; //Line 3
cout<<"Line 4: !found && (x >= 0) evaluates to "
<<(!found && (x >= 0))<<endl; //Line 4
cout<<"Line 5: !(found && (x >= 0)) evaluates to "
<<(!(found && (x >= 0)))<<endl; //Line 5
cout<<"Line 6: x + y <= 20.5 evaluates to "
<<(x + y <= 20.5)<<endl; //Line 6
cout<<"Line 7: (n >= 0) && (n <= 100) evaluates to "
<<((n >= 0) && (n <= 100))<<endl; //Line 7
cout<<"Line 8: ('A' <= ch && ch <= 'Z') evaluates to "
<<('A' <= ch && ch <= 'Z')<<endl; //Line 8
cout<<"Line 9: (a + 2 <= b) && !flag evaluates to "
<<((a + 2 <= b) && !flag)<<endl; //Line 9
return 0;
}
Output:
Line 1: !found evaluates to 0
Line 2: x > 4.0 evaluates to 1
Line 3: !num evaluates to 0
Line 4: !found && (x >= 0) evaluates to 0
Line 5: !(found && (x >= 0)) evaluates to 0
Line 6: x + y <= 20.5 evaluates to 1
Line 7: (n >= 0) && (n <= 100) evaluates to 1
Line 8: ('A' <= ch && ch <= 'Z') evaluates to 1
Line 9: (a + 2 <= b) && !flag evaluates to 1
Logical Expressions
is equivalent to
Earlier versions of C++ did not provide built-in data types that had
logical (or Boolean) values true and false.
Because logical expressions evaluate to either 1 or 0
the value of a logical expression was stored in a variable of the
data type int.
Therefore, you can use the int data type to manipulate logical
(Boolean) expressions
int Data Type and Logical (Boolean) Expressions
int main ()
{
double wages, rate, hours;
cout<<fixed<<setprecision(2); //Line 1
cout<<"Line 2: Enter working hours and rate: ";
//Line 2
cin>>hours>>rate; //Line 3
return 0;
}
if (testCondition1)
{
// statements to be executed if testCondition11 is true
}
else if(testCondition2)
{
// statements to be executed if testCondition1 is false and testCondition2 is true
}
else if (testCondition3)
{
// statements to be executed if testCondition1 and testCondition2 is false and
testCondition3 is true
}
.
.
else
{
// statements to be executed if all test conditions are false
}
Example
Assume that all variables are properly declared, and consider the
following statements:
if(score >= 90)
cout<<"The grade is A"<<endl;
else if(score >= 80)
cout<<"The grade is B"<<endl;
else if(score >= 70)
cout<<"The grade is C"<<endl;
else if(score >= 60)
cout<<"The grade is D"<<endl;
else
cout<<"The grade is F"<<endl;
More example
if(month == 1) //Line 1
cout<<"January"<<endl; //Line 2
else if(month == 2) //Line 3
cout<<"February"<<endl; //Line 4
else if(month == 3) //Line 5
cout<<"March"<<endl; //Line 6
else if(month == 4) //Line 7
cout<<"April"<<endl; //Line 8
else if(month == 5) //Line 9
cout<<"May"<<endl; //Line 10
else if(month == 6) //Line 11
cout<<"June"<<endl; //Line 12
switch Structures
The general form (syntax) of a switch statement is:
switch(expression)
{
case value1: statements1;
break;
case value2: statements2;
break;
.
.
.
case valuen: statementsn;
break;
default: statements;
}
switch Structures
The break statement has a special meaning and may or may not
appear after each statement.
In C++, switch, case, break, and default are reserved words.
In a switch structure, first the expression is evaluated.
The value of the expression is then used to perform the
corresponding action.
Although it need not be, the expression is usually an identifier.
The value of the expression can be only integral.
The expression is sometimes called the selector.
Its value determines which case is selected for execution.
switch Structures
switch(grade)
{
case 'A': cout<<"The grade is A.";
break;
case 'B': cout<<"The grade is B.";
break;
case 'C': cout<<"The grade is C.";
break;
case 'D': cout<<"The grade is D.";
break;
case 'F': cout<<"The grade is F.";
break;
default: cout<<"The grade is invalid.";
}
Where, grade is a variable of the type char. If the value of grade is,
say 'A', the output is The grade is A.
Example 2
Sample Run 2:
Enter an integer between 0 and 10: 1
Sample Run 4:
Enter an integer between 0 and 10: 4
The number you entered is 4
Mickey.
Out of switch structure.
Sample Run 5:
Enter an integer between 0 and 10: 5
Sample Run 6:
Enter an integer between 0 and 10: 7
Sample Run 9:
Enter an integer between 0 and 10: 11
Sample Run 6:
Enter an integer between 0 and 10: 7
Assume that score is an int variable with values between 0 and 100
switch(score / 10)
{
case 0: case 1: case 2:
case 3: case 4: case 5: grade = 'F';
break;
case 6: grade = 'D';
break;
case 7: grade = 'C';
break;
case 8: grade = 'B';
break;
case 9: case 10: grade = 'A';
break;
default: cout<<" Invalid test score."<<endl;
}
Example 4: