Thursday 5 March 2015

if statement in C++

if statement in C++ different between if and switch statement if is the conditional statement in C++ it use to check the condit... thumbnail 1 summary

if statement in C++

different between if and switch statement


if is the conditional statement in C++ it use to check the condition for example if we have two condition pass and fail so if student marks is less than 33 so then fail must be displayed on the screen and if the student marks in greater the 33 so then the pass must be displayed on the screen. It syntax is given blew.
 Syntax
if (condition)
{
a statement or a group of statement
}

Example 1
#include<iostream.h>
int main()
{
int marks=60;
if(marks>33)
{
cout<<"You are Passed in your examination"<<endl;
}
system("pause");
return 0;
};
This is the simple example which show you that how if statement works. In if statement first the condition is checked and if the condition is true so then the program is executed but if condition is false then program skip the statement which is inside the if statement and then jump to the next line.
Example 2
#include<iostream.h>
int main()
{
int a=5;
int b=6;
if(a==b)
cout<<"a and b are equal"<<endl;
if(a>b)
cout<<"a is greater than b"<<endl;
if(a<b)
cout<<"a is less  than b"<<endl;
system ("pause");
return 0;
};



Example 3
#include<iostream.h>
int main()
{
float GPA;
cout<<"Enter your GPA"<<endl;
cin>>GPA;
if(GPA>2.9)
{
cout<<" You are eligible for Laptop scheme."<<endl;
}
if(GPA<2.9)
{
cout<<"You are not eligible for Laptop sachem."<<endl;
}
return 0;
system("pause");
};