Professional Documents
Culture Documents
Operator Description
== Equal to
!= Not equal to
< Less than
<= Less than or equal to
> Greater than
>= Greater than or equal to
Relational Operators and Simple Data Types
if (expression)
Statement
One-Way Selection
Example: -
if (expression)
statement1
else
statement2
bool Data Type and Logical (Boolean) Expressions
bool legalAge;
int age;
legalAge = true;
Operator Description
! not
&& and
|| or
Logical (Boolean) Operators and Logical Expressions
{
statement_1
statement_2
.
.
.
statement_n
}
Multiple Selections: Nested if
Multiple Selections: Nested if
(x > y) || (x == 5) //Line 1
(a == b) && (x >= 7) //Line 2
Comparing Floating-Point Numbers for
Equality: A Precaution
#include <iostream>
#include <iomanip>
#include <cmath>
using namespace std;
int main()
{
double x = 1.0;
double y = 3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0;
cout << flixed << showpoint << setprecision(17);
cout << "3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0 = "
<< 3.0 / 7.0 + 2.0 / 7.0 + 2.0 / 7.0 << endl;
Comparing Floating-Point Numbers for Equality:
A Precaution
#include <iostream>
using namespace std;
int main()
{
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;
}
Sample Runs: In these 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.
Associativity of relational operators: A precaution
= 1 (true)
Associativity of relational operators: A precaution
if (drivingCode = 1)
cout << "The discount on the policy is 25%." << endl;
Using pseudocode to develop, test, and debug a
program
a) if (x > y) then
x is larger
b) if (y > x) then
y is larger
if (x > y) then
x is larger
else
y is larger
switch Structures
switch (expression)
{
case value1:
statements1
break;
case value2:
statements2
break;
...
case valuen:
statementsn
break;
default:
statements
Switch structures
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.";
}
Control Structures II (Repetition)
#include <iostream>
using namespace std;
int main()
{
int calBurnedDay1, calBurnedDay2, calBurnedDay3,
calBurnedDay4, calBurnedDay5, calBurnedDay6,
calBurnedDay7;
int calBurnedInAWeek;
cout << "Enter calories burned day 1: ";
cin >> calBurnedDay1;
cout << endl;
cout << "Enter calories burned day 2: ";
cin >> calBurnedDay2;
cout << endl;
cout << "Enter calories burned day 3: ";
cin >> calBurnedDay3;
cout << endl;
cout << "Enter calories burned day 4: ";
cin >> calBurnedDay4;
cout << endl;
cout << "Enter calories burned day 5: ";
cin >> calBurnedDay5;
cout << endl;
cout << "Enter calories burned day 6: ";
cin >> calBurnedDay6;
cout << endl;
cout << "Enter calories burned day 7: ";
cin >> calBurnedDay7;
cout << endl;
calBurnedInAWeek = calBurnedDay1 + calBurnedDay2 + calBurnedDay3
+ calBurnedDay4 + calBurnedDay5 + calBurnedDay6
+ calBurnedDay7;
cout << "Average number of calories burned each day: "
<< calBurnedInAWeek / 7 << endl;
return 0;
}
Sample Run: In this sample run.
Enter calories burned day 1: 375
Enter calories burned day 2: 425
Enter calories burned day 3: 270
Enter calories burned day 4: 190
Enter calories burned day 5: 350
Enter calories burned day 6: 200
Enter calories burned day 7: 365
Average number of calories burned each day: 310
Control Structures II (Repetition)
while Looping (Repetition) Structure
while (expression)
statement
while Looping (Repetition) Structure
#include <iostream >
using namespace std;
int main()
{
int calBurnedInADay;
int calBurnedInAWeek;
int day;
day = 1;
calBurnedInAWeek = 0;
while (day <= 7)
{
cout << "Enter calories burned each day " << day << ": ";
cin >> calBurnedInADay;
cout << endl;
calBurnedInAWeek = calBurnedInAWeek + calBurnedInADay;
day = day + 1;
}
cout << "Average number of calories burned each day: "
<< calBurnedInAWeek / 7 << endl;
return 0;
}
Sample Run: In this sample run.
Enter calories burned day 1: 375
Enter calories burned day 2: 425
Enter calories burned day 3: 270
Enter calories burned day 4: 190
Enter calories burned day 5: 350
Enter calories burned day 6: 200
Enter calories burned day 7: 365
Average number of calories burned each day: 310
Example
int i = 0;
while (i <= 20)
{
cout << i << " ";
i = i + 5;
}
cout << endl;
i = 0;
while (i <= 20);
{
i = i + 5;
cout << i << " ";
}
cout << endl;
Designing while Loops
10 9 8 7 6 5 4 3 2 1
Example
do
statement
while (expression);
a. i = 11;
while (i <= 10)
{
cout << i << " ";
i = i + 5;
}
cout << endl;
b. i = 11;
do
{
cout << i << " ";
i = i + 5;
}
while (i <= 10);
Nested Control Structures