C - Do calculation within the third control expression in for loop

Description

Do calculation within the third control expression in for loop

Demo

#include <stdio.h>

int main(void)
{
      unsigned  long long sum = 0LL;            // Stores the sum of the integers
      unsigned int count = 0;                   // The number of integers to be summed

      // Read the number of integers to be summed
      printf("\nEnter the number of integers you want to sum: ");
      scanf(" %u", &count);

      // Sum integers from 1 to count
      for(unsigned int i = 1 ; i <= count ; sum += i++);

      printf("\nTotal of the first %u numbers is %llu\n", count, sum);
      return 0;// w  w  w  .j a v a 2  s.co m
}

Result

how It Works

We 've placed the operation that accumulates the sum in the third control expression for the loop:

for(unsigned int i = 1 ; i<= count ; sum += i++);

The loop statement is empty.

Related Example