Documente Academic
Documente Profesional
Documente Cultură
Decision Making In
Computers
A circuit quite simply allows one out of two
choices to be made depending on its inputs
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
The value zero (0) is considered to be FALSE by
C++. Any positive or negative value is considered
to be TRUE
==
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
&&
is the logical AND
operator
||
is the logical OR
operator
Truth Tables
Use this truth table to determine the results of the
logical operators. In this table, 1 represents TRUE
and 0 represents FALSE.
Note that the ! symbol (the logical NOT operator)
changes a TRUE to a FALSE.
AND
A
0
0
1
1
B
0
1
0
1
A && B
0
0
0
1
OR
A
0
0
1
1
B
0
1
0
1
A || B
0
1
1
1
NOT
A
0
1
!A
1
0
if statement
if else statement
Nested if else statement
else..if ladder
IF-THEN
Entry
Test
condition p
Exit
false
true
True
statement a
Compound Conditionals
You
Coding IF Structures
Place
IFELSE
Entry
Test
condition p
false
false
statement a
true
Exit
true
statement a
General Form
if (test expression)
{
True-block statements;
}
else
{
False-block statements;
}
next statement;
if/else if statement
Nested If Statements
If
General Form
if (test condition 1)
{ // true-block1 statements
if (test condition 2)
{
true-block2 statements;
}
else
{
false-block2 statements;
}
}
else
{
false-block1 statements;
}
Switch Structure
Switch Structure
The
Grouping Cases
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; }
Conditional Operator
General
form:
conditional expression ? exp-1:exp-2 ;
f=(c = = 'y') ? 1: 0;
GOTO Statement
Unconditional
branching
GOTO label;
label: statement;
Not recommended but may be used
occasionally