Skip to main content

C Programming for Loop

C Programming for Loop

Loops are used in programming to repeat a specific block of code. After reading this tutorial, you will learn to create for loop in C programming.
Loops are used in programming to repeat a block of code until a specific condition is met. There are three loops in C programming:
for loop

for Loop

 The syntax of for loop is:
for (initializationStatement; testExpression; updateStatement)
{
       // codes 
}

How for loop works?

The initialization statement is executed only once.
Then, the test expression is evaluated. If the test expression is false (0), for loop is terminated. But if the test expression is true (nonzero), codes inside the body of for loop is executed and the update expression is updated.
This process repeats until the test expression is false.
The for loop is commonly used when the number of iterations is known.
To learn more on test expression (when test .expression is evaluated to nonzero (true) and 0 (false)), check out relational and logical operators

for loop Flowchart


Example: for loop

  1. // Program to calculate the sum of first n natural numbers
  2. // Positive integers 1,2,3...n are known as natural numbers
  3. #include <stdio.h>
  4. int main()
  5. {
  6. int num, count, sum = 0;
  7. printf("Enter a positive integer: ");
  8. scanf("%d", &num);
  9. // for loop terminates when n is less than count
  10. for(count = 1; count <= num; ++count)
  11. {
  12. sum += count;
  13. }
  14. printf("Sum = %d", sum);
  15. return 0;
  16. }
Output
Enter a positive integer: 10
Sum = 55
The value entered by the user is stored in variable num. Suppose, the user entered 10.
The count is initialized to 1 and the test expression is evaluated. Since, the test expression count <= num (1 less than or equal to 10) is true, the body of for loop is executed and the value of sum will equal to 1.
Then, the update statement ++count is executed and count will equal to 2. Again, the test expression is evaluated. Since, 2 is also less than 10, the test expression is evaluated to true and the body of for loop is executed. Now, the sum will equal 3.
This process goes on and the sum is calculated until the count reaches 11.
When the count is 11,  the test expression is evaluated to 0 (false) as 11 is not less than or equal to 10. Therefore, the loop terminates and next, the total sum is printed

Comments

Popular posts from this blog

short key of c programming

short key of c programming Shortcut Keys(Hot Keys)     Operation    Alt + F  -            Open file Alt + F3 -           Close Alt + F + N  -    New file Alt + X  -           Exit turbo c Alt + F5 -          Output Screen Alt + F + L  -    Check programme load or not. Alt + ENTER  - Full screen Shift + Del -       Cut Shift + Insert -   Paste Ctr + Insert -    Copy Alt+F9 -           Compile a program Ctr + F8           Bbreak point Ctrl+F9 -          To run a program Ctrl+s -...

Unordered List, Nested lists, Description List,TUTE-8

                                                             Unordered List An unordered list can be created with the <ul> tag and each list item can be created with the <li> tag as shown by the example below: <ul>   <li>Item</li>   <li>Another Item</li>   <li>Yet Another Item</li>  </ul> This will produce a bulleted list (which is the default style): You should use ul to display a list of items, where the order of the items is not important. If changing the order of the items makes the list incorrect, you should use <ol>.   Nested lists You can nest lists to represent sub-items of a list item. <ul>   <li>item 1</li>   <li>item 2     <ul>    ...