To repeat a particular code block, loop is used in a programming language. In this chapter we learn to create a for loop in C programming.
The programming loop is used to repeat any code block until the condition is false.
How does the for loop work?
Initialization Statement is only executed once.
Then test Expression is executed. If false (0) the end of the for loop is over. But if the value of the test expression is true then the code block for the for loop is executed and the value of update Statement is updated. It continues until test Expression is false.
The value entered by the user is 100. The initial value of the variable count is assigned 1 and the test expression will be evaluated. Since count <= 100 (1 is smaller than 100) is true, so the code block of the loop will be executed and the sum of sum will be equal to 1.
Then updateStatement ++ count will be equal to count value of 2 count. Again testExpression will be executed. Since 100 is less than 2, so the value of testExpression will be true and the code block in the loop will be executed. The value of sum will be equal to 3.
This process will continue until the value of count is not reached 101 and the value of sum will be determined.
When count is equal to value 101 test Expression will be false because 101 is greater than 100. So the end of the loop here will be done and the next code will be edited. The sum at the end of the loop will be printed.
Finally,it shows that ,the output is 5050.
0 comments:
Post a Comment