Please help me xplain...

hello guys..good day..this is my 3rd day of learning how to program and I stumbled upon this code and I really don't understand a certain part of this program..can you help me?



#include<iostream>
using namespace std;

int main()
{
int i,j,N;

cout<<"Type the value of N : ";cin>>N;

for(i=1;i<=N;i++)
{

this part------->> for(j=1;j<=N+1-i;j++)cout<<"*";

cout<<endl;
}


system("pause");
}





this code outputs a descending number of asterisks depending on input N..
but I really don't quite understand that part which I marked,can someone kindly explain to me how it works...
Last edited on
closed account (zb0S216C)
The loop you pointed out is called a nested loop. A nested loop is a loop that is within another loop.

The loop first sets j, a predefined variable, to 1. While j's value is less than or equal to N's value, it continues to loop. After each cycle, j is incremented by 1 (++ means increment by 1, and -- means decrement by 1). For each cycle of the loop, an asterisk is displayed.

Please, use [_code][/code] tags (without the underscore) in the future. Thanks.

Wazzak
Last edited on
oh so the j<=N+1-i part means the value of N is decreased by 1 every cycle??

thank you sir..
just a tip, you forgot return 0; at the end of your program and remove the system("pause"); please. read the second post on this forum about that if you are wondering. and when initializing a for loop is is a good idea to start it at 0. this will become evident later. so you would do
for(int j = 0; j <= N; j++) std::cout << "*";
Last edited on
No, N should not be changed. The for just verifies whether j fits the condition. The

j<=N+1-i

means that j has to be smaller than N+1-i.
Topic archived. No new replies allowed.