MHB Understanding nested for loops when decrementing

  • Thread starter Thread starter smilesofmiles
  • Start date Start date
  • Tags Tags
    For loops Loops
AI Thread Summary
Understanding decrementing in nested for loops can be confusing. The first code snippet outputs a pattern by decrementing j from i down to 0, producing numbers in reverse order for each line. The inner loop only runs while j is greater than or equal to 0, preventing it from going negative. The second code attempt incorrectly uses an incrementing loop, which does not achieve the desired output. Proper use of braces can clarify the structure of loops, enhancing readability and understanding of the code's flow.
smilesofmiles
Messages
18
Reaction score
0
Hello! :-) I am having a hard time with understanding the logic behind decrementing nested for loops. The review problem I'm dealing with is below. This one makes the numbers go backwards.

Code:
#include <iostream>
using namespace std;

int main(){
for (int i = 0; i < 5; i++){
		for (int j = i; j >= 0; j--) /* What's going on here? This looks so simple but I can't figure it out. */
			cout << j; // I understand that j is going down by one each time the inner loop condition is met?
			cout << endl;
	}
return 0;
}

When I put this to paper, the second for loop changes j to a number less than 0. I know that can't possibly be right.
So, I tried solving the problem in a different way. I thought it would help me figure out what's going on above but I still don't understand the logic. My code doesn't make the numbers go backwards. Here's what I would've done.

Code:
#include<iostream>
using namespace std;

int main (){
	for (int i = 0; i < 5; i++){ //This is saying that there are up to 5 places in each row from 0.
		for (int j = 0; j <= i; j++) // For each column they will print until they match each row.
			cout << j;  // The row will then increment by one when j <=i is satisfied. 
		cout << endl;
	}
	return 0;
}

What am I missing? Any help or direction is greatly appreciated with a thank you in advance!
 
Last edited:
Technology news on Phys.org
What is the actual task (problem statement) you are supposed to perform with your code?
 
I'm sorry about that I should've been much clearer! There is no specific problem just a lack of understanding about decrementing nested for loops.

The first code I had posted above used this particular line that I am having trouble wrapping my head around.

Code:
#include <iostream>
using namespace std;

int main(){
for (int i = 0; i < 5; i++){
		for (int j = i; j >= 0; j--) //This line.
			cout << j;
			cout << endl;
	}
return 0;
}

The output for this code produces this number pattern.

0
10
210
3210
43210

However, each time I go through the for loop I end up with a negative number. For the first loop, I find that i=0 and because 0 < 5 the condition is satisfied. Then I reach the second for loop and I come up with j = 0 and because 0<=0 the loop body executes. A zero is then printed as j with a newline. When I reach this portion I know that I need to decrease j by one but this leads to a negative number, 0 - 1 = -1. This is where I find a block and would appreciate any help with. Why does it end up being negative? I understand that it would need to be a positive number to continue through the for loop but I am not sure where I went wrong.

The second code was just my attempt to recreate the same output but in a different way. I thought it would help me understand the decrementing portion of the first code. My code was a failure and I realize that I should not have included it.

Thank you for any help or direction. :-)
 
I would use a more standard indenting style:
Code:
#include <iostream>
using namespace std;

int main()
{
	for (int i = 0; i < 5; i++)
	{
		for (int j = i; j >= 0; j--) //This line.
		{
			cout << j;
		}
		cout << endl;
	}
	return 0;
}

During the first iteration of the outer for loop, we have that [m]i = 0[/m], and so the inner for loop will iterate once, for [m]j = 0[/m]. This "0" is output, and then a newline is output since the inner loop is done.

During the second iteration of the outer for loop, we have that [m]i = 1[/m], and so the inner for loop will iterate twice, for [m]j = 1[/m] and then [m]j = 0[/m]. This "1" is output, then the "0", and then a newline is output since the inner loop is done.

The program carries on in a like manner for [m]i = 2,3,4[/m].
 
I don't know why I thought the inner loop absolutely had to keep decreasing past 0. Also, I wasn't quite sure on placements for the braces but now I think I've got it. My teacher tends to not use them in her codes.

Thank you for taking the time to help me learn. :-)
 
smilesofmiles said:
I don't know why I thought the inner loop absolutely had to keep decreasing past 0. Also, I wasn't quite sure on placements for the braces but now I think I've got it. My teacher tends to not use them in her codes.

Thank you for taking the time to help me learn. :-)

The inner loop, indexed by the integer [m]j[/m], will only iterate down to [m]j=0[/m] as given by the condition you placed on it, [m]j >= 0[/m]. This means the inner loop will only iterate as long as [m]j[/m] is greater than or equal to zero.

As far as the braces and indenting go, as you code more and more, you will develop your own style, which makes the best sense to you. For example this:

Code:
		for (int j = i; j >= 0; j--) //This line.
		{
			cout << j;
		}
		cout << endl;

is equivalent to:

Code:
		for (int j = i; j >= 0; j--) //This line.
			cout << j;
		cout << endl;

If the body of the construct consists of only one line of code, then the braces are unnecessary.

I prefer the former code over the latter as it is more clear to me what constitutes the body of the for loop, but it is just a matter of personal preference. Many coders put the opening brace on the same line as the construct declaration:

Code:
		for (int j = i; j >= 0; j--) { //This line.
			cout << j;
		}
		cout << endl;
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top