0% found this document useful (0 votes)
21 views

Conditional Execution

The document discusses conditional execution in C++ using if, else if, and switch statements. It provides examples of calculating income tax based on different income levels using if/else if statements. It also covers logical operators, data types, and flowcharts for conditional execution.

Uploaded by

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

Conditional Execution

The document discusses conditional execution in C++ using if, else if, and switch statements. It provides examples of calculating income tax based on different income levels using if/else if statements. It also covers logical operators, data types, and flowcharts for conditional execution.

Uploaded by

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

Conditional Execution

Let us calculate income tax


Write a program to read the income and print
income tax, using following rules.
If income is ≤180,000, then tax=0
If 180,000< income≤ 500,000, then tax is 10% of
(income -180,000)
If 500,000 < income≤800,000, then tax is
32,000+20% of (income-500,000)
If income>800,000, then tax=92,000 +30% of
(income-800,000 )
Basic if statement
• Form
If (condition) consequent
• Condition: boolean expression
• Boolean: should evaluate to “true” or “false”
• Consequent : C++ statement, eg : assignment
• Consequent could also be a block {...}
o If condition evaluates to true, then the consequent is
executed.
o If condition evaluates to false, then the consequent is
ignored
Conditions
• Simple condition: exp1 relop exp2
Relop : relational operator
• <: less than. <= less than equal to. == equal to
• >: greater than. >=greater than equal to. !=not
equal to
• Condition is considered true if exp 1 relates to exp 2
as per the specified relational operator
• Eg:x=5,y=10,z=100
• x == y , false
Flowchart
• Pictorial representation of a program.
• Statement put inside boxes.
• If box C will possibly executed after box B,
then put an arrow from B to C.
• Convenient for showing conditional execution
because there can be more than one “next”
statement
If statement - Flowchart
Example
{
Float income;
Cin >> income;
If (income<=180000)
Cout<<“No tax owed.”<<endl;
If (income>180000)
Cout<<“You owe tax”<<endl;
}
//Always checks both conditions
//If first condition is true we know second is false and vice versa.
But it checks both the statement under all conditions
Another form of ‘if’
• If (condition) consequent
else alternate
• The condition is first evaluated
• If it is true, then consequent is executed.
• If condition is false, then alternate is executed.
If else flowchart
Example
{
Float income;
If(income<=180000)
Cout<<“no tax owed”<<endl;
Else
Cout<<“you owe a tax”<<endl;
}
//Only one condition check. Thus, more efficient than
previous
Most general form of ‘if’
If (condition1) consequent 1
Else if(condition 2) consequent 2
......
Else if (condition n) consequent n
Else alternate //optional
• Evaluates conditions in order
• Some condition true: execute corresponding consequent.
• All condition false: execute alternate if specified
Flowchart
Example: Tax calculation
Float tax, income;
Cin>>income;
If (income<=180000)
tax=0;
else if (income<=500000)
Tax=(income-180000)*0.1;
else if (income<=800000)
Tax=(income-500000)*0.2+32000;
Else
Tax=(income-800000)*0.3+92000;
Cout<<tax<<endl;
}
Error
Float tax,income;
Cin>>income;
If (income<=180000)
tax=0;
if (income<=500000)
Tax=(income-180000)*0.1;
if (income<=800000)
Tax=(income-500000)*0.2+32000;
Else
Tax=(income-800000)*0.3+92000;
Cout<<tax<<endl;
}
More general form of condition
Sometimes we might want to do something if two
conditions are true or one of the two conditions are true.
Compound conditions:
• AND: condition1 && condition2 : true only if both true
• OR : condition 1||condition2 : true only if at least one is
true
• NOT: !condition: true only if only condition is false
• Components of compound conditions may themselves be
compound conditions
Eg : Condition1 && (condition2||condition3)
Example
• If ((income>180000) &&(income<=500000))
Tax=(income-180000)*0.1;
Same condition may be expressed in many ways
• (income>180000) is same as !
(income<180000)
Precedence
• ! Has higher precedence than && which has
higher precedence than ||
Example:
!P && Q || R
((!P) && Q)||R
Switch statement
General form:
Switch(expression)
{
Case constant1:
Group(1) statement usually ending with break;
Case constant2:
Group(2) statement usually ending with break;
Default:
Default group of statements;
}
• Expressions and constants must be integer
Execution
• The expression is evaluated
• The resulting value is compared with constant1, constant 2
• If some constant 1 is found equal then
• All the statements starting with group(1) are executed till
the end of switch statement. If a break statement is found,
then execution stops.
• If any group of statement does not contain a break
statement then next group is executed.
• If no constant is found equal to the expression then the
default group of statement is executed
Logical data
• C++ has a data type bool into which values of
conditions can be stored.
• Bool is named after George bool, who
formalized the manipulation of
conditions/logical data.
Data type-bool
• Bool highincome, lowincome;
• Defines highincome and lowincome of type
bool
o Highincome=(income>800000);
o Bool fun=true;
• Will set highincome to true if the variable
income contains value larger than 800000
• True and false: boolean constants
Program to check if a given number ‘n’ is a
prime
{
Int n, divisor=2; cin>>n;
Bool divisorfound=false; //no divisor for n found so for.
Repeat(n-2)
{
If(n%divisor==0)
Divisorfound=true;
Cout<<divisorfound;
Divisor=divisor+1;

}
If(!divisorfound)
Cout<<“Prime.\n”;
Else cout<<“Composite.\n”;
}

You might also like