Friday, 19 January 2018

What are Loop Structures?

Welcome back! In my last post, I explain about functions and Types of functions. After that you should know about Loop structures also known as Statement that is used for making long programs into simple and short programs. So let's start.....

C has three loop constructs:
1) For loop
2) While loop
3) Do-while loop
The first two are pre-test loops and do-while is a post-test loop. In the post_test loop, the code is always executed once.

1) For Loop: Allow a set of instructions to be repeatedly executed until a certain condition reached. This condition may be predetermined or open ended.

Syntax:
           for(initialization; condition; increment)
           {
             statement;
           }
Here is a example program, you can try to write this yourself;
Program: For generation of multiplication table as for demonstration.
   #include<stdio.h>
   #include<conio.h>
        void main()
     {
          int row, column;
          puts("\t\t My Handy multiplication table");
          for(row=1; row<=10; row++)
                {
                  for(column=1; column<=10; column++)
                  printf("%6d", row*column);
                  putchar("\n");
                }
           getch();
     }

2) While Construct: While loop check the test condition at the top of the loop, which means that the body of the loop will not execute if the condition is false to begin with. This feature may eliminate the need to perform a separate conditional test before the loop.

Syntax:
              While(condition)
            {
               statement;
            }

3) Do-while construct: Unlike For loop and While loop, which test the loop condition at the top of the loop, the do-while loop checks its condition at the bottem of the loop. This means that a do-while loop always executes atleast once. The general form of the do-while loop is given below:

Syntax:
              do
             {
                statement;
             }
               while(condition);


0 comments:

Post a Comment