For Loop in C++ Programming


The for loop in C++ is an entry-controlled loop.A for loop is a repetition control structure which allows us to write a loop that is executed a specific number of times. The three components of the for loop (separated by ;) are variable declaration/initialization, the condition, and the increment/decrement statement.

The variable declaration is done once as if placed just inside the { on the first run. Then the condition is checked, if it is true the body of the loop will execute, if it is false the loop will stop. Assuming the loop continues, the body will execute and finally when the } is reached the increment statement will execute just before the condition is checked again.

The curly braces are optional (you can one line with a semicolon) if the loop contains just one statement. But, it's always recommended to use braces to avoid misunderstandings and bugs. The for loop components are optional. If your business logic contains one of these parts, you can omit the corresponding component from your for loop.

Syntax:
   for( initial ; condition ; increment / decrement)
   {
        // body of loop;
   }

This C++ program generates a multiplication table of a given number up to a specified limit. Here's how the program works:

  • It includes the iostream library, which allows the program to perform input/output operations.
  • It declares the main() function, which is the entry point of the program.
  • It declares three integer variables: i, n, and t.
  • It displays a message asking the user to enter the limit.
  • It reads the user's input and stores it in the variable n.
  • It displays a message asking the user to enter the number to generate the multiplication table for.
  • It reads the user's input and stores it in the variable t.
  • It begins a for loop that starts at i = 1 and continues as long as i is less than or equal to n.
  • Within the for loop, it displays the current multiplication of the input number t and the current value of i, along with an equal sign and the result of the multiplication, using the cout statement.
  • After displaying the multiplication, it moves to the next line using the endl statement.
  • Once the for loop has completed, it returns 0 to indicate successful execution of the program.

Source Code

//For Loop
/*
    n=5
    t=2
    2*1=2
    .
    .
    2*5=10
*/
#include<iostream>
using namespace std;
int main()
{
    int i,n,t;
    cout<<"\nEnter The Limit : ";
    cin>>n;
    cout<<"\nEnter The Table : ";
    cin>>t;
    for(i=1;i<=n;i++)
    {
        cout<<t<<"*"<<i<<"="<<t*i<<endl;
    }
     return 0;
}
 

Output

Enter The Limit : 10

Enter The Table : 6
6*1=6
6*2=12
6*3=18
6*4=24
6*5=30
6*6=36
6*7=42
6*8=48
6*9=54
6*10=60
To download raw file Click Here

Program List


Flow Control

IF Statement Examples


Switch Case


Goto Statement


Break and Continue


While Loop


Do While Loop


For Loop


Friend Function in C++


String Examples


Array Examples


Structure Examples


Structure & Pointer Examples


Structure & Functions Examples


Enumeration Examples


Template Examples


Functions


Inheritance Examples

Hierarchical Inheritance


Hybrid Inheritance


Multilevel Inheritance


Multiple Inheritance


Single Level Inheritance


Class and Objects

Constructor Example


Destructor Example


Operator Overloading Example


Operator and Function Example


List of Programs


Pointer Examples


Memory Management Examples


Pointers and Arrays


Virtual Function Examples