Cpp - Nested for Loops

Introduction

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

Demo

#include <iostream> 

int main() // ww  w .  j  ava2s  .co  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