Friday, 18 July 2014

C++ Basic Skills


//skillone.C
#include <iostream>
using namespace std;

int main()
{
    int a =30, b =5;
    if (a<10)
        a=a-5;
    b=b+5;
    cout << "Value of a and b is :" <<  a << " and "  << b << endl;
    return 0;
}

result 30 and 10

//skilltwo.C
#include<iostream>
using namespace std;

int main ()
{

 int a = 8, b = 0, c = 0;
    if(!a < 20 && !b || c)
        cout << "check success" << endl;
    else
        cout << "check failure" << endl;

    return 0;
}

result: check success

//skillthree.C
#include<iostream>
using namespace std;
int main()
{
 int i= 1, j=9;

         if (i>=5 && j<5);

         i = j+2;

         cout << " i val " << i <<endl;

    return 0;
}

result i val 11

//skillfour.C
#include<iostream>
using namespace std;
int main()
{
 int a=0, b=0;

         if(!a)
         {
             b =!a;
             if(b)
                a =!b;
         }
         cout << " a  and b val are: " << a << "  " << b << endl;
    return 0;
}

result : a  and b val are: 0  1

//skillfive.C
#include<iostream>
using namespace std;

int main ()
{
int a=5;
         begin:
             if (a)
             {
                 cout << " " << a ;
                 a--;
                 goto begin;
             }

    return 0;
}

result: 5 4 3 2 1

//skillsix.C
#include<iostream>
using namespace std;

int main()
{
int a=2, x=10;
if(a == 2)
    if(x== 8)
        cout <<"a equal 2 and x equal 8" << endl;
else
    cout << "a not equal  to 2" << endl;
    return 0;
}

result : a not equal to 2

//skillseven.C
#include<iostream>
using namespace std;

int main()
{
int a =6, b =4;
    while (a+b)
    {

        cout << a  << " " << b << endl;
        a = a/2;
        b%=3;
    }
    return 0;
}

result :
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
0 1
.........
value of b never becomes zero and hence the condition never becomes false.

//skilleight.C
#include<iostream>
using namespace std;

int main()
{
 int a =9;
    if (a = 5)
        cout << "hurrah I won" << endl;
    else
        cout << "I have to try now" << endl;
    return 0;
}

result:
hurrah I won

//skillnine.C
#include <iostream>
using namespace std;
int main()
{
int i, j, x =0;
        for (i = 0; i<5;i++)
            for(j=i; j>0 ; j--)
                x =i+j+1;
        cout << "x val: " << x << endl;
    return 0;
}

result: x val: 6

//skillten.C
#include <iostream>
using namespace std;

int main()
{
int i = 0, count = 0 ;

        while(i++)
        {
            count++;
            cout << "val of i from start: "<< count;
            if(count == 6)
                break;
        }
        cout << "count : " << count;

    return 0;
}

result:
count : 0

//skilleleven.C
#include <iostream>
using namespace std;


int main ()
{
int j, count = 0;
        for (j =0;  j<5; j++)
        {
         int j = 0;
         while(j++ < 5)
            count++;
        }

        cout << "count val: " << count << endl;

    return 0;
}

result:
count val: 25

//skilltwelve.C
#include <iostream>
using namespace std;

int main ()
{
int j, count = 0;
        for (j =0;  j<5; j++)
        {
        // int j = 0;
         while(j++ < 5)             // 0 , 1, 2, 3, 4
            count++;
        }

        cout << "count val: " << count << endl;
    return 0;
}

result:
count val: 5


//skillthirteen.C
#include <iostream>
using namespace std;

int main()
{

int i;
        for (i =1; i<10;i++)
        {
            if (i== 3)
                continue;
            cout << i << endl;
        }
    return 0;
}


result:

1
2
4
5
6
7
8
9

Note value 3 is skipped because of continue statement.

//skillfourteen.C
#include<iostream>
using namespace std;
int main()
{

int i=1;
        while (i<10)
        {
            if (i==3)
                continue;
            cout << i << endl;
           i++;
        }
    return 0;
}

result:
this program print 1 2 and then goes into an infinite loop 

//skillfifteen.C
#include<iostream>
using namespace std;

int main()
{
 char ch ='A';
        while(ch <='D')
        {
            switch(ch)
            {
            case 'A' :
            case 'B':
                        ch++;
                        continue;
            case 'C' :
            case 'D' :
                ch++;
            }
            cout << ch << endl;
        }
    return 0;
}

result:
D
E
here continue statement is used inside while loop. Hence on continue statement execution, the control goes to while loop condition check and thus works fine.

//skillsixteen.C
#include<iostream>
using namespace std;
int main()
{
char ch='A';
        switch(ch)
        {
        case 'A':
        case 'B':
            ch++;
            continue;

        case 'C' :
        case 'D':
            ch++;
        }
    return 0;
}

result:
Compiliation error
'continue' statement not in  loop statement
 








No comments:

Post a Comment