0% found this document useful (0 votes)
20 views21 pages

C++ Conditional Statements Guide

Uploaded by

tooba amsood
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)
20 views21 pages

C++ Conditional Statements Guide

Uploaded by

tooba amsood
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/ 21

Conditional Statements in with C++

08/10/2024 1
Check Bytes in Memory – Real Numbers
• get information for following data types:
– Float
– double
– long double

• Use ( cout << sizeof(float); ) operator to get


this information, Example:…

08/10/2024 2
If Statement in C/C++
• Supports the use of a decision structure
• Allows statements to be conditionally executed or skipped
over
• Models the way we mentally evaluate situations
“If it is cold outside,
wear a coat and wear a hat.”

The block inside the braces is called the body of the if statement. If there is only 1 statement in the body, the { }
may be omitted.

08/10/2024 3
If Statement in C++

08/10/2024 4
If Statement (Example)

08/10/2024
If condition (Example using multiple statements )

08/10/2024 6
If Statement (Flag)

• A variable that signals a condition


• Usually implemented as a bool
• Meaning:
– true: the condition exists
– false: the condition does not exist
• The flag value can be both set and tested with
if statements

08/10/2024 7
If Statement (Example using Flag)

08/10/2024 8
Don’t Confuse == With =

• Not to confuse the equality operator (==) with the assignment(=)


• Example: operator (=), as in the following statement:
if (x = 2)
cout << "It is True!";
The statement above does not determine whether x is equal to 2, it assigns x
the value 2.
Furthermore, the cout statement will always be executed because the
expression x = 2 is always true.

08/10/2024 9
Important points about if

• if is a keyword. It must be lowercase


• (condition) must be in ( )
• Do not place ; after (condition)
• Don't forget the { } around a multi-statement body

08/10/2024 10
if/else Statement

08/10/2024 12
The if/else Statement
Allows a choice between statements depending on whether (condition) is true or false

true false
condition

statement set 1 statement

set 2

08/10/2024 13
if/else (Example)
if/else (Example with multiple statements)

08/10/2024 15
The else...if Construction
Chain of if statements that test in order until one is found to be true

“If it is raining, take an umbrella,

OR OR
else, if it is windy, take a hat,

else, if it is sunny, take sunglasses.”

else, you are good to go as is

08/10/2024 16
The else...if Example
if (marks>=80)
{
cout<<“\n You got A grade”;
cout<<“\n You won scholarship too”;
}
else if (marks>=70)
cout<<“\n You got B grade”;
else if (marks>=60)
cout<<“\n You got C grade”;
else if (marks>=50)
cout<<“\n You got D grade”;
else
cout<<“\n You are fail”;
08/10/2024 18
Practice Example
Write a program to calculate tax collection according to the
following formula:
– 5% Tax, if salary is above 50000
– 3% Tax , if salary is between 30000 and 50000
– 2% Tax, is salary is between less than 30000
In the end the program should print the calculated tax.

08/10/2024 20
Practice Tasks
• Write a program in C++ to find the Size of fundamental data types.
• Write a program in C++ to take two numbers as input and swap
them. Print before the swap and after the swap.
• Write an if statement that performs the following logic: if the
variable x is equal to 20, then assign 0 to the variable y.
• Write an if statement that performs the following logic: if the
variable price is greater than 500, then assign 0.2 to the variable
discountRate.
• Write an if statement that multiplies payRate by 1.5 if hours are
greater than 40.
08/10/2024 21

You might also like