UIU: Learn C by Examples : Control Instructions


Program 1


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    for ( int i= -1; i <= 10; i++ ) {
        if ( i < 5 ) continue;
        else break;

        printf ("UIU\n");
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Program 2


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int i = 0;

    for (; i <= 5; i++ )
        printf ("%d ", i);

    printf ("\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 3


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    printf (5 > 10 ? "C" : "C++");

    printf ("\n");

    printf (5 < 10 ? "Hello" : "World");

    printf ("\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 4


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a = 500;
    int b = 100;
    int c; 
    
    if ( !a >= 400 ) 
        b = 300;
    
    c = 200;
    
    printf ("%d %d %d\n", a, b, c);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 5


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int i = 9;

    while ( i-- != 0 )
        printf ("%d", i);

    printf ("\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 6


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int x = 3;
    double y =  3.0;

    if ( x == y )
        printf ("equal\n");
    else
        printf ("not equal\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 7


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int i = 0;

    for ( ++i; ++i != 9; i < 5 )
        printf ("%d", i);

    printf ("\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 8


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int val;

    if ( val = scanf ("%d", &val) )
        printf ("Inside if\n");
    else
        printf ("Inside else\n");

    if ( val == printf ("Hello\n") )
        printf ("Inside if\n");
    else
        printf ("Inside else\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 9


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    unsigned int i = 10;

    while ( i != 0 )
        printf ("%d", --i);

    printf ("\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 10


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    double a = 0.7;

    if ( 0.7 > a )
        printf ("Hi\n");
    else
        printf ("Hello\n");

	return 0;
}

// @END_OF_SOURCE_CODE

Program 11


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int k = 5, num = 30;

    k = num < 10 ? 100 : 200;

    printf ("%d %d\n", k, num);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 12


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int x = 10, y = 20;

    if (!(!x) && x)
        printf ("%d\n", x);
    else
        printf ("%d\n", y);

	return 0;
}

// @END_OF_SOURCE_CODE

Program 13


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    for ( int i = 0; i < 5; i++ ) {
        switch (i) {
        default:
            printf ("default\n");
        case 0:
            printf ("case 0\n");
            break;
        case 1:
            printf ("case 1\n");
        case 2:
            printf ("case 2\n");
            break;
        case 3:
            printf ("case 3\n");

        }
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Program 14


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{

    int i = 1;

    switch (i) {
        printf ("Hello\n");
    case 1:
        printf ("case 1\n");
    case 2:
        printf ("case 2\n");
        break;
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Program 15


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int i = 1; 
    
    for (;;) {
        printf ("%d\n", i++);
        if ( i > 9 ) break;
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Program 16


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int i = 1;

    while (1) {
        printf ("%d\n", ++i);
        if ( i > 9 ) break;
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Program 17


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int p = 10;

    switch (p) {
    case 19:
        printf ("10\n");
    case 20:
        printf ("20\n");
    case p:
        printf ("p\n");

    }

	return 0;
}

// @END_OF_SOURCE_CODE

Program 18


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int p = 10;

    switch (p) {
    case 9 + 1:
        printf ("10\n");
        break;
    case 20:
        printf ("20\n");
        break;
    }

	return 0;
}

// @END_OF_SOURCE_CODE

Program 19


// @BEGIN_OF_SOURCE_CODE

#include <cstdio>

int main ()
{
    int a [3] = {3, 2, 1}; 
    
    int x = 5;
    
    if ( x == 5 || a [100] == 8 ) 
        printf ("crash!");
    else 
        printf ("Hello");

	return 0;
}

// @END_OF_SOURCE_CODE

Acknowledgement: http://indiabix.com/

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s