if statement in C++
different between if and switch statement
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"); };