Tuesday, 3 September 2013

SWITCH STATEMENT

SWITCH CASE:

SYNTAX:

switch(expression)
{
case 1: statement 1;
             break;
case 2: statement 2;
            break;
case 3: statement 3;
             break;
default : statement 4;
               break;
}

SAMPLE PROGRAM :

#include<iostream.h>
#include<conio.h>
void main()
{
int ch;
cout <<"\n ENTER THE NUMBERS BELOW FIVE ";
cin >> ch;
switch(ch)
{
case 1:cout <<"\n THE NO. IS ONE ";
           break;
case 2: cout <<"\n THE NO. IS TWO ";
            break;
case 3: cout <<"\n THE NO. IS THREE ";
            break;
case 4: cout <<"\n THE NO. IS FOUR ";
            break;
default : cout <<"\n ENTER THE CORRECT CHOICE ";
              break;
}
getch();
}

 


Monday, 2 September 2013

CONSTRUCTOR WITH ARGUMENTS

CONSTRUCTOR WITH ARGUMENTS:

#include<conio.h>
class add
{
int num1, num2, sum=0;
public()
{
add( int a, int b)       \\ CONSTRUCTOR WITH ARGUMENTS \\
{
   num1=a;
   num2=b;
}
void addition()
{
sum=num1+num2;
}
void display()
{
cout <<"\n NUM1= " <<num1;
cout <<"\n NUM2= " <<num2;
cout <<"\n SUM = " <<sum;
}
};
void main()
{
clrscr();
add a(50, 60) ;
a.addition();
a.display();
getch();
}

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 .