Conditional statements, also known as selection or Decision making statements, used to make decisions based on the expression. If the expression evaluates to True or 1 the statements following the condition or inside curly braces gets executed. If the expression evaluates to False or 0 another set of statements after the braces gets executed.
if Statement: In if conditional statement the expression in bracket evaluated, if the result of expression is True than the statements inside curly braces get executed or if the result is false than then program control skips the IF block and execute rest of the program.
if (expression)
{
Multiple statements;
}
Example of if statement:
#include <iostream>
using namespace std;
int main()
{
int a, b=20;
cout<<"Enter the value of a:"<<"\n";
cin>>a;
if(a==10){
a = a+b;
}
cout << "Value of a = " <<a<< endl;
return 0;
}
Output:
Enter the value of a:10 Value of a = 30
If-else Statement: if- else statement is almost similar to If statement except when the expression evaluate to false the ELSE block executed, than the rest of program.
The syntax of the if-else statements is :
if (expression) { Statement ; } else { Statement2; }
Example demonstrating use of if-else.
int main()
{
int a, b=20;
cout<<"Enter the value of a:"<<"\n";
cin>>a;
if(a==10){
a = a+b;
}
else
a = 0;
cout << "Value of a = " <<a<< endl;
return 0;
}
Output:
Enter the value of a: 10 Value of a = 30
Nested if-else Statement: When if-else block contain another if-else statement inside it called Nested if-else. And if-else can be nested in three ways.
1. if-else statement nested within the IF block.
if (conditionl) { statementl; if(condition2) statement2; else statement3; } else statement4;
2. if-else statement nested within ELSE block.
if(condition1) statement1; else { statement4; if (condition2) statement2; else statement3; } statement5;
3. if-else statement is nested within both the if and else blocks.
if(conditionl) { statementl; if (condition2) statement2; else statement3; } else { statement4; if (condition3) statement5; else statement6; } statement7;
WAP program to find greatest of three number using Nested if-else statements.
#include <iostream>
using namespace std;
int main()
{
int a,b,c;
cout<<"Enter the values of a,b,c:"<<"\n";
cin>>a>>b>>c;
if (a>b){
if(a>c)
cout<<"a is greatest";
else
cout<<"c is greatest";
}
else{
if(c>b)
cout<<"c greatest";
else
cout<<"b greatest";
}
return 0;
}
Output:
Enter the values of a,b,c: 10 20 5 b greatest
Else – if ladder has if-else as outermost statement and multiple else-if inside. In else-if ladder user can decide among multiple options. The outermost if statement executed first and result is true then all else-if and else are skipped rest program executed, if outermost if statement result to false then all else-if statements tested, any of else-if true that statements executed and rest skipped. If all the conditions are false, then the final else statement will be executed.
The syntax of e1se- if ladder is,
if(condition1) statement1; else-if(condition2) statement2; else-if(condition3) statement3; . . . . else statement;
A program to determine whether a character is in lower-case or upper case using else-if laddr.
#include<iostream>
using namespace std;
int main ()
{
char ch;
cout<<"Enter alphabet:";
cin>>ch;
if( (ch>='A') && (ch<='Z'))
cout<<"upper case letter";
else-if ( (ch>=' a') && (ch<=' z' ) )
cout<<"lower case letter";
else
cout<<"It is not an alphabet";
return 0;
}
Output:
Enter alphabet: $ It is not an alphabet
Conditional or Ternary Operator: The conditional operator ‘? :’ is an alternative to if-else statements. As you can see in below example the operator selects one of the two values or expressions based on a given condition.
Note that the conditional operator selects one of the two values and not the statements as if-else statement. In addition, it cannot select more than one value at a time, whereas if-else statement can select and execute more than one statement at a time.
Ternary operator can replace this code:
if ( a < b ) { c = 1; } else { c = 2; }
You can replace the above code with this:
c = (a < b) ? 1 : 2;
WAP program to find maximum of two number using conditional or ternary operators.
#include <iostream>
using namespace std;
int main()
{
int a,b,max;
cout<<"Enter the values of a,b:"<<"\n";
cin>>a>>b;
max = (a > b) ? a : b;
cout<<"maximum value among a,b is "<<max;
return 0;
}
Output:
Enter the values of a,b: 10 20 maximum value among a,b is 20
switch: switch statement tests the value of an expression and compares it with the multiple cases, once the match found the block inside that case will be executed. If none of the case match the expression or its result than the default block is executed. Switch is a control statement that allows a value to change control of execution. All the cases values in a switch block has a different constant identifier.
The syntax of the switch statement:
switch(expression)
{
case : statement1;
break;
case : statement2;
break;
case : statement3;
default: statement4;
break;
}
WAP to find Add, mul, div, and sub using switch statement
#include <iostream>
using namespace std;
int main()
{
int opt,num1,num2;
int ADD,MUL,SUB,DIV;
cout<<"1. Addition"<<"\n";
cout<<"2. Subtraction"<<"\n";
cout<<"3. Multiplication"<<"\n";
cout<<"4. Division"<<"\n";
cout<<"Enter two numbers: "<<"\n";
cin>>num1>>num2;
cout<<"enter option 1-4: "<<"\n";
cin>>opt;
switch(opt)
{
case 1:
ADD = num1+num2;
cout<<"Addition num1 + num2 = "<<ADD;
break;
case 2:
SUB = num1-num2;
cout<<"Subtraction num1-num2 = "<<SUB;
break;
case 3:
MUL = num1*num2;
cout<<"Multiplication num1*num2 = "<<MUL;
break;
case 4:
DIV = num1/num2;
cout<<"Division num1/num2 = "<<DIV;
break;
default:
cout<<"out of options";
break;
}
cout<<"\n"<<"Bye bye! Thank you Visit again to hellocodies.com"<<"\n";
return 0;
}
Output:
1. Addition 2. Subtraction 3. Multiplication 4. Division Enter two numbers : 3 enter option 1-4: 3 Multiplication num1*num2 = 200 Bye bye! Thank you Visit again to hellocodies.com
This is the end of conditional statements in C/C++. Hope you understand it, if anything is wrong please use comment or contact form to inform.