Pages

Monday, July 15, 2013

C language II (do while loop, for loop, if statement, loops, switch statement, while loop )


IF STATEMENT
If statement is used to apply any condition to the program which means that if the inputted condition is true with the given condition then program will proceed with the same condition otherwise program will skipped.
Syntax :

if ( condition )
     statement ;
IF-ELSE STATEMENT
This statement is used to apply more than one condition to the program which means that if inputted value is true for condition than program will proceed with same condition or if condition is not true than program will follow the else condition.
Syntax :
if ( condition )
  statement;
else
  statement;    

NESTED IF-ELSE STATEMENT
This statement contains more than one if statement with corresponding condition at each statement with it.
Syntax :
if ( condition 1 )
  {
     statement 1;
  }
else
  if ( condition 2 )
    if (condition 3 )
     {
        statement 2;
     }
    else
      statement 3;
  else
   statement 4;

SWITCH STATEMENT
Switch statement is used to proceed program with a block of statement depending on value of a variable or an expression.
Syntax :
switch (expression )
{
  case (value 1) : statement 1
      break;
  case (value 2) : statement 2
     break;
  case (value 3) : statement 3
     break;
……………………………….
……………………………….
   case (value n) : statement n
     break;
  default:   default statement
     break;
}

• break : Used to transfer the control to the end of switch statement if any case is matched with variable.

• goto : This statement is used to transfer the control from one part of program to another part of program.

Syntax:



……………..

goto level;
……………..
……………..
……………..
……………..
          level :
 statement;

 level :
 statement;
……………..
……………..
……………..
……………..
goto level;
……………..

LOOP CONTROL STRUCTURE

Loop control structures are used to execute and repeat a block of statements depending on the value of a condition.There are three types of loop control statements in C language

1. FOR LOOP : For loop is used to execute and repeat a block of statements depending on a given condition. It is written as:
for( initial value ; condition ; increment )
{
    statement
}
• initial value = starting value
• condition = last or maximum value
• increment = change in value which is added every time to initial value
Ex :  for(i=1;i<=20;i++)  , where i is any integer and i++ is increment by 1.
NESTED FOR LOOP : In Nested for loop “statement” lies completely inside another block of for loop.
Ex :
for(i=1;i<=3;i++)
{
  for(j=1;j<=5;j++)
    {
      statement
     }
}                                                      ( here i and j are integers)

2. WHILE LOOP : A while  loop is used to execute and repeat a block of statements depending on given condition. It is written as:

  while( condition )
   {
       statement
    }
Ex: while (i<=20)                                           (here i is integer )
      {
       printf( “Value is correct”);
      }

3. DO WHILE LOOP :A do-while statement is used to execute and repeat a block of statements depending on a condition. It is written as:

do
{
   statement
}
while (condition);


Ex:     i=1;
          do
           {
     i=i+1;
       }
           while(i<=20);

No comments:

Post a Comment