Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +917541905230, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Tuesday, November 24, 2020

For loop in C

Loop: A loop is used for executing a block of statements repeatedly until a given condition returns false.

Loop in C | welcome2protec.com


A loop is used for executing a block of statements repeatedly until a given condition returns false.

There are three types of loop in C programming.

  1. for loop
  2. while loop
  3. do....while loop

In this tutorial, We will learn about for loop. Rest two loops(while and do...while loop) will learn in the next tutorial.



For loop in C:

For loop is the most commonly used looping technique. The reason why it is so popular is because it has all the three parts of the loop: initialization, test expression, and update expression in the same line.

Syntax

for (initializationStatement; testExpression; updateStatement)
{
    // statements to be executed repeatedly.
}

How does a for loop work in C?

Step 1: First initialization happens and the counter variable gets initialized.

Step 2: In the second step the test expression (condition) is checked, where the counter variable is tested for the given condition, if the condition returns true then the C statements inside the body of for loop gets executed.

Step 3: If the condition returns false then the for loop gets terminated and the control comes out of the loop.

Step 4: After successful execution of statements inside the body of loop, the counter variable is incremented or decremented, depending on the operation (++ or –). And again the test expression(condition) is checked.

This process is goes until the test expression(condition) is false. And when the condition is false, the loop will be terminated. Lets understand with a simple example at below.

To learn more about test expression or condition visit relational and logical operators.


Flow chart of for loop in C:

For loop | welcome2protec.com
For loop | welcome2protec.com

Example 1: Program to print number using for loop

// Print numbers from 1 to 10
#include <stdio.h>

int main() {
  int i;

  for (i = 1; i <= 10; ++i)
  {
    printf("%d ", i);
  }
  return 0;
}

Output

1 2 3 4 5 6 7 8 9 10

Explanation:

Here we have given the initial value of i as 1 (initialization). The test condition is i<=10 and the update expression is i++.

Let’s understand how this works. In the beginning, the control goes to the initial condition and assigns the value 1 to i. Now it checks whether 1<=10 which is true. Since the condition is true the control flows to the body of the loop and prints 1.

the control flows back to for again. Now, it updates the value of i (i=2) and checks the condition again whether 2<=10 which stand true again. So the control again flows to the body of the loop and prints 2 again. (Note that the control does not go back to the initialization expression in the next iteration).

Similarly, the process continues for values of i =3, 4,5,6,7,8,9, and 10. When i becomes equal to 11, the condition i<=10 becomes false and the loop terminates and the program ends.


Example 2: Program to calculate the sum of first n natural numbers using for loop

/* Program to calculate the sum of first n natural numbers.
* Positive integers 1,2,3...n are known as natural numbers. */

#include <stdio.h>
int main()
{
    int num, count, sum = 0;
    printf("Enter a positive integer: ");
    scanf("%d", &num);

    // for loop terminates when num is less than count
    for(count = 1; count <= num; ++count)
    {
        sum += count;
    }
    printf("Sum = %d", sum);
    return 0;
}

Output

Enter a positive integer: 10
Sum = 55

Different types of for loop in C:

I am using variable num as the counter variable in all the following examples.

1) Here instead of num++, I’m using num=num+1 which is same as num++.

Example 1: num++ (or num = num + 1)

/* Here instead of num++, I’m using num=num+1 which is same as num++. */
 for(num = 20; num <= 20; num=num+1)
 
 /* num++ or ++num can be written as num = num + 1 
  * num += count can be written as num = count + 1 */

2) Initialization part can be skipped as you can see at below example, since you can initialized it before the loop. But don't forget to write Semicolon (;) before condition else you will get compilation error.

Example 2: Initialization part can be skipped

/*Initialization part has been skipped and initialized it before the loop.*/
 num = 20;
for(; num<=20; mum++)
 

3) Like initialization, you can also skip the increment part as you can see at below example. In this case semicolon (;) is must after condition (test expression). In this case the increment or decrement part is written inside the loop.

Example 3: Like initialization, increment part can be skipped too

for(num=10; num<=10;)
{
    //statements
    num++;
}

4) This is also possible. The counter variable is initialized before the loop and incremented inside the loop.

Example 4:

int num=10;
for(; num <= 10;)
{
  //statements
   num++;
}

5) In C, multiple variables can be initialized in the for loop. Let's take a simple example to understand it

Example: Multiple variables can be initialized

/*Multiple variable initialization*/
for(i = 0, j = 1; i <= 5 && j <=5; i++, j++;)










Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec

Help others by sharing this page.

Ahmad Irshad

Author & Editor

I love blogging, teaching, learning computer science and sharing it to others. I've written and develped this site so that students may learn computer science related tutorials eaisly. MCA / MCITP

0 Comments:

Post a Comment

Please don't enter any spam link in the comment box.


Protec Computer Academy (An ISO 9001:2015 Certified Institute, Powered by E-Max Education, Branch Code EMAX/EK-80503, Registered by government of India.) is a best IT training center in Siwan with 100% Job placement assistance. Where you can learn Programming, WebDesigning, Hardware|Networking, Blogging, WordPress, Digitial marketing, English Speaking, And many more...| All certificates are valid in Government Jobs as well as in Private Companies. *** At Tara Market, Beside Vishal Mega Mart - Siwan*** +966532621401, Email- ahmad.irshad781@gmail.com *** Follow us on | | @welcome2protec
Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec Protec
Contact Us