C++ conditional statements | if,else ,elseif statement in c++ | nesting of if else statement in c++ | C++ if statement | example | My Cs Tutorial
If,else statement in c++ | My CS Tutorial |
C++ supports the usual logical conditions from mathematics:-
Less than: a < b
Less than or equal to: a <= b
Greater than: a > b
Greater than or equal to: a >= b
Equal to a == b
Not Equal to: a != b
You can use these conditions to perform different actions for different decisions.
C++ has the following conditional statements:
Use if to specify a block of code to be executed, if a specified condition is true
Use else to specify a block of code to be executed, if the same condition is false
Use else if to specify a new condition to test, if the first condition is false
Use switch to specify many alternative blocks of code to be executed
CONDITIONAL STRUCTURE: if statement
The if keyword is used to execute a statement or block only if a condition is fulfilled. Its form is:-
if(condition) statement....
Where condition is the expression that is being evaluated. If this condition is true, statement is executed. If it is false, statement is ignored (not executed) and the program continues right after this conditional structure.
For example, the following code fragment prints x is 100 only if the value stored in the x variable is indeed 100:-
if (x == 100)
cout << "x is 100";
If we want more than a single statement to be executed in case that the condition is true we can specify a block using braces { }:-
if (x == 100)
{
cout << "x is ";
cout << x;
}
C++ else statement
We can additionally specify what we want to happen if the condition is not true by using the keyword else. Its form used in conjunction with if is:-
if (condition)
statement1...
else
statement2...
For example:-
if (x == 100)
cout << "x is 100";
else
cout << "x is not 100";
prints on the screen x is 100 if indeed x has a value of 100, but if it has not -and only if not- it prints out x is not 100.
The if + else structures can be concatenated with the intention of verifying a range of values. The following example shows its use telling if the value currently stored in x is positive, negative or none of them (i.e. zero):-
if (x > 0)
cout << "x is positive";
else if (x < 0)
cout << "x is negative";
else
cout << "x is 0";
C++ else if statement
In C++ language, else if statement like a if statement.It is use when if statement have many conditions.
Syntax of else if statement:-
if(condition)
{
// GIVEN STATEMENT OF IF STATEMENT
}
else if
{
// GIVEN STATEMENT OF ELSE IF STATEMENT
}
else
{
// GIVEN STATEMENT OF ELSE STATEMENT
}
Example:
#include<iostream.h>
#include<conio.h>
int main()
{
int num;
cout<<"\n enter the number between 1 to 3=";
cin>>num;
if(num==1)
cout<<"one";
else if(num==2)
cout<<"two";
else if(num==3)
cout<<"three";
else
cout<<"wrong entry";
return 0;
}
C++ nested if else statement
In C++ programming nested if else statement is a important feature. When we use if or else statement inside a if statement or else statement this is called nesting of if else statement.
Example:
#include<iostream.h>
#include<conio.h>
int main()
{
int a=10,b=25;
clrscr();
if(a==10)
{
if(b==25)
{
cout<<"value of a is 10, and value of b is 25";
}
}
return 0;
}
C++ conditional statements | if,else ,elseif statement in c++ | nesting of if else statement in c++ | C++ if statement | example | My Cs Tutorial
_______________________________________
Please share this post and blog link with your friends.For more programs use this blog.
If you have any problem, please comment in comment box, subscribe this blog for notifications of new post on your email and follow this blog.If you have any method of this tutorial or program or want to give any suggestion send email on hc78326@gmail.com
Created by-- HARSH CHAUHAN
No comments:
Post a Comment