Understanding nested for loops when decrementing

  • Context: MHB 
  • Thread starter Thread starter smilesofmiles
  • Start date Start date
  • Tags Tags
    For loops Loops
Click For Summary

Discussion Overview

The discussion revolves around understanding the logic of decrementing nested for loops in C++. Participants explore the behavior of two different code snippets, focusing on how the inner loop operates and the output it generates.

Discussion Character

  • Exploratory
  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant expresses confusion about the inner loop's decrementing behavior, particularly how it can lead to negative values.
  • Another participant clarifies that the inner loop only iterates while j is greater than or equal to zero, thus preventing it from going negative.
  • There is a discussion about the output pattern generated by the first code snippet, which produces a sequence of numbers in reverse order.
  • One participant attempts to recreate the output using a different approach but finds it unsuccessful, leading to further questions about the logic.
  • Another participant suggests a more standard indentation style for clarity in the code structure.
  • Participants discuss the use of braces in loops, noting that their inclusion is a matter of personal coding style preference.

Areas of Agreement / Disagreement

Participants generally agree on the mechanics of the inner loop and its conditions, but there is no consensus on the necessity of braces or specific coding styles. The confusion regarding the decrementing behavior is acknowledged but not fully resolved.

Contextual Notes

Some participants mention uncertainties regarding the placement of braces and the implications of different coding styles, which may affect readability and understanding of the loop structure.

Who May Find This Useful

Readers interested in programming, particularly those learning about loops and control structures in C++, may find this discussion beneficial.

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;
 

Similar threads

Replies
12
Views
3K
  • · Replies 13 ·
Replies
13
Views
2K
  • · Replies 22 ·
Replies
22
Views
4K
  • · Replies 1 ·
Replies
1
Views
1K
  • · Replies 1 ·
Replies
1
Views
2K
  • · Replies 5 ·
Replies
5
Views
3K
  • · Replies 15 ·
Replies
15
Views
4K
  • · Replies 1 ·
Replies
1
Views
6K
  • · Replies 4 ·
Replies
4
Views
6K
  • · Replies 2 ·
Replies
2
Views
8K