Conditional Statement in C++
The statements of a computer program are executed one after the other in the order in which they are written. This is known as sequential execution of the program.
The order of execution of the statements in a program can be changed. This is done with the help of conditional statements. The conditional statements are used to execute a set of statements after testing a condition. The conditional statements are also called selection statements.
if Statement
The if statement is used to execute a set of statement after testing a condition.
The if statement evaluates a condition. If the given conditions is true, the statement following the "if statement" is executed. If the given condition is false, the statement following the "if statement" condition is ignored and the control transfers to the next statement.
Syntax:
The syntax of the "if statement" is:
if (condiiton)
{
Statement-1;
Statement-2;
Statement-3
Statement-m
}
Statement-n
In the above syntax, the set of statements (from statement-1 to statement-m) has been enclosed in curly braces. These are called compound statement.
If the condition given in the "if statement" is true, the compound statement given in the curly braces are executed. If the condition is false, the control shifts to the statement-n and the set of statement that follows the "if statement" is ignored.
Flowchart of if statement:
Example:
#include<iostream>
using namespace std;
int main()
{
int a,b;
a=70;
b=50;
if(a>b)
cout<<"Karachi"<<"\n";
cout<<"ok"<<"\n";
}
ok
If the values of a is 20, then the given conditions becomes false and the statement cout<<"Karachi"<<"\n"; will be ignored. The control will shift to the next statement and only the statement cout<<"ok"<<"\n"; will be executed.
if-else Statement
This is another form of "if statement". It is used for making two way decision. In this statement, one condition and two blocks of statements are given. Either one of the two blocks of statements is executed after evaluating a condition.
The if-else statement tests the given relational condition. If the condition is true then the first block of statements is executed. If the condition is false, the first block of statement is ignored and the second block following the else is executed.
Syntax:
if (condition)
Statement-1;
else
Statement-2;
Flowchart of if-else Statement:
Example:
Write a program to input two integers and then to find out whether these numbers are equal or not.
#include<iostream>
using namespace std;
int main()
{
int a,b;
cout<<"Enter first number :";
cin>>a;
cout<<"Enter second number :";
cin>>b;
if(a%2==b%2)
cout<<"Both numbers are equal";
else
cout<<"Numbers are different";
}
nested if Statement
Syntax:
Flowchart of nested if Statement:
Write a program to input three integer values from the keyboard. Find out the greatest number and print it on the screen.
Switch Statement
Syntax
The syntax for a switch statement in C++ is as follows:
switch(expression) {
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
// you can have any number of case statements.
default : //Optional
statement(s);
}
Flowchart:
This would produce the following result:
You passed Your grade is D
Break Statement
Syntax
The syntax for a break statement in C is as follows:
break;
Flow Diagram:
Example
output:
1 2