top of page
for loop nested loop statement

The for statement is a statement that repeats itself, similar to the while statement , and the variable is defined inside it . However, the for statement contains three important elements:
We can write it in the compiler like this: {program commands} for ( int i=o ; i<5 ; i++ )
1- Starting point ( definition ) int i=o
2- End point ( condition ) i<5
3- Change the counter value ( counter ) ++ i
4- We open the curly brackets and put the programming commands in them.
Note: Variables must be separated
by semicolons.
We can also put two rotation sentences in the same range.

#include<iostream> //We start by placing the libraries
using namespace std; //We start by placing the libraries
int main() {//main function
int w,h;
cout<<"enter square width"<<endl;
cin>>w;
cout<<"enter square height"<<endl;
cin>>h;

for( int i=0 ; i<h ; i++ ) {
for( int j=0 ; j<w ; j++ ) {
cout<<"$"; }
cout<<endl;}
// endl We print the width of the box from the newline code
return 0;
}

Screenshot 2024-09-13 205827.png
bottom of page