Discussion About C Language Structure

According to last post about syntactic structure of C Language structure, by this time let us discuss about routine structure. These expertise are really basic knowledge to learn before you engage in this industry or only the people are interested in programming. Well, routine structure normally has to refer to three parts such as judgement statement (selective structure), loop statement (loop structure) and goto statement (loop structure: loop or not loop).

Judgement statement (selective structure)

If statement: if…else;

Switch statement: switch…case;

 

  • Below, there is an example for this kinds of statement application. Please have a look,

#include <stdio.h>int main(){    int x = 20;    int y = 22;    if (x<y)    {        printf(“Variable x is less than y”);    }return 0;}

 

Output should be as below,

Variable x is less than y

 

Loop statement (loop structure)

While statement: while; Do…while;

For statement: for…

 

  • Let us to read about another example about Do…While statement such as Syntax of do-while loop.

#include <stdio.h>int main(){         int j=0;         do         {                 printf(“Value of variable j is: %d\n”, j);                 j++;         }while (j<=3);         return 0;}

 

Output should be as below,

Value of variable j is: 0Value of variable j is: 1Value of variable j is: 2Value of variable j is: 3

 

  • The next is about while statement below,

#include <stdio.h>int main(){    int i=0;    while(i==1)    {         printf(“while vs do-while”);    }printf(“Out of loop”);}

 

Output should be,

Out of loop

 

  • This is example about for statement below,

#include <stdio.h>int main(){   int i;   for (i=1; i<=3; i++)   {       printf(“%d\n”, i);   }   return 0;}

 

Output should be below,

123

 

Goto statement (loop structure: loop or not loop)

Goto statement: Break; continue; return;

 

  • Here we go, there is the example for goto statement below,

#include <stdio.h>int main(){   int sum=0;   for(int i = 0; i<=10; i++){         sum = sum+i;         if(i==5){            goto addition;         }   }   addition:   printf(“%d”, sum);    return 0;}

 

Output is about:

15

 

In this example, we write a label as addition, so when the value of i reach to 5, then according to goto statement it jumps to addition. So the final sum value would be 10+5 equals to 15.

 

Programming and Code Information.

Click Here for More Post.