Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 45

D E C I S I O N S / C O N T R O L S TAT E M E N T S

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

Some statements are executed only if certain conditions are met


A condition is met if it evaluates to true
CONTROL STRUCTURES
(CONTINUED)
PROGRAMMING &
DECISIONS
Practically all computer programs, when modeled with a flowchart,
demonstrate that branching occurs within their algorithms.
DECISION MAKING IN C+
+
1. if statement
2. switch statement
3. ? conditional operator statement
4. goto statement
USING RELATIONAL
OPERATORS
Relational operators provide the tools with which programs make
decisions with true and false evaluations

== 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.)

&& is the logical AND operator


|| is the logical OR operator
! is the logical NOT 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 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:

IF-THEN :One-Way Selection


IF-THEN-ELSE : Two-Way Selection
LEVELS OF COMPLEXITY
FOR IF
Simple if statement: one way selection
if … else statement: two way selection
Nested if … else statement : multiple
selections
ONE-WAY SELECTION
The syntax of one-way selection is:

The statement is executed if the value of the expression is


true
The statement is bypassed if the value is false;
program goes to the next statement
if is a reserved word
ONE-WAY SELECTION
(CONTINUED)
IF-THEN
Entry

Test false true


condition p

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";
}

IF structures use a control expression to


determine if the code in the braces is to be
executed or not
COMPOUND
CONDITIONALS
You must use the AND operator (&&) to form a compound relational
expression:

if (0 < number && number < 10)


{
cout << number << " is greater than 0
but less than 10." << endl;
}
CODING IF STRUCTURES

Place a semicolon at the end of each


statement within the braces, which can
contain many executable statements
Use curly braces around the body of
an IF structure even if there is only
one statement
#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter an integer: ";


cin >> number;

// checks if the number is positive


if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
cout << "This statement is always executed.";
return 0;
}
#include< iostream>
using namespace std;
int main( )
{
int x,y;
x=15;
y=13;
if (x > y )
{
cout << "x is greater than y";
}
return 0;
}
TWO-WAY SELECTION
Two-way selection takes the form:

If expression is true, statement1 is executed; otherwise,


statement2 is executed
 statement1 and statement2 are any C++ statements

else is a reserved word


TWO-WAY SELECTION
(CONTINUED)
Entry
GENERAL FORM
if (test expression)
{
True-block statements;
true
} false
Test condition p
else
{
False-block statements;
“false” “true”
} statement a statement a
Exit
next statement;
THE IF … ELSE
STATEMENT
Two-way selection structure since either the
block of code after the "if" part will be executed
or the block of code after the "else" part will be
executed

The “If" part is executed if the control expression


is TRUE while the "else" part is executed if the
"if" part is FALSE, guaranteeing one part of the
expression to be executed or the other
// Program to check whether an integer is positive or negative
// This program considers 0 as a positive number

#include <iostream>
using namespace std;

int main() {
int number;

cout << "Enter an integer: ";


cin >> number;
if (number >= 0) {
cout << "You entered a positive integer: " << number << endl;
}
else {
cout << "You entered a negative integer: " << number << endl;
}
cout << "This line is always printed.";
return 0;
}
#include<iostream>
using namespace std;

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;

cout << "Enter an integer: ";


cin >> number;
if (number > 0) {
cout << "You entered a positive integer: " << number << endl;
}
else if (number < 0) {
cout << "You entered a negative integer: " << number << endl;
}
else {
cout << "You entered 0." << endl;
}
cout << "This line is always printed.";
return 0;
}
#include<iostream>
using namespace std;
int main( )
{
int a,b,c;
cout << "enter 3 number";
cin >> a >> b >> c;
if(a > b)
{
if( a > c)
{
cout << "a is greatest";
}
else
{
cout << "c is greatest";
}
}
else
{
if( b> c)
{
cout << "b is greatest";
}
else
{
cout << "c is greatest";
}
}
return 0;
}
NESTED IF STATEMENTS
If structures and if/else statements can be nested within one another
in order to model complex decision structures.
 Use the braces and semicolons properly when coding such structures.
GENERAL FORM
if (test condition 1)
{ // true-block1 statements
if (test condition 2)
{
true-block2 statements;
}
else
{
false-block2 statements;
}
}
else
{
false-block1 statements;
}
// NESTED IF EXAMPLES

// C++ program to find if an integer is even or odd or neither (0)


// using nested if statements

#include <iostream>
using namespace std;

int main() {
int num;

cout << "Enter an integer: ";


cin >> 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.

2 if...else statementAn ‘if’ statement can be followed by an


optional ‘else’ statement, which executes when the boolean
expression is false.

3 switch statementA ‘switch’ statement allows a variable to be


tested for equality against a list of values.

4 nested if statementsYou can use one ‘if’ or ‘else if’ statement


inside another ‘if’ or ‘else if’ statement(s).

5 nested switch statementsYou can use one ‘switch’ statement


inside another ‘switch’ statement(s).
SHORT HAND TERNARY
OPERATOR
There is also a short-hand if else, which is known as the ternary operator because
it consists of three operands. It can be used to replace multiple lines of code with
a single line. It is often used to replace simple if else 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

You might also like