Skip to main content

Posts

examples of do while loop in c

/* /* do while loop In do while loops also the loop execution is terminated on the basis of test condition. The main difference between do while loop and while loop is in do while  loop the condition is tested at the end of loop body, i.e do while loop is exit controlled whereas the  other two loops are entry controlled loops. Note:- In do while loop the loop body will execute  at least once irrespective of test condition. Syntax: initialization expression; do {    // statements    update_expression; }  while (test_expression); */ // C program to illustrate do-whil...

examples of while loop in c

/* While Loop While studying for loop we have seen that the number of  iterations is known beforehand, i.e. the number of times  the loop body is needed to be executed is known to us.  while loops are used in situations where we do not know  the exact number of iterations of loop beforehand.   The loop execution is terminated on the basis of test condition. Syntax: We have already stated that a loop is mainly consisted of three  statements – initialization expression, test expression, update expression.  The syntax of the three loops – For,...

examples of for loop

/* /// For loop A for loop is a repetition control structure which allows us to  write a loop that is executed a specific number of times.  The loop enables us to perform n number of steps together in one line. Syntax: for (initialization expression; test expression; update expression) {          // body of the loop      // statements we want to execute } In for loop, a loop variable is used to control the loop.  First initialize this loop variable to some value,   then check whether this variable is less than or...

loops in c

/* 1.In computer programming, a loop is a sequence of  instructions that is repeated until a certain condition is reached. 2.An operation is done, such as getting an item of data and  changing it, and then some condition is checked such as whether   a counter has reached a prescribed number. 3.Counter not Reached: If the counter has not reached the  desired number, the next instruction in the sequence returns  to the first instruction in the sequence and repeat it. 4.Counter reached: If the condition has been reached,  the next instruction “falls through”...

examples of Switch Statement in C

// Following is a simple C program  // to demonstrate syntax of switch.  /* Syntax: switch (n) {     case 1: // code to be executed if n = 1;         break;     case 2: // code to be executed if n = 2;         break;     default: // code to be executed if n doesn't match any cases } */ #include   <stdio.h>   int   main ()  {  int   x  ; printf ( "enter the number \n " ); scanf ( "%d" ,& x ); switch  ( x )  {      case   1 :  printf ( "Choice is 1" );            break ;...

another example of if-else-if ladder in C

#include <stdio.h> int   main () {     int   age ;     printf ( "Enter your age \n " );     scanf ( "%d" ,& age );     if  ( age >= 18 )    {        printf ( "you can vote" );    }     else   if  ( age < 14 )    {        printf ( "you can vote for kids" );    }     else    {        printf ( "you can,t vote sorry!" );    }             return   0 ; } //we can also do like this #include <stdio.h> int   main () {     int   age ;     printf ( "Enter your age \n " );     scanf ( "%d" ,&...

example of if-else-if ladder in C

// example of if-else-if ladder in C #include   <stdio.h>   int   main () {      int   i  =  20 ;      if  ( i  ==  10 )         printf ( "i is 10" );      else   if  ( i  ==  15 )         printf ( "i is 15" );      else   if  ( i  ==  20 )         printf ( "i is 20" );      else        printf ( "i is not present" );  }