For loop in C or C++

For loop in C or C++

for loop in C or C++

Loops are used to repeat some steps until the desired condition is satisfied. Loops basically execute certain piece of code which programmer does not want to write again and again. For e.g. if I want print numbers from 1 to 100, I have to declare 100 variables assign values to them and print them. It will definitely make my program large and will consume a lot of memory. Thus to avoid this problems looping structure provides easiest way by re-executing everything written inside it. For loop in C or C++ is also one kind of looping structure. It comes under branching looping structure. It’s general syntax is

Syntax -
for(initialization; setting of a condition; increment/decrement)
{
    task1;
    task2;
    task3;
}

Let’s see the code which will print 1 to 100 numbers using for loop.

1. Code

C program

//to print nos from 1 to 100 using for loop
#include <stdio.h>

int main()
{
    int i;

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

C++ program

//to print nos from 1 to 100 using for loop
#include <iostream>

using namespace std;

int main()
{
    int i;

    for(i = 1; i <= 100; i++)
    {
        cout<<i<<"\t";
    }
    
    return 0;
}

2. Output

For loop in C or C++

Also See : How is C++ different from C.

3. Program Parts

  1. include is a pre-processor directive. It is used to include header files.
  2. stdio/iostream is header file which has certain commands that c/c++ supports. E.g. return, main, etc.
  3. conio is a header file which is use to include commands like clrscr(),getch() in the code.
  4. .h is an extension for header file.
  5. int is data type. It indicates that the program returns a value.
  6. main() is main function that indicates the compiler that the user written programs starts from here.
  7. i is a variable name.
  8. for is looping structure in C/C++. It repeats the code inside it until specified condition is satisfied.
  9. print/cout is the keyword use to print a message.
  10. return statement is use to return a value. If any error occurs the program will return 0.

4. Explanation

As usual program will check the headers files first. If required header files are available then it will execute further commands. Now the program enters main function. Here it will declare a variable i. After declaration it will encounter for loop where it will first initialize i to 1, then will check if i is less than or equal to 100. If the condition is true then it will for loop and execute the commands written inside it. After executing print statement the value of i will be increased by 1. Now it check again if i is less than or equal to 100 and will continue to execute printf statement. Once value of i becomes 101 then it won’t satisfy the condition of i less than or equal to 100. Thus it will come out of the for loop and the program execution will stop. And that’s it we have successfully printed 1 to 100 numbers.

Conclusion

So these were the programs showing for loop in C/C++. If you liked this post then do share it with your friends and programming buddies. Also do tell us if you want us to post more examples of for loop. Tell us what content you would like us to post.

And for daily updates do follow us on instagram. Visit again!

ErrorFreeProgram.

4 COMMENTS

Leave a Reply

Your email address will not be published. Required fields are marked *