What is For loop in C++ Language? Example program to print counting up to given limit

Introduction: In this article i will explain what is the For loop in C++ Language and How to print the counting up to specified limit e.g. 1 to 6 or 6 to 1 using For Loop with example programs.
In previous articles i explained What is goto statement in C++ Language,its advantages and disadvantages with program example and Get number of days in month,year using nested if and Calculate sum,average and division of marks and Find week day corresponding to number using switch case and Find greatest of three numbers using multiple if statement .

Definition of  For Loop in C++ Language?

Description: For loop is an iterative statement that allows the code to be executed repeatedly specified number of times.
The statements written within the For loop repeat continuously for a specific number of times. For loop repeats the statement or group of statements until the specific count is met.  For loop can be used in the situation where the number of repetition is known i.e. how many times statement or the group of statements is to be executed is known in advance.

Syntax of For Loop:
for (initialize counter variable ; condition ; increment/decrement the counter variable)
{
                code to be executed;
}

 Implementation: Let's create a program in C++ language  to print the counting up to specified limit.

Here in this article i have demonstrated the use of For Loop using two basic programs:
  • Printing counting from 1 to given limit
  • Printing counting from given limit to 1 

//Printing counting from 1 to given limit
#include<conio.h>
#include<iostream.h>
void main()
{
 int limit,i;
 clrscr();
 cout<<"Enter the limit: ";
 cin>>limit;
 cout<<"Counting from 1 to "<<limit<<endl;
  for(i=1;i<=limit;i++)
  {
   cout<<i<<endl;
  }
 getch();

}
  •  Run the program using Ctrl+F9 
Output:  Enter the limit: 6
Counting from 1 to 6
1
2
3
4
5
6


//Printing counting from given limit to 1
#include<conio.h>
#include<iostream.h>
void main()
{
 int limit,i;
 clrscr();
 cout<<"Enter the limit: ";
 cin>>limit;
 cout<<"Counting from "<<limit<<" to 1"<<endl;
  for(i=limit;i>=1;i--)
  {
   cout<<i<<endl;
  }
 getch();

}
  • Run the program using Ctrl+F9 

Output:  Enter the limit: 6
Counting from 6 to 1
6
5
4
3
2
1

Now over to you:
"If you like my work; you can appreciate by leaving your comments, hitting Facebook like button, following on Google+, Twitter, Linked in and Pinterest, stumbling my posts on stumble upon and subscribing for receiving free updates directly to your inbox . Stay tuned and stay connected for more technical updates."
Previous
Next Post »

If you have any question about any post, Feel free to ask.You can simply drop a comment below post or contact via Contact Us form. Your feedback and suggestions will be highly appreciated. Also try to leave comments from your account not from the anonymous account so that i can respond to you easily.. Out Of Topic Show Conversion CodeHide Conversion Code Show EmoticonHide Emoticon

Thanks for your comment