C Programming -Lecture 6 : While Loop
If we want to have so many times with the same computer, then loop (cycle?) Use. There are several types of loop language such as C - while, for, do-while and so on. Today, we will discuss the while loop.
We can say without seeing the name of the computer understand "until" the conditions to be met "unless" This will be the job.
It is solely a look at how it is written
While Loop Syntax
First, we look at the structure of the loop
while ([expression])
{
[Statement Block]
}
There is a Boolean expression that will decide whether or not to execute the loop statement block statement. At the beginning of the loop, the computer will find the value of expressions. If the statement is True, then the block will be executed. Statement block the computer again and then execute the new expression will be out. True again, then again, if the value of the block of statements to be executed. Thus, as long as the expression must be True to run the loop or cycle. False when the expression value of the loop when the loop will stop and the computer will then have to deal with it.
While loop example
Now therefore see an example of the loop
1. int num;
![]() |
| Example of while |
II. num = 0;
3.
4. while (num <4)
5. {
6. printf ( "% d", num);
7. num = num + 1;
8. }
9.
10.
Here we are num variable line between the two numbers 0 am. The loop has the number 4 line. Lupati we will see how some of these steps.
Step # 1
0 4 on the number line, so num variables (num <4) expression values ??True. Therefore, lines 5-8 will be
executed. 5, the number of blocks on the loop
line number 6, we see the output of
0. 7, No. 1 from the zero line, the value of num num = 1 will be
increased. 8, No. 4, the number of computers in the last line of the while loop block will be back on line.
Step II
4 No. 1 line, so num variables (num <4) expression values ??True. Therefore, lines 5-8 will be executed.
6 No. 1 line, we see the
output. 7, No. 1 line, the value of num num = 2 will be increased to
1. 8, the number of blocks on the computer at the end of the while loop will be back on line number 4.
Step 3:
4 No. 2 line, so num variables (num <4) expression values ??True. Therefore, lines 5-8 will be executed.
6 No. 2 line, we see the
output. 7, No. 2 on the value of num num = 3 will be increased to
1. 8, the number of blocks on the computer at the end of the while loop will be back on line number 4.
Step # 4:
3, number 4 line, so num variables (num <4) expression values ??True. Therefore, lines 5-8 will be executed.
3, the output line number 6, we shall
see. 7, No. 3 line 1 to increase the value of num num = 4
in. To 8 numbers you see on the computer while loop at the end of the block will return to the No. 4 line.
Step 5
4 No. 4 line, so num variables (num <4) expression value False. So another 5-8 lines will not be executed.
Computers will be moved to No. 9 on the next actions (if any) and to the end of the loop.
While example tracing / debugging
In this video, we are at the top of the steps on the computer Visual Studio debugging is how I see it. You can debug ekhaibhabe using any other IDE. If you wish Visual C ++ 2010 Express can be downloaded for free from the Microsoft website (search if the link).
Infinite Loop
We have seen the end of the loop while loop when the expression value is False. Lichi in such a way that the value of expression, but we do not ever have to False, then loop while asalai not ever stop. In the above example, we have seen as a result of having to change the num variable expression of the value of the loop when the loop stops False. But if we like the likhi
1. int num;
II. num = 0;
3.
4. while (num <4)
5. {
6. printf ( "% d", num);
7.
8. }
9.
10.
There is no chance of changing the num variable now and lupao not stop. It's a simple example. But sometimes it can cause due to a bug in the program. Or you may need a lot of time to do so.

No comments: