Cpp - Nested for Loops

Introduction

Loops can be nested with one loop sitting in the body of another.

Demo

#include <iostream> 

int main() //w w w  .  ja va 2 s . c  o  m
{ 
    int rows, columns; 
    char character; 
 
    std::cout << "How many rows? "; 
    std::cin >> rows; 
    std::cout << "How many columns? "; 
    std::cin >> columns; 
    std::cout << "What character to display? "; 
    std::cin >> character; 
 
    std::cout << "\n"; 
    for (int i = 0; i < rows; i++) 
    { 
           for (int j = 0; j < columns; j++) 
           { 
                std::cout << character; 
           } 
           std::cout << "\n"; 
    } 
    return 0; 
}

Result

Related Topic