Professional Documents
Culture Documents
3 Decisions 1
3 Decisions 1
Sonam Yar
Lecturer Computer Science
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
DECISION/CONTROL
STRUCTURES
A computer can proceed:
In sequence
Selectively (branch) - making a choice
Repetitively (iteratively) - looping
== 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
USING LOGICAL
OPERATORS
When complex decisions must be coded into an
algorithm, it may be necessary to "chain together" a
few relational expressions (that use relational
operators)
This is done with logical operators (also called
Boolean operators.)
AND OR NOT
A B A && B A B A || B A !A
0 0 0 0 0 0 0 1
0 1 0 0 1 1 1 0
1 0 0 1 0 1
1 1 1 1 1 1
ORDER OF LOGICAL
OPERATIONS
Logical operators may be mixed within
evaluation statements but the following order of
preference must be respected:
1. NOT operator (!)
2. AND operator (&&)
3. OR operator (||)
Short-circuit evaluation in C++ allows the
compiler to quickly evaluate a logical expression
in some cases without having to completely
evaluate each part of the expression
COMPLETE ORDER OF
OPERATIONS
The complete order of operations including all of the
arithmetic, relational, and logical operators including
all of the basic arithmetic, relational, & logical
operators is:
*, /, %
+, -
<, >, <=, >=, ==, !=
!
&&
||
COMPARE AND BRANCH
A program can instruct a computer to compare two items
and do something based on a match or mismatch which,
in turn, redirect the sequence of programming
instructions.
There are two forms:
Exit
True
statement a
USE THE “IF” STRUCTURE
Practically all computer languages have some
sort of if structure. In C++, the if structure is a
one-way selection structure:
if (number == 3)
{
cout << "The value of number is 3";
}
int main() {
int number;
#include <iostream>
using namespace std;
int main() {
int number;
void main( )
{
int x,y;
x=15;
y=18;
if (x > y )
{
cout << "x is greater than y";
}
else
{
cout << "y is greater than x";
}
return 0;
}
IF… ELSE IF LADDER: GENERAL
FORM
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else if (condition n)
statement n;
else
default statement;
statement x;
IF/ELSE IF STATEMENT
As soon as a true condition is found, the statement associated with it is
executed and control is transferred to the statement after the ladder
The else clause is optional just as it is with an if statement.
if (number > 10)
{
cout << number << " is greater than 10." << endl;
}
else if (number <= 9 && number >= 6)
{
cout << number << " is greater than 5
and less than 10." << endl;
}
else
{
cout << number << "must be less than 6." << endl;
}
number = number + 1;
// Program to check whether an integer is positive, negative or zero
#include <iostream>
using namespace std;
int main() {
int number;
#include <iostream>
using namespace std;
int main() {
int num;
// outer if condition
if (num != 0) {
// inner if condition
if ((num % 2) == 0) {
cout << "The number is even." << endl;
}
// inner else condition
else {
cout << "The number is odd." << endl;
}
}
// outer else condition
else {
cout << "The number is 0 and it is neither even nor odd." << endl;
}
cout << "This line is always printed." << endl;
}
SWITCH STRUCTURE
The switch structure is a multiple-selection
structure that allows even more complicated
decision statements than a two-way if/else structure
allows.
It chooses one of the "cases" depending on the result
of the control expression.
Only variables with the INT or CHAR data types
may be used in the control expressions (i.e.
parentheses) of switch statements.
Single quotes must be used around CHAR variables
Single quotes are NOT used around the integer values
SAMPLE SWITCH
STRUCTURE
switch (character_entered)
{
case ‘A’ :
cout << “The character entered was A.\n”;
break;
case ‘B’:
cout << “The character entered was B.\n”;
default:
cout << “Illegal entry\n”;
}
SWITCH STRUCTURE
•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.
•When break statement is encountered inside a loop, the loop is
immediately exited and the program continues with the statement
immediately following the loop.
•The break statement can also be used to jump out of a loop.
•The default case, if present, will result if none of the prior cases apply
#include<iostream>
using namespace std;
int main()
{
int day = 4;
switch (day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
case 4:
cout << "Thursday";
break;
case 5:
cout << "Friday";
break;
case 6:
cout << "Saturday";
break;
case 7:
cout << "Sunday";
break;
}
return 0;
}
// Output "Thursday" (day 4)
switch (number)
{
case 1:
case 3:
GROUPING CASES
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; }
SWITCH STRUCTURES
(CONTINUED)
One or more statements may follow a case label
Braces are not needed to turn multiple statements into a single
compound statement
The break statement may or may not appear after each statement
switch, case, break, and default are reserved words
S.No Statement & Description
1 if statementAn ‘if’ statement consists of a boolean expression
followed by one or more statements.
Syntax
variable = (condition) ? expressionTrue : expressionFalse;
Instead of writhing
If( )
{ }
Else
{ }
CONDITIONAL OPERATOR
General form:
conditional expression ? exp-1:exp-2 ;
The conditional expression is evaluated first. If the result is
non-zero, exp-1 is evaluated and is returned as the value of
the conditional expression. Otherwise, exp-2 is evaluated
and its value is returned
f=(c = = 'y') ? 1: 0;
GOTO STATEMENT
Unconditional branching
GOTO label;
…
label: statement;
Not recommended but may be used occasionally