0% found this document useful (0 votes)
39 views12 pages

If Else and Switch Statement in C++

Uploaded by

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

If Else and Switch Statement in C++

Uploaded by

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

Statements in C++

1. Simple statements
2. Structured statements-
decision control statements/conditional statements-if-else , switch …case
repetitive/iterative/loop statements-for loop, while loop and do…while
loop

If else Statement in C++


BY CHAITANYA SINGH | FILED UNDER: LEARN C++
Sometimes we need to execute a block of statements only when a particular
condition is met or not met. This is called decision making, as we are executing
a certain code after making a decision in the program logic. For decision making
in C++, we have four types of control statements (or control structures), which
are as follows:
a) if statement
b) nested if statement
c) if-else statement
d) if-else-if statement
If statement in C++
If statement consists a condition, followed by statement or a set of statements
as shown below:
Syntax of if statement

if(condition) if(a>b)
cout<<”a is max”;

{ else
Statement1(s); cout<<”b is max”;
Statement2;
}
The statements inside if parenthesis (usually referred as if body) gets executed
only when the given condition is true. If the condition is false then the
statements inside if body are completely ignored.

Flow diagram of If statement


Example of if statement
#include <iostream>
using namespace std;
int main(){
int num=70;
if( num < 100 ){
/* This cout statement will only execute,
* if the above condition is true
*/
cout<<"number is less than 100";
}

if(num > 100){


/* This cout statement will only execute,
* if the above condition is true
*/
cout<<"number is greater than 100";
}
return 0;
}
Output:
number is less than 100
Nested if statement in C++
When there is an if statement inside another if statement then it is called the
nested if statement.
The structure of nested if looks like this:
if(condition_1)
{
Statement1(s);

if(condition_2)
{
Statement2(s);
}
}

if(a>b)
{cout<<”a is max”;

if(a>c)
{cout<<”a is max”;}
}

Statement1 would execute if the condition_1 is true. Statement2 would only


execute if both the conditions( condition_1 and condition_2) are true.

Example of Nested if statement


#include <iostream>
using namespace std;
int main(){
int num=90;
/* Nested if statement. An if statement
* inside another if body
*/
if( num < 100 ){
cout<<"number is less than 100"<<endl;
if(num > 50){
cout<<"number is greater than 50";
}
}
return 0;
}
Output:
number is less than 100
number is greater than 50
If else statement in C++
Sometimes you have a condition and you want to execute a block of code if
condition is true and execute another piece of code if the same condition is
false. This can be achieved in C++ using if-else statement.
This is how an if-else statement looks:
if(condition)
{
Statement(s);
}
else
{
Statement(s);
}
The statements inside “if” would execute if the condition is true, and the
statements inside “else” would execute if the condition is false.

Flow diagram of if-else

Example of if-else statement


#include <iostream>
using namespace std;
int main(){
int num=66;
if( num < 50 ){
//This would run if above condition is true
cout<<"num is less than 50";
}
else {
//This would run if above condition is false
cout<<"num is greater than or equal 50";
}
return 0;
}

Output:
num is greater than or equal 50

if-else-if Statement in C++


if-else-if statement is used when we need to check multiple conditions. In this
control structure we have only one “if” and one “else”, however we can have
multiple “else if” blocks. This is how it looks:
if(condition_1)
{
/*if condition_1 is true execute this*/
statement(s);
}
else if(condition_2)
{
/* execute this if condition_1 is not met and
* condition_2 is met
*/
statement(s);
}
else if(condition_3)
{
/* execute this if condition_1 & condition_2 are
* not met and condition_3 is met
*/
statement(s);
}
.
.
.
else
{
/* if none of the condition is true
* then these statements gets executed
*/
statement(s);
}
Note: The most important point to note here is that in if-else-if, as soon as the
condition is met, the corresponding set of statements get executed, rest gets
ignored. If none of the condition is met then the statements inside “else” gets
executed.
Example of if-else-if
#include <iostream>
using namespace std;
int main()
{
int num;
cout<<"Enter an integer number between 1 & 99999: ";
cin>>num;
if(num <100 && num>=1)
{
cout<<"Its a two digit number";
}
else if(num <1000) && num>=100)
{
cout<<"Its a three digit number";
}
else if(num <10000 && num>=1000) {
cout<<"Its a four digit number";
}
else if(num <100000 && num>=10000) {
cout<<"Its a five digit number";
}
else {
cout<<"number is not between 1 & 99999";
}
return 0;
}
Output:
Enter an integer number between 1 & 99999: 8976
Its a four digit number

Switch Case statement in C++ with example

BY CHAITANYA SINGH | FILED UNDER: LEARN C++

Switch case statement is used when we have multiple conditions and we need
to perform different action based on the condition. When we have multiple
conditions and we need to execute a block of statements when a particular
condition is satisfied. In such case either we can use lengthy if..else-if
statement or switch case. The problem with lengthy if..else-if is that it becomes
complex when we have several conditions. The switch case is a clean and
efficient method of handling such scenarios.

The syntax of Switch case statement:

switch (variable or an integer expression)


{ case constant:
//C++ code ;
case constant:
//C++ code ;
default:
//C++ code ; }
Switch Case statement is mostly used with break statement even though the
break statement is optional. We will first see an example without break
statement and then we will discuss switch case with break

Example of Switch Case

#include <iostream>
using namespace std;

int main()
{
int num;
cout<<”enter the value of num”;
cin>>num; 2

switch(num) 2

{
case 1:
int a,b;
cout<<"Case1: Value is: "<<num<<endl;
break;
case 2:
cout<<"Case2: Value is: "<<num<<endl;
break;
case 3:
cout<<"Case3: Value is: "<<num<<endl;
break;
default:
cout<<" invalid Value " <<endl;

return 0;

Output:
Default: Value is: 5
Explanation: In switch I gave an expression, you can give variable as well. I gave
the expression num+2, where num value is 5 and after addition the expression
resulted 7. Since there is no case defined with value 4 the default case got
executed.
Switch Case Flow Diagram
It evaluates the value of expression or variable (based on whatever is given
inside switch braces), then based on the outcome it executes the corresponding
case.
Break statement in Switch Case
Before we discuss about break statement, Let’s see what happens when we don’t
use break statement in switch case. See the example below:
#include <iostream>
using namespace std;
int main(){
int i=2;
switch(i) {
case 1: cout<<"Case1 "<<endl;
case 2: cout<<"Case2 "<<endl;
break;
case 3: cout<<"Case3 "<<endl;
case 4: cout<<"Case4 "<<endl;
default: cout<<"Default "<<endl;
}
return 0;
}
Output:
Case2
Case3
Case4
Default
In the above program, we have the variable i inside switch braces, which means
whatever the value of variable i is, the corresponding case block gets executed.
We have passed integer value 2 to the switch, so the control switched to the
case 2, however we don’t have break statement after the case 2 that caused the
flow to continue to the subsequent cases till the end. However this is not what
we wanted, we wanted to execute the right case block and ignore rest blocks.
The solution to this issue is to use the break statement in after every case block.
Break statements are used when you want your program-flow to come out of the
switch body. Whenever a break statement is encountered in the switch body, the
execution flow would directly come out of the switch, ignoring rest of the cases.
This is why you must end each case block with the break statement.
Let’s take the same example but this time with break statement.
#include <iostream>
using namespace std;
int main(){
int i=2;
switch(i) {
case 1:
cout<<"Case1 "<<endl;
break;
case 2:
cout<<"Case2 "<<endl;
break;
case 3:
cout<<"Case3 "<<endl;
break;
case 4:
cout<<"Case4 "<<endl;
break;
default:
cout<<"Default "<<endl;
break;
}

return 0;
}
Output:
Case2
Now you can see that only case 2 got executed, rest of the subsequent cases
were ignored.
Why didn’t I use break statement after default?
The control would itself come out of the switch after default so I didn’t use
break statement after it, however if you want you can use it, there is no harm in
doing that.
Important Notes
1) Case doesn’t always need to have order 1, 2, 3 and so on. It can have any
integer value after case keyword. Also, case doesn’t need to be in an ascending
order always, you can specify them in any order based on the requirement.
2) You can also use characters in switch case. for example –
#include <iostream>
using namespace std;
int main()
{
char ch;
cout<<”enter your choice”<<endl;
cin>>ch; ‘a’
switch(ch)
{ Default
case 'd': cout<<"Case1 ";
break;
case 'b': cout<<"Case2 ";
break;
case 'x': cout<<"Case3 ";
break;
case 'y': cout<<"Case4 ";
break;
default: cout<<"Default ";
}
return 0;
}
3) Nesting of switch statements are allowed, which means you can have switch
statements inside another switch. However nested switch statements should be
avoided as it makes program more complex and less readable.

Switch case-Menu driven program

#include<iostream.h>
#include<conio.h>
void main()
{
int a,b,c; OUTPUT
clrscr();
cout<<”enter the values of a and b”; enter the values of a and b
cin>>a>>b; 10 20
cout<<”menu driven program”<<”\n”; menu driven program
cout<<”1=Addition” <<endl; 1=Addition
cout<<”2=Subtraction’<<endl; 2=Subtraction
cout<<”3=Even/Odd” <<endl; 3=Even/Odd
cout<<”Please enter your choice”<<endl; Please enter your choice
cin>>c;
5

switch(c) please enter a valid choice


{ case 1:
int d=a+b;
cout<<”sum of a and b=”<<d;
break;
case 2:
int e=a-b;
cout<<”difference of a and b=”<<e;
break;
case 3:
if(a%2==0)
cout<<”a is even”;
else
cout<<”a is odd”;
break;
default :
cout<<”please enter a valid choice”;
break;
}

getch();

write a program to check whether an entered number is even or odd

#include<iostream.h>
#include<conio.h>

int main()
{
int a, b;
clrscr();
cout<<”enter the value of a”;
cin>>a; 9
if(a%2==0);
cout<<” a is even”;
else
cout<<” a is odd”;

getch();
return 0;
}

WAP to find the roots of a quadratic equation.


D= b*b-4*a*c
If(D==0)
X=
Else if

Max of 3 nos say a,b,c,d


if(a<b && a<c && a<d)
cout<<”a is max”;
else if (b>a &&b>c &&b>d)
cout<<”b is max”;
else if(c>a &&c>b &&c>d)
cout<<”c is max”;
else
cout<<”d is max”;

You might also like