FreePrograms


C language is a middle level language. It is a structured language. C language is a case sensitive language. All syntax written in c language is in lower case. C is the basis for C++.
  CONTACT US   ABOUT US   PRIVACY  DISCLAIMER

FREE C PROGRAMS 

What is C C vs C++ vs Java Program Structure Data Types in C Basic Rules of C & C++
Functions in C If, Else Conditions Loops in C Switch Case Arrays
Pointers Structures in C Strings in C Command Line Arguments Type Casting
Linked Lists Recursion Binary Trees Inheritance Multiple Inheritance
Templates File I/O Object Oriented Programming Data Structures in C C interview Questions

Switch Case in C Language




    The basic form of a switch statement is:

    switch (variable)

    {

    case expression1:

    do something 1;
    break;


    case expression2:
    do something 2;
    break;


    ..
    default:
    do default processing;

    }

    In the c program below, if you enter 5 , it will display you entered 5, else if the condition fails it will display the default , that is "i don't know what u entered". The break statement terminates the loop.

Another Example of Switch Case Program:

switch( marks )
     {
        case 'A' : printf( "A++" );
                   break;

        case 'B' : printf( "A+" );
           break;

case 'C' : printf( "A" );
           break;

        case 'D' : printf( "B" );
           break;

case 'F' : printf( "C" );   
           break;

default  : printf( "YOU FAILED" );
                   break;
}


The keyword break must be included at the end of each case statement. The default clause is optional

We also have a continue statement which is used with switch case.

A continue statement causes a jump to the loop-continuation portion of the smallest enclosing iteration statement; that is, to the end of the loop body.

Example of Continue Statement:

#include <stdio.h>
#include <conio.h>
void main()

 {

int numb;
clrscr();
do

{

printf(" \nEnter any number :");
scanf("%d", &numb);
if (numb< 0) {
break;
}
if (numb >10) {
printf("Error\n");
continue;
}
printf("The number is: %d", numb);
} while (numb!= 0);

getch();
}

In the C program above, when user enters any number less than 10 it prints it. For e.g say 5 it prints The number is 5.

When number entered is greater than 10 say 12. Then its prints Error and again asks user to enter the number.

The continue statement prints Error and jumps to the do while loop without terminating the loop and user is again asked to Enter any number.

Difference between break and continue statements?

while (<expression>)
{
<statement>
if (<condition>)
break;
<statement>
}
// control jumps here on the break

------------------------------------------------------------------

while (<expression>) {
.
if (<condition>)
continue;
..
..
// control jumps here on the continue
}

 

COPYRIGHT 2009 ALL RIGHTS RESERVED FREECPROGRAMS.COM