Wyo C++ - Ch. 7 Notes
Objective #1: Understand how decisions are made in programs.
- Everything that happens inside of a computer is a result of the operation
of its circuits. A circuit quite simply allows one out of two choices
to be made depending on its inputs. Within all non-trivial computer programs
there are many decision that in essence are like the hardware circuits inside
of the computer. Practically all computer programs, when modeled with a flowcharts,
demonstrate that branching occurs within their algorithms. This gives the
user a more interactive experience and it allows the programs to actually
solve problems.
Objective #2: Understand how true and false is represented in C++.
- When decisions are made in a computer program, they are simply the result of a
computation in which the final result is either true or false. Computer programs do not
have intuition or "gut feelings"; they simply make thousands of yes-or-no-like
decisions at blinding speeds. The computer programmer must set up these decisions within
the algorithm of a computer program to achieve the desired results.
Objective #3: Use relational operators.
- Relational operators provide the tools with which programs make decisions
with true and false evaluations.
Relational operators:
== equal to NOTE:
this is two equals symbols next to each other, not to be confused with the
assignment operator, =
> greater than
< less than
>= greater than or equal to
<= less than or equal to
!= not equal to
Objective #4: Use logical operators.
Objective #5: Use the if structure.
Objective #6: Use the if/else structure.
Objective #7: Use nested if structures.
- If structures and if/else structures can be nested within one another
in order to model complex decision structures. Be sure to use the braces and
semicolons properly when coding such structures. Also, be sure to rigorously
check the logic of your algorithm since it is quite easy to overlook possible
errors.
- Example:
The if statement that tests for divisibility by 5 is located inside of the
if statement that tests for divisibility by 3 therefore it is considered to
be a nested if statement.
if (number % 3 == 0)
{
cout
<< number << " is divisible by 3." << endl;
if
(number % 5 == 0)
{
cout << number << " is divisible by 3 and 5."
<< endl;
}
}
Objective #8: Use the switch structure.
switch(characterEntered)
{
case 'A':
case 'B':
cout << "You entered a B";
break;
default:
cout << "Illegal entry";
break;
}
However, in the following example, the would be no output if characterEntered
= 'A':
switch(characterEntered)
{
case 'A':
break;
case 'B':
cout << "You entered a B";
break;
default:
cout << "Illegal entry";
break;
}
- The break statement must be used within each case if you do not want following
cases to evaluate once one case is found. When the break statement is executed
within a switch, C++ will execute the next statement outside of the switch
statement. However, sometimes it is desirable not to use the break statement
in a particular case. Example:
switch (donationLevel)
{
case 1:
cout << "You donated
over $1,000, therefore you will receive a complimentary Wyomissing pocket
watch." << endl;
case 2:
cout << "You donated
over $500, therefore you can have a complimentary Wyomissing 14K gold pen
& pencil set. " << endl;
case 3:
cout
<< "You donated over $250, therefore you will receive a complimentary
Wyomissing necktie." << endl;
case 4:
cout
<< "You donated over $100, therefore you will receive a complimentary
Wyomissing bumper sticker. " << endl;
break;
default:
cout
<< "You are cheap and ungenerous." << endl;
break;
}
- You can save redundancy in your code by placing the cases next to each other
as in the following example.
switch (number)
{
case 1:
case 3:
case 5:
case 7:
case 9:
cout << number << " is an even
number." << endl;
break;
case 2:
case 4:
case 6:
case 8:
cout << number << " is an odd
number. " << endl;
break;
default:
cout << number << " is not
a value between or including 1 and 9." << endl;
break;
}