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:
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
#include <iostream>
using namespace std;
int main()
{
int i = 1;
if (i < 0) {
cout << "Negative Number";
}
cout << "positive Number";
}
positive Number
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
}
#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;
}
Person is an adult for voting.
The nested if-else means an if-else statement within an if or else block. Let’s take an example for better understanding:
#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;
}
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;
#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";
}
i is not present
Note: also read about the C++ sizeof() and typedef Operator
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.
Problem Statement: Given n pairs of parentheses, write a function to generate all combinations of well-formed parentheses. Example…
Given an integer A. Compute and return the square root of A. If A is…
Given a zero-based permutation nums (0-indexed), build an array ans of the same length where…
A heap is a specialized tree-based data structure that satisfies the heap property. It is…
What is the Lowest Common Ancestor? In a tree, the lowest common ancestor (LCA) of…