Loops in C Part I


Looping constructs are used to repeat a section of code a number of times, depending on some condition being either true or false in other words Loops are basically means to do a task multiple times, without actually coding all statements over and over again. This section will discuss the use of control statements in C. We will show how they can be used to write powerful programs by;

Loops in C mainly of three types: –
 ‘while’ loop
‘do while’ loop
‘for’ loop

While loop

Looping doesn’t get much simpler than while loop. A while statement is like a repeating if statement. Like an If statement, if the test condition is true: the statements get executed. The difference is that after the statements have been executed, the test condition is checked again. If it is still true the statements get executed again. This cycle repeats until the test condition evaluates to false.

The most important thing to know with while loops are that the control expression is evaluated before the statement is run. If the expression is false, the statement is not run. So a while loop may never execute a statement.

Syntax for while loop

while(condition)

{
               statements;
               increment/decrement;
}

Properties of while loop:

  •  Task of the expression is to check the condition. Loop will execute until condition is true otherwise loop will terminate.
  •  If any expression returns zero then condition will false and if it returns any non- zero number then condition will true.

Example:

#include <stdio.h>

#include <conio.h>

Int main()

{
               int x;

               x = 1;

               while(x<=5)

               {
                               printf("\n I am learning while loop");

                               x+=1      //incrementing expression variable
               }

               return 0;
}

 

Do While Loop

In some of the places you need to execute block of statements at least once before you evaluate the condition. The do while loop executes the content of the loop once before checking the condition of the while.

The main difference between Do while loop and while loop is that do while loop will be executed at least once, where as while loop will check the condition first and then it may or may not get executed depending on the condition.

Syntax

do

{

   Single statement

   or

   Block of statements;

}while(expression);

 

In above syntax, the first the block of statements are executed, after that while statement is executed. If the resultant condition is true then program control goes and evaluates the body of a loop once again. This process continues till condition becomes true. When it becomes false, then the loop terminates.

Example:

#include <stdio.h>

int main(){

    int k = 15;

    int i = 0;

    // using do while loop statement

    do{

        i++;

        printf("%d\n",i);

    }while(i < k);

               return 0;

}
Please read second and third part of this tutorial for more control statements in C

 

Related posts:

About author

This article was written by admin

Admin has over twenty years experience in the electronics industry, largely dedicated to embedded software. A frequent presenter at conferences and seminars and author of numerous technical articles. Working presently as Development Manager in India. A firm Believer in Knowledge grows when it shared.

Comments

Comments (2)
  1. Gennie Homans says - Posted: October 24, 2012

    I found your weblog website on google and test a few of your early posts. Proceed to maintain up the very good operate. I just further up your RSS feed to my MSN News Reader. Looking for forward to studying more from you in a while!…

  2. Blondell Rushworth says - Posted: October 25, 2012

    Good web site! I really love how it is simple on my eyes and zthe data are well written. I’m wondering how I could be notified whenever a new post has been made. I have subscribed to your RSS feed which must do the trick! Have a nice day!

Leave your comment

Your email address will not be published. Required fields are marked *