11th Computer Science 12th Computer Science BCA CBSE CBSE JAC JAC

Conditional statements in C++ Language

Conditional statements in C++ Language
Written by AIPS

The statements which are used for decision making , are called conditional statements.

There are three conditional statements.

Note:

Statement: –

A statement is an instruction given to the computer to perform some specific task or action . A statement can be either single or compound.

single statement specifies single task or action and it is always terminated by semicolon(;) .

A compound statement specifies multiple actions and is enclosed with curly brackets( { } ). it is also known as block.

By default the statements are executed init the same order in which they appear in the program and each statement is executed only once . serial execution of statements makes a program inflexible and unsuitable for most of the practical application.

To make a program more flexible control statement are used.

control statements are classified into three categories.

Conditional statements

  1. conditional operator or Ternary operator
  2. if statement
  3. switch statement

Iterative statements or Looping

  1. for loop
  2. while loop
  3. do-while loop

Jump statements

  1. break statement
  2. continue statement
  3. goto statement

Conditional or Ternary operator:-

It is a conditional statement and used for making decision when we have two options.

It requires three operands so , also known as ternary operator.

syntax

condition ? <true block> : <false block> ;

Comma(,)

This operator is used for variable separator.

Q. write a program to check person is eligible for vote or not. using Ternary or conditional operator.compile time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
a=10;
a>=18?cout<<” eligible for vote”:cout<<” not eligible for vote”;
getch();
}

output

not eligible for vote

Q. write a program to check person is eligible for vote or not. (using Ternary or conditional operator.Run time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<”Enter age”;
cin>>a;
a>=18?cout<<” eligible for vote”:cout<<” not eligible for vote”;
getch();
}

output

Enter age=12

not eligible for vote

Q. write a program to check number is even or odd. (using Ternary or conditional operator.compile time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
n=5;
n%2==0?cout<<” Even number”:cout<<”Odd number”;
getch();
}

output

Odd number

Q. write a program to check number is even or odd. (using Ternary or conditional operator.Run time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<”Enter any number=”;
cin>>n;
n%2==0?cout<<” Even number”:cout<<”Odd number”;
getch();
}

output

Enter any number=5

Odd number

Q. write a program to check student is pass or fail. (using Ternary or conditional operator.compile time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m;
m=70;
m>=33?cout<<” student is pass”:cout<<”student is fail”;
getch();
}

output

student is pass

Q. write a program to check student is pass or fail. (using Ternary or conditional operator.Run time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int m;
cout<<”Enter marks=”;
cin>>m;
m>=33?cout<<” student is pass”:cout<<”student is fail”;
getch();
}

output

Enter marks=70

student is pass

Q. write a program to find greater between two numbers. (using Ternary or conditional operator.compile time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
a=10;
b=20;
a>b?cout<<” a is greater”:cout<<”b is greater”;
getch();
}

output

b is greater

Q. write a program to find greater between two numbers. (using Ternary or conditional operator.Run time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<”Enter value of a and b=”;
cin>>a>>b;
a>b?cout<<” a is greater”:cout<<”b is greater”;
getch();
}

Output

Enter value of a and b=10

20

b is greater

Q. write a program to find the greatest among three numbers. (using Ternary or conditional operator.compile time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=20;
c=30;
a>b && a>c ?cout<<” a is the greatest”: b>c? cout<<”b is the greatest”:cout<<”c is the greatest”;
getch();
}

Output

c is the greatest

Q. write a program to find the greatest among three numbers. (using Ternary or conditional operator.Run time.)

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<”Enter value of a ,b and c=”;
cin>>a>>b>>c;
a>b && a>c ?cout<<” a is the greatest”: b>c? cout<<”b is the greatest”:cout<<”c is the greatest”;
getch();
}

Output

Enter value of a and b=10

20

30

c is the greatest

If statement:-

It is a conditional statement and used for making decision . it can be used in four ways.

  1. Simple if
  2. If-else
  3. Nested if-else
  4. Ladder if-else

Simple if:-

This statement executed some statements when specified condition is true.

it is also known as single selection structure.

multiple conditions can be combined using logical operators.

Syntax

if(test condition)

{

……………………….

…………………………

}

Q. Write a program to check student is pass or fail. using simple if.compile time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float m;
m=12;
if(m>=33)
{
cout<<”student is pass”;
}
if(m<33)
{
cout<<”student is fail”;
}
getch();
}

Output

Sudent is fail

Q. Write a program to check student is pass or fail. using simple if.run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float m;
cout<<”enter marks=”;
cin>>m;
if(m>=33)
{
cout<<”student is pass”;
}
if(m<33)
{
cout<<”student is fail”;
}
getch();
}

Output

Enter marks=70

Student is pass

Q. Write a program to check number is even or odd using simple if.compile time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
n=5;
if(n%2==0)
{
cout<<”number is even”;
}
if(n%2==1)
{
cout<<”number is odd”;
}
getch();
}

output

number is odd

Q. Write a program to check number is even or odd using simple if.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<”Enter any number=”;
cin>>n;
if(n%2==0)
{
cout<<”number is even”;
}
if(n%2==1)
{
cout<<”number is odd”;
}
getch();
}

Output

Enter any number=5

number is odd

Q. Write a program to check person is eligible for vote or not. using simple if.compile time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
a=10;
if(a>=18)
{
cout<<” eligible for vote”;
}
if(a<18)
{
cout<<” not eligible for vote”;
}
getch();
}

output

not eligible for vote

Q. Write a program to check person is eligible for vote or not. using simple if.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<”Enter age=”;
cin>>a;
if(a>=18)
{
cout<<” eligible for vote”;
}
if(a<18)
{
cout<<” not eligible for vote”;
}
getch();
}

output

Enter age=12

not eligible for vote

Q. Write a program to find greater between two numbers. using simple if.compile time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
a=10;
b=20;
if(a>b)
{
cout<<”a is greater”;
}
if(b>a)
{
cout<<”b is greater”;
}
getch();
}

Output

b is greater

Q. Write a program to find greater between two numbers. using simple if.Run time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<”Enter value of a and b=”;
cin>>a>>b;
if(a>b)
{
cout<<”a is greater”;
}
if(b>a)
{
cout<<”b is greater”;
}
getch();
}

output

Enter value of a and b=10

20

b is greater

Q. write a program to find the greatest among three numbers. using simple if.compile time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=20;
c=30;
if(a>b && a>c)
{
cout<<” a is the greatest”;
}
if(b>a && b>c)
{
cout<<” b is the greatest”;
}
if(c>a && c>b)
{
cout<<” c is the greatest”;
}

getch();
}

output

c is the greatest

Q. write a program to find the greatest among three numbers. using simple if.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<”Enter value of a,b and c=”;
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<” a is the greatest”;
}
if(b>a && b>c)
{
cout<<” b is the greatest”;
}
if(c>a && c>b)
{
cout<<” c is the greatest”;
}

getch();
}

output

Enter value of a,b and c=10

20

30

c is the greatest

Q.An electricity board charges according to following rates:

For the First 100 units——   Rs 2 per unit

For the Next 200 units——   Rs 3 per unit.

Beyond 300 units——   Rs 4 per unit.

   All users are charged meter charge also, which is Rs 50. Write a program in C++ to read the number of units consumed  and  print out the charges.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float cu,ch;
cout<<”Enter consumed unit=”;
cin>>cu;
if(cu<=100)
{
ch=cu*2;
}
if(cu>100 && cu<=300)
{
ch=200+(cu-100)*3;
}
if(cu>300)
{
ch=800+(cu-300)*4;
}
cout<<”charges=”<<ch;
cout<<”\ncharges with meter=”<<(ch+50);
getch();
}

Output

Enter consumed unit=150

charges=350

charges with meter=400

if else:-

This statement executes some statements when specified condition is true or false.

It is also known as double selection structure.

It is an alternative of conditional operator.

syntax

if(test condition)

{

true block

}

else

{

false block

}

Q. write a program to check student is pass or fail. using if-else, compile time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float m;
m=12;
if(m>=33)
{
cout<<”student is pass”;
}
else
{
cout<<”student is fail”;
}
getch();
}

Output

Sudent is fail

Q. Write a program to check student is pass or fail. Using if-else.run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float m;
cout<<”enter marks=”;
cin>>m;
if(m>=33)
{
cout<<”student is pass”;
}
else
{
cout<<”student is fail”;
}
getch();
}

Output

Enter marks=70

Student is pass

Q. Write a program to check number is even or odd using  if-else.compile time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
n=5;
if(n%2==0)
{
cout<<”number is even”;
}
else
{
cout<<”number is odd”;
}
getch();
}

Output

number is odd

Q. Write a program to check number is even or odd using  if-else.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int n;
cout<<”Enter any number=”;
cin>>n;
if(n%2==0)
{
cout<<”number is even”;
}
else
{
cout<<”number is odd”;
}
getch();
}

Output

Enter any number=5

number is odd

Q. write a program to check person is eligible for vote or not. Using if-else.compile time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
a=10;
if(a>=18)
{
cout<<” eligible for vote”;
}
else
{
cout<<” not eligible for vote”;
}
getch();
}

output

not eligible for vote

Q. write a program to check person is eligible for vote or not. using if-else.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a;
cout<<”Enter age=”;
cin>>a;
if(a>=18)
{
cout<<” eligible for vote”;
}
else
{
cout<<” not eligible for vote”;
}
getch();
}

output

Enter age=12

not eligible for vote

Q. write a program to find greater between two numbers. using if-else.compile time

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
a=10;
b=20;
if(a>b)
{
cout<<”a is greater”;
}
else
{
cout<<”b is greater”;
}
getch();
}

output

b is greater

Q. write a program to find greater between two numbers. using if-else.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b;
cout<<”Enter value of a and b=”;
cin>>a>>b;
if(a>b)
{
cout<<”a is greater”;
}
else
{
cout<<”b is greater”;
}
getch();
}

Output

Enter value of a and b=10

20

b is greater

Nested if-else

“if -else” inside another if -else , is known as Nested if -else. It is used to check multiple condition.

if(condition)

{

if(condition)

{

}

else

{

}

}

else

{

if(condition)

{

}

else

{

}

}

Q. write a program to find the greatest among three numbers. using Nested if-else.compile time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=20;
c=30;
if(a>b)
{
if(a>c)
{
cout<<”a is the greatest”;
}
else
{
cout<<”c is the greatest”;
}

}

else
{
if(b>c)
{
cout<<”b is the greatest”;
}
else
{
cout<<”c is the greatest”;
}

}
getch();
}

output

c is the greatest

Q. write a program to find the greatest among three numbers. using Nested if-else.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<”Enter value of a,b and c=”;
cin>>a>>b>>c;
if(a>b)
{
if(a>c)
{
cout<<”a is the greatest”;
}
else
{
cout<<”c is the greatest”;
}
}

else
{
if(b>c)
{
cout<<”b is the greatest”;
}
else
{
cout<<”c is the greatest”;
}

}
getch();
}

output

Enter value of a,b and c=10

20

30

c is the greatest

Ladder If-else

It is derived form of if statement and used for decision making when we have multiple options.

It evaluates test condition from the top and executes the statement which satisfied the condition and skip from the rest cases.

when none of the condition is satisfied then else block is executed.

if(test condition1)

{

statement 1;

}

else if (test condition 2)

{

statement 2;

}

else if(test condition 3)

{

statement 3;

}

else if (test condition n)

{

statement n;

}

else

{

default message;

}

Q. Write a program to find the greatest among three numbers. using ladder if-else.compile time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
a=10;
b=20;
c=30;
if(a>b && a>c)
{
cout<<” a is the greatest”;
}
else if(b>a && b>c)
{
cout<<” b is the greatest”;
}
else if(c>a && c>b)
{
cout<<” c is the greatest”;
}
else 
{
cout<<”all are equal”;
}

getch();
}

output

c is greatest

Q. Write a program to find the greatest among three numbers. using ladder if-else.Run time.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
int a,b,c;
cout<<”Enter value of a,b and c=”;
cin>>a>>b>>c;
if(a>b && a>c)
{
cout<<” a is the greatest”;
}
else if(b>a && b>c)
{
cout<<” b is the greatest”;
}
else if(c>a && c>b)
{
cout<<” c is the greatest”;
}
else 
{
cout<<”all are equal”;
}

getch();
}

output

Enter value of a,b and c=10

20

30

c is the greatest

Q.An electricity board charges according to following rates:

For the First 100 units——   Rs 2 per unit

For the Next 200 units——   Rs 3 per unit.

Beyond 300 units——   Rs 4 per unit.

   All users are charged meter charge also, which is Rs 50. Write a program in C++ to read the number of units consumed  and  print out the charges.

#include<iostream.h>
#include<conio.h>
void main()
{
clrscr();
float cu,ch;
cout<<”Enter consumed unit=”;
cin>>cu;
if(cu<=100)
{
ch=cu*2;
}
else if(cu>100 && cu<=300)
{
ch=200+(cu-100)*3;
}
else
{
ch=800+(cu-300)*4;
}
cout<<”charges=”<<ch;
cout<<”\ncharges with meter=”<<(ch+50);
getch();
}

Output

Enter consumed unit=150

charges=350

charges with meter=400

About the author

AIPS

Leave a Comment