First of all, hi everyone!
Im a C++ newbie and i'd like to get a good grasp of the concepts soon.

I'm currently working on this

#include <iostream>
using namespace std;
 

int main()

{

int i, j, lines;
	
	cout << "This Program creates Christmas Trees." << endl;
	
	do
	{
		cout << "Please enter a number between 1 and 30" << endl;
		cin >> lines;
	}	
	while (lines < 1 || lines > 30);

	for(j = 1; j <= lines*2; j=j+2)
	{
	
		for(i=j; i <= 80 ; i=i+2)
	{
		cout << " ";

	}

		for(i = 1; i <= j; i++)
	{
		cout << "*";
	}
	

	cout << endl;
	}
system ("pause");
return 0;
}

I understand everything except for the most important parts-- the for loops.

My textbook does a really poor job explaining them, and i am confused as what the int i, j mean
and how the loops work together.

This code asks the user to input a number between 1 and 30 and it creates a perfect triangle of asterisks (or a "Tree").

My problem is that like I said, I don't understand how the tree is done, or why you need the equations in the loops.

I'd greatly appreciate if someone could help me better understand these concepts!
Thanks a lot =]

From the looks of it when you select the number the program prints out a tree by looping for more information on loops see http://www.cplusplus.com/doc/tutorial/control/ its halfway down the page.

for(j = 1; j <= lines*2; j=j+2)

as for this bit of code it is saying that from j is equal to one and j is less than the number of lines *2 add two to j- this is the control statement

hi
>>>My textbook does a really poor job explaining them, and i am confused as what the int i, j mean
and how the loops work together.

the i and j are used to to print out the spaces and the stars in a special order , i used to print the spaces
and j is used to print the stars the equations which updates i , and j are used so that the shape required get printed

Hello and welcome!

Just a little suggestion: Please be more specific with your thread title. A generic title like "pls halp noob here" wont help anyone who's got the same problem and searching for it.

Anyway off to the explanation!

int i, j, lines;

This is a variable declaration. You are telling the compiler that your program has three variables, namely 'i', 'j', and 'lines', and all three these variables are of the integer type. The compiler wants to know that in order to reserve enough memory on the stack for the program to run.

do
{
  cout << "Please enter a number between 1 and 30" << endl;
  cin >> lines;
}	
while (lines < 1 || lines > 30);

This is known as a DO-WHILE loop. It basically means:

do
{
  some instructions;
  and some more instructions;
}
while (everything in here checks out as true)

It basically asks the user to input a number between 1 and 30, and it will continue to ask this question until the correct number has been supplied (ie a number between 1 and 30). It is used to ensure that no values are given that fall outside the program's parameters.

for(j = 1; j <= lines*2; j=j+2)

This translates to:

"for the variable j, starting at one, continue until j <= lines * 2. After every iteration, add 2 to the current value of j."

So in effect, this is going to execute the the code block as many times as the user specified during input. In this case j also holds the amount of stars it should print on the screen. 1 for the first iteration, 3 for the second, 5 for the third and so on (j = j+2.

for(i=j; i <= 80 ; i=i+2)
{
  cout << " ";
}

This code makes sure that all the stars are centered, so that it looks like a pyramid.
We know that 'j' holds the amount of stars that needs to be printed out during that line. The code uses that value, to print out an appropriate amount of whitespace characters, so that the pyramid looks like a pyramid.

So, if we need to print out:
1 star, print 80 blank spaces.
3 stars, 78 spaces
5 stars, 76 spaces
and so on.

for(i = 1; i <= j; i++)
{
  cout << "*";
}

If you grasp the whitespace code, you will understand this easier. This code prints out the amount of stars. 'j' holds the amount of stars to print per line, and this for-loop does just that, it prints the amount of stars according to the value of 'j'.

cout << endl;

This is at the end of the main for-loop. It prints out the EOL character, so that we can print the next batch of stars on a new line.

Just a note of caution:

system("pause")

Can possibly cause problems. I compile on Ubuntu-Linux, and it won't even compile with that line in. I presume you use Windows?
As far as I can tell, it's supposed to pause the system, so that the terminal doesn't close before you can see the output.
Some anti-virus programs will actually flag a compiled executable with a 'system' command in as malicious software...
Rather use something like

cout << "press any key to quit ";
char temp;
cint >> temp;
return 0;
Comments
Good effort

a for loop is very useful. It will loop through the code contained within the curly braces.

for(::)
{
cout << "loop";
}

there are three sections that control the for loop.

for(1:2:and 3)

1. You tell the for loop to use this variable to control it. you can either declare at 1 or before the loop. (int i = 0 : 2 : 3)

2. Here, you tell the loop to check if this statement returns TRUE. If it indeed returns TRUE, the loop will continue. (int i = 0: i < 10: 3)

3. In the last part of the brackets, you should tell the loop to change something, because if (in this case 'i') nothing changes, the loop will run forever. (int i = 0: i < 10: i++).

The ++ operator adds 1 to the variable. eventually i will become equal to 10 and the loop will break. alternatively to exit the loop when you want, you can use the BREAK: statement.

Hello and welcome!

Just a little suggestion: Please be more specific with your thread title. A generic title like "pls halp noob here" wont help anyone who's got the same problem and searching for it.

Anyway off to the explanation!

int i, j, lines;

This is a variable declaration. You are telling the compiler that your program has three variables, namely 'i', 'j', and 'lines', and all three these variables are of the integer type. The compiler wants to know that in order to reserve enough memory on the stack for the program to run.

do
{
  cout << "Please enter a number between 1 and 30" << endl;
  cin >> lines;
}	
while (lines < 1 || lines > 30);

This is known as a DO-WHILE loop. It basically means:

do
{
  some instructions;
  and some more instructions;
}
while (everything in here checks out as true)

It basically asks the user to input a number between 1 and 30, and it will continue to ask this question until the correct number has been supplied (ie a number between 1 and 30). It is used to ensure that no values are given that fall outside the program's parameters.

for(j = 1; j <= lines*2; j=j+2)

This translates to:

"for the variable j, starting at one, continue until j <= lines * 2. After every iteration, add 2 to the current value of j."

So in effect, this is going to execute the the code block as many times as the user specified during input. In this case j also holds the amount of stars it should print on the screen. 1 for the first iteration, 3 for the second, 5 for the third and so on (j = j+2.

for(i=j; i <= 80 ; i=i+2)
{
  cout << " ";
}

This code makes sure that all the stars are centered, so that it looks like a pyramid.
We know that 'j' holds the amount of stars that needs to be printed out during that line. The code uses that value, to print out an appropriate amount of whitespace characters, so that the pyramid looks like a pyramid.

So, if we need to print out:
1 star, print 80 blank spaces.
3 stars, 78 spaces
5 stars, 76 spaces
and so on.

for(i = 1; i <= j; i++)
{
  cout << "*";
}

If you grasp the whitespace code, you will understand this easier. This code prints out the amount of stars. 'j' holds the amount of stars to print per line, and this for-loop does just that, it prints the amount of stars according to the value of 'j'.

cout << endl;

This is at the end of the main for-loop. It prints out the EOL character, so that we can print the next batch of stars on a new line.

Just a note of caution:

system("pause")

Can possibly cause problems. I compile on Ubuntu-Linux, and it won't even compile with that line in. I presume you use Windows?
As far as I can tell, it's supposed to pause the system, so that the terminal doesn't close before you can see the output.
Some anti-virus programs will actually flag a compiled executable with a 'system' command in as malicious software...
Rather use something like

cout << "press any key to quit ";
char temp;
cint >> temp;
return 0;

Wow, thanks a lot! That clears up that question.

Way to go the extra mile and thanks everyone for their help/hints.

as for the system("pause")-

Yes, I use windows. I didn't realize that.
I will try to implement that into my next code.
Do you have to declare a variable for "temp"? or what does "temp" mean.

Another question. Is it complicated to code and implement something like "Press Q to exit"?

This article has been dead for over six months. Start a new discussion instead.