Home | Hindi | Kabir | Poetry | Workshop | BoloKids | Writers | Contribute | Search | Contact | Share This Page!                      Shop Online

  News
Channels
In Focus

Analysis  
Bolography  
Cartoons
Environment   
Opinion 

Columns
 Business
 My Word 
 PlainSpeak 
 Random Thoughts 
Our Heritage

Architecture
Astrology
Ayurveda
Buddhism
Cinema 
Culture
Dances
Festivals
Hinduism
History  
People  
Places 
Sikhism
Spirituality 
Vastu 
Vithika  

Society & Lifestyle

Family Matters 
Health
Parenting
Perspective 
Recipes
Society
Teens 
Women 

Creative Writings

Book Reviews
Ghalib's Corner
Humor
Individuality
Jagoji
Literary Shelf 
Love Letters  
Memoirs
Musings
Ramblings
Stories
Travelogues

Computing
  General Articles
 
CC++ 
  Flash 
  Internet Security 
 
Java 
 
Linux     
  Networking  

Computing | CC++   
The DO Statement

Last time, we embarked on a journey of loops. So let's continue from where we left.

The while statement discussed is an entry-controlled loop structure, in which the loop will not be executed if the test condition comes out to be false. But in some situations it might be necessary to execute the loop, even if the test-condition fails. So this kind of loops are called the exit-controlled loop structure. This takes the form:

do
{
   body of the loop
}
while (test condition);

Example: 

do
{
   ch = getchar();
   printf("The character entered is %c",ch);
}
while (ch != 'n')

ch is assumed to be a variable of type character. Now the loop is executed first under no test and the input by the user is outputted. Then the entered value would be tested by the condition for further advancement.

The FOR Statement:

The for loop is an entry-controlled loop that provides a more concise loop control structure. The general form of the for loop is:

for (initialization; test-condition ; increment)
{
  body of the loop
}

The execution of the for statement is as follows:

  1. Initialization of the control variables is done first, using assignment statements 
    such as i = 1 and count = 0.  The variables i and count are known as the loop control variables.
       
  2. The value of the control variable is tested using the test-condition. The test-condition is a relational expression, such as i<10 that determines when the loop will exit. If the condition is true, the body of the loop is executed; otherwise the loop is terminated and the execution continues with the statement that immediately follows the loop.
       
  3. When the body of the loop is executed, the control is transferred  back to the for statement after evaluating the last statement in the loop. Now , the control variable is incremented using an assignment statement such as i = i+1 and the new value of the control variable is again tested to see whether it satisfies the loop condition. If the condition is satisfied, the body of the loop is again executed. This process continues till the value of the control variable fails to satisfy the test-condition.    

Example:

for( i = 0; i<10; i = i+1)
{
   printf("%d",i);
}

The for loop will be executed 10 times. Each time a value equal to the current value of i will be printed out. 

Additional Features of for Loop

1. More than one variable can be initialized at a time in the for statement. For example:

for (p=1,n=0; n<18; ++n)  

2. Like the initialization section, the increment section may also have more than one part. 
    For example,

for (a=2,b=30; n <= m; n=n+1,m=m-1)                   
{
p = b/a;
printf("%d ",p);
}

3. It is also permissible to use expressions in the assignment statements of initialization and increment sections. For example 

for (w = (a+b); w>0; w=w/2) is valid.

4. One or more sections in the for statement can be omitted, if necessary. For example

e = 2;
for (; e != 10 ;)
{
   printf("%d",e);
   e = e + 2;
}

Both the initialization and increment sections are omitted in the for statement. In such cases, the sections are left blank. However, the semicolons separating the sections must remain. If the test-condition is not present, the for statement sets up an infinite loop.    

– Sachin Mehta
February 15, 2001

C/C ++        
Managing Input-Output Operations
     
Managing Output Operations
    
Decision Making And Branching
     
Decision Making and Branching (if statement)
     
Decision Making and Looping
    
The Do Statement
   
Arrays
    
Arrays - Multi-Dimensional
    
Handling of Character Strings
    
Handling of Character Strings - 2  
User Defined Functions   
   User Defined Functions...contd    
Handling of Functions  

Computing
Flash | Internet Security 
Java | Linux | Networking  

General Articles  

Top | Previous | Next   
 


 

Recommend This Page!

Analysis | Architecture | Astrology | Ayurveda | Book Reviews | Buddhism | Cartoons | Cinema | Computing | Culture | Dances
Environment | Fables | Family Matters | Festivals | Hinduism | Health | History | Home Remedies | Humor | Individuality | Jagoji
Literary Shelf | Memoirs | Musings | Opinion | Parenting | Perspective | Photo Essays | Places | Ramblings
Random Thoughts | Recipes | Sikhism | Society | Spirituality | Stories | Teens | Travelogues | Vastu | Vithika | Women

Home | Hindi | Bolography | BoloKids | Kabir | Poetry | Quotes | Workshop | Writers | Contribute | Search | Contact


Boloji.com is owned and managed by Boloji Media Inc

Privacy Policy | Disclaimer
No part of this Internet site may be reproduced without prior written permission of the copyright holder.