CS 107 - 6/24/15
setw (10) - setw() is a function
functions take parameter information
(the item(s) in the parantheses)
Prof. Troy normally uses the printf() from the C stdio.h library
for formatting output.
printf ("%10.3f", x);
The above seems easier to Prof. Troy than
cout << set1(10) << setprecision(3) << x ;
Re-write the NESTED-IF Statement from Monday 6/22 in its
most verbose manner
int average;
char grade;
cin >> average;
if ( average >= 90 )
{
grade = 'A';
}
else
{
if ( average >= 80 )
{
grade = 'B';
}
else
{
if ( average >= 70 )
{
grade = 'C';
}
else
{
if ( average >= 60 )
{
grade = 'D';
}
else
{
grade = 'F';
}
}
}
}
cout << "The final grade is: " << grade << endl;
A very poor indentation pattern
if (
average >=
90
)
{
grade
= 'A' ;
}
The above is harder to understand (IMHO) than:
if ( average >= 90 )
{
grade = 'A';
}
Another poor indentation is:
if ( average >= 90 )
grade = 'A';
else if ( average >= 80 )
grade = 'B';
else if ( average >= 70 )
grade = 'C';
else if ( average >= 60 )
grade = 'D';
else
grade = 'F';
Echo Back - show the user what he/she typed, so they can
verify the input is correct.
int weight;
cout << " Enter the weight amount: " ;
cin >> weight;
// the following line is the "ECHO BACK" line
cout << "The weight amount entered was : " << weight << endl;
Relational operators: == != < <= > >=
compare two value and "return" a boolean value
If we want a complex boolean expression, we may need to use
the Boolean Operators
Take one or two Boolean values and "return" a Boolean
based on the operation being performed.
In C/C++ we have 3 Boolean Operators
- AND &&
- OR ||
- NOT !
For the AND Operation, it will "return" a TRUE value if
both operands are TRUE; otherwise it will "return" a
FALSE value.
Check for a value in the range from 10 to 20 inclusive:
if ( (value >= 10) && (value <= 20) )
For the OR Operation, it will "return" a TRUE value if
at least one operand is TRUE; otherwise if will "return"
a FALSE value.
Check if a value is outside of the range from 10 to 20 inclusive:
if ( ( value < 10 ) || ( value > 20 ) )
TRUTH TABLES:
x y | x && y x || y !x !y
---------|----------------------------------
T T | T T F F
T F | F T F T
F T | F T T F
F F | F F T T
For the NOT Operation, IT IS A UNARY OPERATION (taking
only one operand).
If the operand is TRUE, NOT "returns" a FALSE value.
If the operand is FALSE, NOT "returns" a TRUE value.
if ( ! ( value > 10 ) )
This example of NOT is most often written using a differnt
relational operator
To identify if a student is in the C or D range
int average;
char grade;
cin >> average;
if ( ( average < 80 ) && ( average >= 60 ) )
{
cout << "The student is in the C/D Range" << endl;
}
Looping Statements - Chapter 6 The while statement
Repeat a block of code "while some condition is true"
Syntax:
while ( )
{
;
}
Print the numbers from 1 to 10
int value;
value = 1; // initialization
while ( value <= 10 ) // condition
{
cout << value << endl; // body statement(s)
value = value + 1; // increment statement
}