LATEST

How to use for loop in C / C++

In this tutorial, we will learn about the C / C++ for loop and its working with the help of some examples. This is one of the basic knowledge in C / C++ programming language, so please learn it carefully.

What is for loop in C / C++?

In C / C++ programming language, for loop is used to repeat a block of code. It’s used in case we want to repeat a certain action many times.

For example: Let’s say we want to display numbers from 1 to 100. Then instead of writing the print statement 100 times, we can use a for loop.

For loop is used in case we know the number of iterations in advance, different from while and do...while loops.

Flowchart of for Loop in C++:

loop 01 PNG

Suppose we want to display "Welcome to learnnc.com" 10 times, then the for loop would look like this:

for(int i = 1; i<=10;i++){
    cout<<"Welcome to learnnc.com\n";
}

Explain:

  • The condition of the for loop is i <= 10, now i = 1, so the statements inside the body for loop are executed.
  • After executing, update i++ value and execute next loop.
  • Continue looping until i > 10, which doesn't satisfy the condition, so the loop ends.

Output: As a result, we have 10 lines "Welcome to learnnc.com".

The syntax of for loop in C / C++

The syntax of the for loop is shown below:

for (initialization; condition; update) {
    // body of-loop 
}

Here,

  • initialization: initializes variable (executed only once).
  • condition: condition of the loop, there are two case:
    • If true: The body of for loop is executed.
    • If false: The for loop is terminated.
  • update: Updates the value of initialized variables and again check the condition for next loop.

Example: Let’s say we want to display numbers from 1 to 100, then the for loop would look like this:

cout<<"Numbers from 1 to 100: \n";
  for(int i = 1; i<=100;i++){
    cout<<i<<" ";
  }

Expain:

  • Initialization: int i = 1.
  • Condition: i <= 100.
  • Update: i++.

Output: The numbers from 1 to 100.

For loop repeats infinitely

The case where the for loop is infinitely repeated is when its condition is always true. This is not good, it will slow down the program and possibly freeze.

For example:

#include <iostream>  
using namespace std;  
int main () {  
    for (; ;) {    
         cout << "For loop repeats infinitely";    
    }    
}

Explain: We do not condition it here. That is, the condition is always true, so the loop will repeat indefinitely.

Other example:

#include <iostream>  
using namespace std;  
    
int main () {
    for(int i=5;i>0;i++){      
        cout << "For loop repeats infinitely"; 
    }  
}

Explain: The condition of the for loop is i > 0, but i = 5 so the condition is always true. This leads to infinite looping.

Let's see the examples with for loop in the next section to understand better.

For loop in C / C++ with examples

Here, we have two examples with for loop in C/C++. These are basic examples when using for loop in C/C++.

Example 1: Displays numbers from 1 to n, with n being entered from the user.

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int n;
  cout<<"Enter number n: ";
  cin>>n;
  cout<<"Numbers from 1 to "<<n<<" : \n";
  for(int i = 1; i<=n;i++){
    cout<<i<<" ";
  }
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int n;
  printf("Enter number n: ");
  scanf("%d", &n);
  printf("Numbers from 1 to %d : \n",n);
  for(int i = 1; i<=n;i++){
    printf("%d ",i);
  }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

loop 02 PNG

Example 2: Displaying "Welcome to learnnc.com" n times, with n being entered from the user.

C++ programming:

#include <iostream>
using namespace std;
int main() {
  int n;
  cout<<"Enter number n: ";
  cin>>n;
  for(int i = 1; i<=n;i++){
    cout<<"Welcome to learnnc.com!!!\n";
  }
  cout<<"\n-------------------------------\n";
  cout<<"This program is posted at learnnc.com";
  return 0;
}

C programming:

#include <stdio.h>

int main(void) {
  int n;
  printf("Enter number n: ");
  scanf("%d", &n);
  for(int i = 1; i<=n;i++){
    printf("Welcome to learnnc.com!!!\n");
  }
  
  printf("\n-------------------------------\n");
  printf("This program is posted at learnnc.com");
  return 0;
}

Output:

loop 03 PNG

Thank you for following this tutorial, visit learnc.com for more articles.

Cùng chuyên mục:

How to use strings in C / C++

How to use strings in C / C++

How to use multidimensional Arrays in C / C++

How to use multidimensional Arrays in C / C++

Searching and sorting arrays in C / C++

Searching and sorting arrays in C / C++

How to use arrays in C / C++

How to use arrays in C / C++

How to use goto statement in C / C++

How to use goto statement in C / C++

How to use switch...case statement in C / C++

How to use switch...case statement in C / C++

How to use continue statement in C / C++

How to use continue statement in C / C++

How to use break statement in C / C++

How to use break statement in C / C++

How to use if - else statement in C / C++

How to use if - else statement in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use Binary Search Tree (BST) in C / C++

How to use do while loop in C / C++

How to use do while loop in C / C++

How to use while loop in C / C++

How to use while loop in C / C++

Top