C Programming language

Statement in C

In this section we are going to give Statement in C, Types of Statements with Examples.

Previous Home Next

Statement in C

Statement is a part of our program that can be executed. In other words every statement in our program alone or in combination specifies an action to performed by our program.

C provides variety of statements. One of the reason for popularity of C is because of the extreme power provided to programmer in C due to it rich and diverse set of statements define in C.

Types Of Statements

Statements in C are categorized into following types:

  1. Conditional Statement

    Its also called a selection statement. this statement depends on the condition. that decide the flow of statements based on evaluation of the results of condition. if - else and switch statements always come in conditional statements.

    if-else: if - else is a conditional statements. that always depend on condition. if statement have a some condition (true or false), that is given by the program .

    if the if condition will not be true than it will read the else statement and execute the else statement and go to the next line of the program for execution. if the if statement is true than it execute the if statement and it will not read the else statement and go to next line of the program for execution.

    General form of if-else statement

    
    if (condition) statement;
    else statement;
    OR
    if ( condition ) {
    expresion1;
    }
    else  {
    expresion2;
    }
    
    

    Example

    
    #include<stdio.h>
      int main(){
      int a,b;
         printf("Enter value for a :");
         scanf("%d",&a);
         printf("Enter value for b:");
         scanf("%d",&b);
       if ( a > b ){
          printf("a is large number - %d\n",a);
         }
       else{
        printf("b is large number - %d\n",b);
        }
    	return 0;
    }
    
    

    General representation of nested if in C

    
     if (condition1) statement1
    else
    if (condition2) statement2
    else
    .
    .
    .
    
    

    switch Statement

    It has a built in multiple - branch structure and work similar to if else ladder generally. The input value to to the switch statement construct is a int or char variable. Note that no float or any other data is allowed. This input variable is compared against a group of integer constant.

    All the statements in and after the case in which there is a match is executed until a break statement is encountered or end of switch is reached. We are also allowed to give a default case which will be executed if no other statement match is found.

    Representation of switch in C

    
    switch (input) {
     case constant1 :
      statement1;
      break;
     case constant2 :
      statement2;
      break;
    .
    .
    .
    case default :
      statement n+1;
      break;
    
    

    Example

    
    #include <stdio.h>
    int main (void)
    {
    char ch;
    printf ("\nEnter Character x,y or z: ");
    ch = getchar ();
    switch (ch)
     {
      case 'x' : printf ("You Entered x");
                 break;
      case 'y' : printf ("You Entered y");
                 break;
      case 'z' : printf ("You Entered z");
                 break;
      default : printf ("You Didnot Entered x, y or z");
      }
    return 0;
    }
    
    
  2. Iteration Statements

    These are used to run a particular block statements repeatedly or in other words form a loop. for, while and do-while statements come under this category.

    for Statement: for statement is one of the iteration statement.

    Loops generated by for statement is are also called entry controlled or close ended because the condition for iteration of loop is check at the starting of the loop and loop will not execute even once if the condition will not satisfy even once.

    General form of for statement is:

    
    for (initialization; condition, increment) 
      statement;
    
    
    1. Initialization: In this we initialize the loop control value to a starting value.

    2. Condition: In this evaluate the condition for the iteration of the loop and loop repeat only when this is true.

    3. Increment: In this we determine how the value of loop control value changes with each iteration of the loop. It can be increment, decrement or any mathematical expression.

    Example of for loop

    
    #include <stdio.h>
    int main (void)
    {
    int num;
    int i;
    for (i = 0; i<100;i++)
    printf ("\n%d",i);
    for (int j = 100; j>0;j--)
    printf ("\n%d",i);
    }
    
    

    While Statement

    while is an iteration statement . It is used to create loops. While produce entry controlled loops . loops in which condition check is performed at starting.

    form of while statement is:

    
    while (condition){
    statement }
    
    

    Example

    
    #include <stdio.h>
    int main (void)
    {
    int i = 0;
    while (i>10) 
     {
         printf ("\n%d", i); 
         // Loop prints values 0 to 9.
        i++;
     }     
    return 0;
    }
    
    

    do - while Statement

    do - while is also iteration statement. It is similar to while but has many different properties. It is an exit controlled loop which means that condition for next loop iteration is checked at bottom of the loop. the do-while loop will run at least once.

    form of do - while statement is:

    
    do {
         statement;  
       }
    while (condition);
    
    

    Example of do-while

    
    #include <stdio.h>
    int main (void)
    {
    int i = 0;
    while (i>10) 
     {
         printf ("\n%d", i); // Loop prints values 0 to 9.
        i++;
     }     
    return 0;
    }
    
    
  3. Jump Statements

    The are used to make the flow of your statements from one point to another. break, continue, goto and return come under this statements.

    return :return is a jump statement. Return statement has a special property that it can return a value with it to the calling function if the function is declared non - void. Void functions are the one which are explicitly declared not to return a value and hence a return statement with value when encountered in such a function leads to an error by compiler. Also a function declared non-void should always return a value of the respective type.

    form of return statement is:

    return expression;

    Example of return

    
    #include <stdio.h>
    int function (void)
    {
     int b;
     b = scanf ("%d", &b);
    return b; // returns the value of b to the calling function.
    }
    int main ()
    {
     printf ("\n%d", function ());
     return 0;
    }
    
    
  4. goto Statement

    goto statement is a jump statements .Goto is used for jumping from one point to another point in your function. You can not jump from one function to another. Jump points or way points for goto are marked by label statements. Label statement can be anywhere in the function above or below the goto statement.

    form of goto statement is

    
    .
    goto label1;
    .
    .
    label1 :
    .
    .
    label2 :
    .
    .
    goto label2;
    
    

    Example of goto

    
    #include <stdio.h>
    int main (void)// Print value 0 to 9
    {
    a = 1; 
    loop:;  // label stament
    printf ("\n%d",a);
    a++;
    if (a < 10) goto loop ; // jump statement   
    retrun 0;
    }
    
    
  5. break Statement

    break statement is a jump statement.Break statement when encountered within a loop immediately terminates the loop by passing condition check and execution transfer to the first statement after the loop. In case of switch it terminates the flow of control from one case to another.

    Example of break

    
    #include <stdio.h>
    int main ()
    {
     for (int i = 0; i<100; i++)
      {
        printf ("\n%d", i);
        if (i == 10);       
         // This code prints value only upto 10.
        break;
      }
     return 0;
    }
    
    
  6. continue Statement

    continue is a jump statement. It is analogues to break statement. Unlike break which breaks the loop, continue statement forces the next execution of loop bypassing any code in between. For for statements it causes the conditional check and increment of loop variable, for while and do-while it passes the control to the condition check jumping all the statements in between. Continue plays an important role in efficiently applying certain algorithms.

    Example of continue

    
    #include <stdio.h>
    int main ()
    {
     for (int i = 0; i<100; i++)
      {
        if (i == 10);
         // This code prints value only upto 9 even though loop executes 100 times.
         continue ;
        printf ("\n%d", i);
      }
    return 0;
    }
    
    
Previous Home Next