knowledgediarybd.com

Your Virtual Knowledge Campus

If, Else If..Else & Switch Statement In C++

J4If,else if, Else & Switch Statement In C++ are  the most important in c++ programming. Mainly they are used for checking one or more  conditional operation like grater than, less than or equal e.t.c. logical condition.

These statement provides a easiest way to solve a problem in a very short time & increase the performance of the c++ program code.

Dear learner, here you can learn how to use if, else, else if, and

switch statement in c++ that will satisfy your interest to learn c++ language.

Decision making statements in C++:

There are Four types of decision making statements in C++. These are:

1. if statement

2. if else statement

3. if else if ..else  statement.

4. switch statement

1. if statement:

you can Use it when  you need to check a specific condition is true.

Syntax of  if statement :

if ( condition)

code to be executed here if condition is true;

For Example:

#include<iostream>
using namespace std;
int main()
{
int x=9;

if(x<10)
cout<<"The if condition is true"<<endl;

return 0;
}

out put will be:The if condition is true.

2. if else statement:

Use it when  you need to check  one  condition but result may be two Like: yes or No

Syntax of if else statement:

if ( condition )

  {

A Statement to be executed if condition is true;

}

else

  {

  A Statement to be executed if condition is false;

}

For Example:

#include<iostream>
using namespace std;
int main()
{
int x=11;

if(x<10)
{
cout<<"The if condition is true"<<endl;
}
else
{
cout<<"The if condition is False"<<endl;
}

return 0;
}

3. if else if ..else  statement.

Use it when  you will need to check multiple condition statement.

Syntax  of if else if ..else  statement :

if ( condition )

  {

First statement executed if condition is true;

}

elseif ( condition )

  {

Second statement executed if condition is true;

}

else

  {

Final statement executed if all above condition is false;

}

For Example:

#include<iostream>
using namespace std;
int main()
{
int x=10;

if(x<10)
{
cout<<"The value of x is :"<<x<<endl;
}
else if (x>10)
{
cout<<"The value of x is :"<<x<<endl;
}
else //it is last else here no need to check any condition.

cout<<"The value of x is:10"<<endl;

return 0;
}

4. switch statement

switch statement allows a variable to be tested for equality against a number of values. Each value is called a case, and the variable being switched on is checked for every case.

Syntax  of switch statement :

switch statement
 Example of switch statement:

#include<iostream>
using namespace std;
int main()
{
int marks;
cout<<" Enter your marks:"<<endl;
cin>>marks;
switch(marks/5)
{
case 20:
case 19:
case 18:
case 17:
case 16:cout<<"your Gread is =A+"<<endl;break;
case 15:cout<<"your Gread is =A"<<endl;break;
case 14:cout<<"your Gread is =A-"<<endl;break;
case 13:cout<<"your Gread is =B+"<<endl;break;
case 12:cout<<"your Gread is =B"<<endl;break;
case 11: cout<<"your Gread is =B-"<<endl;break;
case 10:cout<<"your Gread is =C+"<<endl;break;
case 9: cout<<"your Gread is =C-"<<endl;break;
case 8: cout<<"your Gread is =D"<<endl;break;
default: cout<<"your Gread is =F"<<endl;break;
}
return 0;
}

Note:More interested things are waiting to publish..Stay connected to us..

——————-:Thank you:—————–

Related Post or you may interest:

  1. Introduction & Fundamental of C++
  2. Code Blocks Compiler Download
  3. Variable In C++
  4. Data Types In C++
  5. Token in c++
  6. C++ Operators,Operands and expression
  7. For loop while loop&Do while loop in C++
  8. Continue and Break Statement In C++
knowledgediarybd.com © 2017
shares