0% found this document useful (0 votes)
180 views7 pages

Decision Making in C++

The document discusses decision making and conditional statements in C++. It covers the if statement, if-else statement, nested if statements, and the ternary operator. The if statement executes code if a condition is true, while if-else allows executing one code block if true and another if false. Nested if statements place an if statement inside another if. The ternary operator provides a short-hand for simple if-else statements. Examples are provided for each conditional statement.

Uploaded by

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

Decision Making in C++

The document discusses decision making and conditional statements in C++. It covers the if statement, if-else statement, nested if statements, and the ternary operator. The if statement executes code if a condition is true, while if-else allows executing one code block if true and another if false. Nested if statements place an if statement inside another if. The ternary operator provides a short-hand for simple if-else statements. Examples are provided for each conditional statement.

Uploaded by

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

Decision Making in C++

There come situations in real life when we need to make some decisions and based on these decisions,
we decide what should we do next. Similar situations arise in programming also where we need to make
some decisions and based on these decisions we will execute the next block of code.

 if statement in C++

if statement is the most simple decision making statement. It is used to decide whether a certain
statement or block of statements will be executed or not i.e if a certain condition is true then a
block of statement is executed otherwise not.

Syntax:
Flowchart:-

 if-else in C++:-

The if statement alone tells us that if a condition is true it will execute a block of statements and
if the condition is false it won’t. But what if we want to do something else if the condition is
false. Here comes the else statement.

SYNTAX:-
Flowchart:-

 nested-if in C++ :-

A nested if in C is an if statement that is the target of another if statement. Nested if statements


means an if statement inside another if statement. we can place an if statement inside another if
statement.

Syntax:-
Flowchart :-

program to illustrate nested-if statement:-

#include <iostream>
using namespace std;
  
int main() 
{
    int i = 10;
  
    if (i == 10)
    {
        // First if statement
        if (i < 15)
           cout<<"i is smaller than 15\n";
  
        // Nested - if statement
        // Will only be executed if statement above
        // is true
        if (i < 12)
            cout<<"i is smaller than 12 too\n";
        else
            cout<<"i is greater than 15";
    }
  
    return 0;
}
 Short Hand If...Else (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 :-

Example:-

IMPORTANT POINTS ABOUT IF STATEMENT:-

 if statements are executed from the top down.

 As soon as one of the conditions controlling the if is true, the statement associated with
that if is executed, and the rest of the else-if ladder is bypassed
 C++ supports the usual logical conditions from mathematics:

o Less than: a < b


o Less than or equal to: a <= b
o Greater than: a > b
o Greater than or equal to: a >= b
o Equal to a == b
o Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.

Questions on if-else statement:-


 Write a C program to find maximum between two numbers.
 Write a C program to find maximum between three numbers.
 Write a C program to check whether a number is negative, positive or zero.
 Write a C program to check whether a number is divisible by 5 and 11 or not.
 Write a C program to check whether a number is even or odd.
 Write a C program to check whether a year is leap year or not.
 Write a C program to input week number and print week day.
 Write a C program to input month number and print number of days in that month.
 Write a C program to count total number of notes in given amount.
 Write a C program to input angles of a triangle and check whether triangle is valid or
not.
 Write a C program to input all sides of a triangle and check whether triangle is valid or
not.
 Write a C program to check whether the triangle is equilateral, isosceles or scalene
triangle.
 Write a C program to find all roots of a quadratic equation.
 Write a C program to calculate profit or loss.
 Write a C program to input marks of five subjects Physics, Chemistry, Biology,
Mathematics and Computer. Calculate percentage and grade according to following:
Percentage >= 90% : Grade A
Percentage >= 80% : Grade B
Percentage >= 70% : Grade C
Percentage >= 60% : Grade D
Percentage >= 40% : Grade E
Percentage < 40% : Grade F

 Write a C program to input electricity unit charges and calculate total electricity bill according
to the given condition:
For first 50 units Rs. 0.50/unit
For next 100 units Rs. 0.75/unit
For next 100 units Rs. 1.20/unit
For unit above 250 Rs. 1.50/unit
An additional surcharge of 20% is added to the bill
SOLUTIONS:-
https://codeforwin.org/2015/05/if-else-programming-practice.html

You might also like