0% found this document useful (0 votes)
11 views19 pages

Session2 Decision Structure

Uploaded by

hachikomax27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views19 pages

Session2 Decision Structure

Uploaded by

hachikomax27
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 19

DECISION C++

STRUCTURES
if condition and switch case statement

Presentators:
JOHN MICHAEL LEGASPI
CONDITIONAL STATEMENTS

• A conditional statement lets us choose which statement will be executed next

• Therefore they are sometimes called selection statements

• Conditional statements give us the power to make basic decisions

• The C++ conditional statements are the:

• if statement

• if-else statement

• switch statement
FLOW OF CONTROL

• Unless specified otherwise, the order of statement execution through a function is


linear: one statement after another in sequence
• Some programming statements allow us to:
• decide whether or not to execute a particular statement
• execute a statement over and over, repetitively
• These decisions are based on Boolean expressions (or conditions) that evaluate to
true or false
• The order of statement execution is called the flow of control
THE IF STATEMENT

• The if statement has the following syntax:

The condition must be a


Boolean expression. It must
if is a C++ evaluate to either true or false.
reserved word

if ( condition )
statement;

If the condition is true, the statement is executed.


If it is false, the statement is skipped.
LOGIC OF AN IF STATEMENT

condition
evaluated

true
false
statement
THE IFSTATEMENT

• An example of an if statement:
if (FirstNo > SecondNo)
result = FirstNo - SecondNo;
cout<<"The resultant is"<< result;

• First the condition is evaluated -- the value of FirstNo is either greater than the
value of SecondNo, or it is not

• If the condition is true, the assignment statement is executed -- if it isn’t, it is


skipped.
• Either way, the call to cout is executed next
Age < 18 1-17
A
• A condition often uses one of C++'s equality operators or relational operators
== equal to

!= not equal to

< less than

> greater than

<= less than or equal to

>= greater than or equal to

• Note the difference between the equality operator (==) and the assignment
operator (=)
LOGICAL OPERATORS

• C++ provides logical operators.


• The binary logical operators combine two Boolean expressions into one.
• The unary logical operator switches the value of a Boolean expression.
Operator Meaning Kind
&& AND Binary
|| OR Binary
! NOT Unary

• Binary logical operators have lower precedence than relational operators


• NOT has the same precedence as negation.
LOGICAL NOT

• The logical NOT operation is also called logical negation or logical complement
• If some condition a is true, then !a is false; if a is false, then !a is true
• Logical expressions can be shown using a truth table

a !a
True False
False True
LOGICAL AND & LOGICAL OR

• The logical AND expression


a && b
is true if both a and b are true, and false otherwise

• The logical OR expression


a || b
is true if a or b or both are true, and false otherwise
IF-ELSE STATEMENT:

General form of an if-else statement:


if(BooleanExpression)
statement or block 1
else
statement or block 2
LOGIC OF AN IF-ELSE STATEMENT

condition
evaluated

true false

statement1 statement2
THE CONDITIONAL OPERATOR

• C++ provides and operator to create short expressions that work like if-else
statements.
BooleanExpression ? Value1 : Value2;
• If BooleanExpression is true, Value1 is returned
• If BooleanExpression is false, Value2 is returned

• Example:
if (score < 50)
cout<<“Sorry! You Have Failed…";
else
cout<<"You Have Successfully Passed! ";
THE IF-ELSE IF STATEMENT

• Sometimes you need to be able to test a series of conditions


• You can do this with the if-else if statement

• General form:
if (BooleanExpression1)
statement or block 1
else if (BooleanExpression2)
statement or block 2
else
statement or block 3
• If BooleanExpression1 is true, then statement or block 1 is executed.
• If BooleanExpression1 is false, then BooleanExpression2 is tested.
• If BooleanExpression2 is true, then statement or block 2 ais executed.
• If BooleanExpression2 is false, then statement or block 3 is executed.

Note: You can have as many if else clauses as is


needed.
NESTED IFSTATEMENTS

• Nesting is enclosing one structure inside of another.


• A block in C++ can contain any valid C++ code, this includes other if statements:
if(BooleanExpression1) {
if(BooleanExpression2) {
statement1;
statement2;
}
statement3;
statement4;
}
• If BooleanExpression1 is true and BooleanExpression2 is true , what is executed?
• statement1 , statement2 , statement3 , statement4
• If BooleanExpression1 is true and BooleanExpression2 is false , what is executed?
• statement3 , statement4
SWITCH CASE STATEMENT

• Switch case statement is used when we have multiple conditions and we need to perform
different action based on the condition. When we have multiple conditions and we need to
execute a block of statements when a particular condition is satisfied.

The syntax of Switch case statement:


switch (variable or an integer expression) {
case constant:
//C++ code ;
break;
case constant:
//C++ code ;
break;
default: //C++ code ;
}
Example:
#include <iostream>
using namespace std;

int main(){
int num=5;
switch(num+2) {
case 1:
cout<<"Case1: Value is: "<<num<<endl;
case 2:
cout<<"Case2: Value is: "<<num<<endl;
case 3:
cout<<"Case3: Value is: "<<num<<endl;
default:
cout<<"Default: Value is: "<<num<<endl;
}
return 0;
}
THE END
ANY QUESTION

You might also like