IF Statement Using Language C++ in DEV C++
keep practicing on it.
There are different ways to use if statements given below.
- If statement
- If else Statement
- Nested if statements
- Nested if else statements
- Exercises
If Statement in C++
It is just like number of possibilities you have and depending upon your condition , it compares and gives you the desired result.
Syntax
if(boolean expression)
{
statements;
}
now if the condition is true , statements will execute ,other wise flow of program jumps to statements after the if statement.There can be any number of if statements depending upon your program.
Example:
Lets take on example on the if statement. I will define integer variable and assign some value to it and then according to condition in the if statement , statements are executed , carefully look at the program , you can understand this easily.
Program code:
Output: After compiling and execution , the output will be as follows
If else Statement in C++
Syntax
if( boolean expression )
{
statements ;
}statements ;
else
{
{
statements ;
}
}
How it works:
if else condition is like you can say that choice between two things. Choice depends upon the Boolean expression you are going to use.If boolean expression is TRUE than statements below the if execute otherwise statements below the else execute.
Example:
Lets take an example , i define two integers variable x and y and assign them two values 10 , 15 respectively.I put boolean expression that if variable x is greater than y than output statement will dislpay TRUE and if variable x is less than variable y than output statement will b FALSE.
Lets take an example , i define two integers variable x and y and assign them two values 10 , 15 respectively.I put boolean expression that if variable x is greater than y than output statement will dislpay TRUE and if variable x is less than variable y than output statement will b FALSE.
So my code looks like this
Go to Execute option , compile it first if there are errors somewhere in program it will show otherwise it will execute the .exe file where output looks like this
keep practicing on it.
Nested if Statements in C++
It is very interesting that you can use if statements inside another if statements. Now if you know the use of if statements you can easy learn this.
Syntax:
if (boolean expression)
{
if (boolean expression)
{
.
.
.
}
}
Dots represent that you can use any number of statements depending upon your program.
Example:
Now under stand this program
Program code:
Output: After compiling and executing , the output is
In the similar way you can if else statements in nested loops.
Exercises:
1.What is the output of following program ?
#include<iostream>
using namespace std;
int main()
{
int x,y ;
x = 10 ;
y = 15 ;
if( x >= y )
cout<<" True ";
else
cout<<" False " ;
}
2.What is the output of following program ?
#include<iostream>
using namespace std;
int main()
{
int x,y ;
x = 10 ;
y = 15 ;
if( x <= y )
cout<<" True ";
else
cout<<" False " ;
}
3.What is the output of following program ?
#include<iostream>
using namespace std;
int main()
{
int x,y ;
x = 10 ;
y = 15 ;
if( x & y )
cout<<" True ";
else
cout<<" False " ;
}
4.What is the output of following program ?
#include<iostream>
using namespace std;
int main()
{
int x,y ;
x = 10 ;
y = 15 ;
if( x || y )
cout<<" True ";
else
cout<<" False " ;
}
5.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a = 9;
int b = 19;
if(a < 10)
{
if (b < 20)
{
cout<<"a is less than 10"<<endl<<endl;
cout<<"a is less than 20"<<endl<<endl;
}
}
cout<<" value of a is = "<< a <<endl;
cout<<" value of b is = "<< b <<endl;
getch();
}
6.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a = 9;
int b = 19;
if(a > 10)
{
if (a > 20)
{
cout<<"a is less than 10"<<endl<<endl;
cout<<"a is less than 20"<<endl<<endl;
}
}
cout<<" value of a is = "<< a <<endl;
cout<<" value of b is = "<< b <<endl;
getch();
}
7.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a = 9;
int b = 19;
if(a >= 10)
{
if (a >= 20)
{
cout<<"a is less than 10"<<endl<<endl;
cout<<"a is less than 20"<<endl<<endl;
}
}
cout<<" value of a is = "<< a <<endl;
cout<<" value of b is = "<< b <<endl;
getch();
}
8.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a = 10;
if(a > 50)
{
cout<<"a is less than 50"<<endl<<endl;
}
cout<<" value of a is = "<< a <<endl;
getch();
}
9.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a = 10;
if(a = 50)
{
cout<<"a is less than 50"<<endl<<endl;
}
cout<<" value of a is = "<< a <<endl;
getch();
}
10.What is the output of following program ?
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int a = 10;
if(a <= 50)
{
cout<<"a is less than 50"<<endl<<endl;
}
cout<<" value of a is = "<< a <<endl;
getch();
}
Practice on these exercises.
You're the best,thanks for all of your hard work
ReplyDeletereally the best can you teach me
ReplyDelete