In real life, we face situations where we must make decisions and decide what to do next. Similarly, the programmer must specify one or more conditions to be evaluated or tested by the program, as well as a statement or statements to be executed if the condition is determined to be true, and optionally, additional statements to be executed if the condition is determined to be false.
Decision-making statements available in C++ are:
- if statement
- if-else statements
- nested if statements
- if-else-if ladder
- switch statements
- Jump Statements:
- break
- continue
- goto
- return
if statement:
The if statement is the most straightforward decision-making statement. It is used to determine whether or not a specific statement or block of statements will be executed, i.e. if a certain condition is true, then a block of statements is executed, otherwise not.
Syntax:
if(condition)
{ // condition is true
// execute Statements
}
// condition is false
// execute Statements
Flowchart:
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 1;
if (i < 0) {
cout << "Negative Number";
}
cout << "positive Number";
}
Output:
positive Number
if-else statements:
When the condition is false, we can use the else statement in conjunction with the if statement to execute a block of code.
Syntax:
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Flowchart:
Example:
#include <iostream>
using namespace std;
int main()
{
int age=21;
cout<<"Enter Age: ";
if(age>18)
cout<<"Person is an adult for voting.";
else
cout<<"Person is underage for voting.";
return 0;
}
Output:
Person is an adult for voting.
Nested if-else statements:
The nested if-else means an if-else statement within an if or else block. Let’s take an example for better understanding:
Example:
#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;
}
else-if Ladder:
A user can select from several options here. When one of the if conditions are met, the statement associated with that if is executed, and the rest of the else-if ladder is skipped. If none of the conditions are met, the last else statement is executed.
Syntax:
if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Example:
#include <iostream>
using namespace std;
int main()
{
int i = 90;
if (i == 10)
cout << "i is 10";
else if (i == 15)
cout << "i is 15";
else if (i == 20)
cout << "i is 20";
else
cout << "i is not present";
}
Output:
i is not present
Key points:
- The group of statements within the if curly braces (excluding the else) forms an if block.
- The group of statements within the else curly braces creates an else block.
- The else is written just below the if. The statements in the if block and those in the else block are indented to the right.
- If there is only one statement in the else block to be executed, we could drop the pair of curly braces.
Note: also read about the C++ sizeof() and typedef Operator
Follow Me
If you like my post, please follow me to read my latest post on programming and technology.
https://www.instagram.com/coderz.py/
https://www.facebook.com/coderz.py
Staying up to the mark is what defines me. Hi all! I’m Rabecca Fatima a keen learner, great enthusiast, ready to take new challenges as stepping stones towards flying colors.
Leave a Comment