Monday, 2 September 2013

C++ statements with syntax

If statement : 

Syntax :   

if(test condition)
{
   statment1;
}

Example:

#include<iostream.h>
#include<conio.h>
#include<string.h>
void main()
{
int age;
cout <<"\n Enter the age";
cin >>age;
if (age>=18)
{
    cout <<"\n YOU CAN VOTE ";
}
getch();
}

Explanation : 

                 The control enters the if statement and the test condition will checked and if the condition is TRUE  then the output will be displayed . If the condition is FALSE then nothing will be displayed.


If......else Statement :

Syntax:

if(test condition) 

statement 1;

else 

statement 2;

Example :

#include<iostream.h>
#include<conio.h>
void main()
{
int n;
cout <<"\n Enter the number";
cin >>n;
if (n%2==0)
cout <<"\n You have entered EVEN no";
else
cout <<"\n You have entered ODD no ";
getch();
}

Explanation :

                 The control enters the main() function . After getting the input value the test condition will checked and if the test condition is TRUE then the statement 1 will be displayed,  otherwise statement 2 will be displayed .






 


            

No comments:

Post a Comment