Friday 20, July 2018
Welcome Guest, Register | Login  
      Home       Tutorials       Free Ebooks       Free Scripts       Tech News       Articles       About Us       Blog        Forum

Loops

Loops are used to repeat one statement or set statements more than one time. Most real programs contain some construct that loops within the program, performing repetitive actions on a stream of data or a region of memory. There are several ways to loop in C.

 

For Loop
For loop is a counter loop. The for loop allows automatic initialization of instrumentation of a counter variable. The general form is

 

for (initialization; condition; increment/decrement)
{
    statements block
}

 

If the statement block is only one statement, the braces are not necessary. Although the for allows a number of variations, generally the initialization is used to set a counter variable to its starting value. The condition is generally a relational statement that checks the counter variable against a termination value, and the increment increments (or decrements) the counter value. The loop repeats until the condition becomes false.

 

Example
Main(){
int i;

 

        for(i = 0; i < count; i++)
            {         
                printf(“%d\n”,i);
            }
}

 


While
Loop

The while loop repeats a statement until the test at the top proves false.

The while loop has the general form:
while(condition)
{
    statement block
}

 

The while tests its condition at the top of the loops. Therefore, if the condition is false to begin with, the loop will not execute at all. The condition may be any expression. An example of a while follows. It reads characters until end-of-file is encountered.

Example

Main(){

int t = 0;

while(t<=10)
{
            printf(“%d\n”,t);

t=t+1;

 }

}

 


do-while loop

This is very similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The test then verifies the data, and loops back to read again if it was unacceptable.

void 

main(void){
int val;
do
{       printf("Enter 1 to continue and 0 to exit :");
        scanf("%d\n", &val);
} while (val!= 1 && val!= 0)

}

 


 
 
C Language Tutorial
 
  Introduction
Introduction
  First Program
First Program
  Constants and Variables
Constants and Variables
  Loops
Loops
  Control Structure
Control Structure
  Functions
Functions
  Pointers
Pointers
  Arrays
Arrays
  Strings
Strings
  Structures and Unions
Structures and Unions
  File Handling
File Handling
  Graphics
Graphics
 
 
 
Web Designing Tutorials
  HTML Tutorial
HTML Tutorial
  DHTML Tutorial
DHTML  Tutorial
  CSS Tutorial
CSS Tutorial
  XHTML Tutorial
XHTML Tutorial
 
Programming Languages Tutorials
  C Language Tutorial
C Language Tutorial
  C++ Tutorial
C++ Tutorial
  Java Language Tutorial
Java Language Tutorial
  Data Structure Theory Tutorial
Data Structure Theory Tutorial
 
Server Side Scripting Tutorials
  PHP Tutorial
PHP Tutorial
  SQL Tutorial
SQL Tutorial
  ASP Tutorial
ASP Tutorial
 
Client Side Scripting Tutorials
  JavaScript Tutorial
JavaScript Tutorial
  VBScript Tutorial
VBScript Tutorial
 
 
 
POPULAR E-BOOKS
 
Download Marketing With Excellence Ebook Marketing With Excellence
   
Download Creating Applications with Mozilla  Ebook Creating Applications with Mozilla
   
Download How to be a Programmer: A Short, Comprehensive, and Personal Summary  Ebook How to be a Programmer: A Short, Comprehensive, and Personal Summary
   
Download Viral Marketing Secrets Ebook Viral Marketing Secrets
   
Download 5 Minutes Forex Trading Strategy Ebook 5 Minutes Forex Trading Strategy
   
Studiesinn.com 2018 All Rights Reserved.