Society & Lifestyle
|Computing||Share This Page|
Decision Making And Looping
|by Sachin Mehta|
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:
The C language provides for three loop constructs for performing loop operations. They are:
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)
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.
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.
|More by : Sachin Mehta|
|Views: 5488 Comments: 0|
|Top | Computing|