Decision Making And Looping by Sachin Mehta SignUp
Boloji.com

Channels

In Focus

 
Analysis
Cartoons
Education
Environment
Opinion
Photo Essays
 
 

Columns

 
A Bystander's Diary
Business
Random Thoughts
 
 

Our Heritage

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

Society & Lifestyle

 
Health
Parenting
Perspective
Recipes
Society
Teens
Women
 
 

Creative Writings

 
Book Reviews
Computing
Ghalib's Corner
Humor
Individuality
Literary Shelf
Love Letters
Memoirs
Quotes
Stories
Travelogues
Workshop
 
 
Computing Share This Page
Decision Making And Looping
by Sachin Mehta Bookmark and Share

In looping, a sequence of statements are executed until some condition is satisfied which is placed for termination of the loop. A program loop consists of two segments, one is the body of the loop and the other known as the control statement. The control is tested always for execution of the body of the loop. Depending on the position of the control statement in the loop, a control may be classified as the entry-controlled loop or as the exit-controlled loop. In the entry-controlled loop, first the conditions are tested and if satisfied then only  body of loop is executed. In the exit-controlled, the test is made at the end of the body, so the body is executed unconditionally first time.

A looping process, in general, would involve the following four steps:
  1. Setting and initialization of a counter.
  2. Execution of the statements in the loop.
  3. Test for a specified condition for execution of the loop.
  4. Incrementing the counter.

The C language provides for three loop constructs for performing loop operations. They are:

  1. The while statement.
  2. The do statement.
  3. The for statement.

Loops find application in almost all C/C++ programs that you're likely to encounter. So I'll deal this topic in detail. So it's spread over two articles.

The while Statement

This is one of the most simplest looping structure. The basic format of the while statement is

while (test condition)
{
   body of the loop

The while is an entry-controlled loop statement. The test condition is evaluated and only if the condition is true the body is executed. After execution of the body, the test-condition is once again evaluated and if it is true, the body is executed once again. This process of repeated execution of the body continues until the test-condition finally becomes false and the control is transferred  out of the loop. On exit, the program continues with the statement immediately after the body of the loop. If the body contains only one statement its not necessary to put the braces, but placing them is a good programming practice.

Example:
........
.......
x = 10;                    -------------   Initialization
while (x < 16)          -------------   Test condition
{
  printf("%d",x);        -------------   body of the loop
  x = x+1;
}   
........

The above example shows a simple position where the test condition is evaluated first and since the test is satisfied the body is executed. This will continue exactly six times printing out 10 to 15. Finally when x becomes 16 then test fails & so the loop ends. The program continues with the statements after the loop.

The next article will deal with do-while and for loops. So do join in next time.

Share This:
08-Feb-2001
More by :  Sachin Mehta
 
Views: 4536      Comments: 0




Name *
Email ID
 (will not be published)
Comment
Characters
Verification Code*
Can't read? Reload
Please fill the above code for verification.
 
Top | Computing



 
 
 
 
 
 
 
2018 All Rights Reserved
 
No part of this Internet site may be reproduced without prior written permission of the copyright holder
.