C Program to Find Total of Even Integers

C for loop with Example

The most interesting and most difficult of all loops is the C for loop. The name for loop comes from the typical description of classic for loop: For all values of variable from value1 to value2 in steps of value3, repeat the following sequence of commands.

You typically use the for loop to execute a block of statements a given number of times. Let’s suppose you want to display the numbers from 1 to 10. Instead of writing ten statements that call printf(), you could write this:

for(int count = 1 ; count <= 10 ; ++count)
{
printf(" %d", count);
}

The C for loop operation is controlled by what appears between the parentheses that follow the keyword for. The execution that you want to repeat each time the loop repeats is the block containing the statement that calls printf(). Because you have just a single statement here, you could avoid and above example shows the three control expressions that are separated by semicolons. These expressions control the operation of the loop.


The syntax of for C for Loop statement is given by for statement: = for (expr ; expr ; expr) statement

Example,

for (i = 1; i <= n; ++i)
factorial ~'= i;

for (j = 2; k % j == 0; ++j) {
}

printf(lf%d is a divisor of %d\nlf, j, k);
sum += j;
}

The first control expression, int count = 1, is executed only once, when the loop starts. For example, this creates variable, count, with the initial value 1. This variable is local to the loop and does not exist outside the loop.

The second control expression must belogical expression that ultimately can result in true or false. In this case, it’s the expression count <= 10, which will evaluate to true as long as count is not greater than ten. The second expression is evaluated at the beginning of each loop iteration. If the expression evaluates to true, the loop continues, and if it’s false, the loop ends and execution of the program continues with first statement following the loop block or loop statement.

The third control expression, ++count in this case, is executed at the end of each loop iteration. Here you use the increment operator to add 1 to the value of count. On the first iteration, count will be 1, so the printf() will output 1. On the second iteration, count will have been incremented to 2, so the printf() will output the value 2. This will continue until the value 10 has been displayed.

C for loop with example

This example program prints out all the primes numbers between 1 and the macro value maxint. Prime numbers are numbers which cannot be divided by any number except 1 without leaving a remainder. This example explain how C For loop works with program,

/************************************************/
/* Prime Number Generator #1 */
/************************************************/

#include <stdio.h>
#define MAXINT 500
#define TRUE 1
#define FALSE 0
/*************************************************/
/* Level 0 */
/*************************************************/
main ()
{ int i;
for (i = 2; i <= MAXINT; i++)
{
if (prime(i))
{
printf ("%5d",i);
}
}
}
/*************************************************/
/* Level 1 */
/*************************************************/
prime (i) /* check for a prime number */
int i;
{ int j;
for (j = 2; j <= i/2; j++)
{
if (i % j == 0)
{
return FALSE;
}
}
return TRUE;
}

 

Online Training Tutorials

  • One time vendor – Posting FB60One time vendor - Posting FB60 Setting Profile option  (This setting is only for the first time you are using one time / small dollar vendor) A. Go to FB60 B. Go to Editing […]
  • Print Product of Two MatricesC Program to Print Product of Two MatricesFollowing C Program to Print Product of Two Matrices, then display the result on the screen: The simplest form of multidimensional array is the two-dimensional array. A two-dimensional […]
  • SAP IDESWhat is SAP IDES?SAP IDES in the R/3 System represents a model company. It consists of an international group with subsidiaries in several countries. SAP IDES contains application data for diverse business […]
  • Search engine optimizationSEO -10 Most Common SEO Mistakes to AvoidCommon SEO mistakes : In recent years a number of illicit techniques have grown up to artificially manipulate a web site’s ranking. These techniques are referred to as spamming or […]
  • sap pp tutorialSAP PP Tables ( Production Planning ) MobuleSAP PP ( Production Planning ) is one the biggest functional module in SAP. This SAP PP module mainly deals with production process includes capacity planning, Master production […]
  • SAP Business One More than 70,000 Customers TrustSAP Business One More than 70,000 Customers TrustBy exceeding the 70,000 client milestone, SAP Business One has achieved a significant milestone. These figures alone demonstrate how well regarded and enterprise-ready SAP Business One is […]
  • SAP Password for DatabaseChanging SAP Password for Database with BRCONNECTChanging SAP Password for Database To Changing SAP Password for Database Users Using BRCONNECT As of Release 6.10, you can use BRCONNECT to change the passwords for the database […]
  • what is sapWhat is Cross Client Concept in SAP?Cross Client is defined as a self-contained commercial, organizational, and technical unit within an SAP System. This means that all business data within a client is protected from other […]