Understanding nested for loops when decrementing

In summary, there is a lack of understanding about decrementing nested for loops. The first code uses a particular line that may be difficult to comprehend, while the second code is an attempt to recreate the same output but in a different way. The output pattern produced by the first code is 0, 10, 210, 3210, 43210, and the confusion arises when j is decreased past 0. However, the inner loop will only iterate down to 0 as given by the condition j >= 0. The placement of braces may also cause confusion, but ultimately, the program outputs the desired pattern by iterating through the outer for loop and then the inner for loop.
  • #1
smilesofmiles
19
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
  • #2
What is the actual task (problem statement) you are supposed to perform with your code?
 
  • #3
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. :-)
 
  • #4
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].
 
  • #5
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. :-)
 
  • #6
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;
 

What is a nested for loop?

A nested for loop is a control flow statement in programming that allows for the execution of a series of statements multiple times. It consists of a loop within another loop, with each loop having its own control variable and body of code.

Why is decrementing used in nested for loops?

Decrementing is used in nested for loops in order to control the number of iterations that occur within the loop. By decrementing the control variables, the loop will execute a certain number of times before terminating, rather than continuing indefinitely.

How do you read and understand a nested for loop when decrementing?

To read and understand a nested for loop when decrementing, it is important to start from the innermost loop and work your way out. Keep track of the values of the control variables as they change with each iteration, and pay attention to the conditions that determine when the loop will terminate.

What are some common mistakes when using nested for loops with decrementing?

Some common mistakes when using nested for loops with decrementing include forgetting to decrement the control variable, which can lead to an infinite loop, and not keeping track of the values of the control variables, which can result in unexpected behavior. It is also important to ensure that the conditions for terminating the loop are correct, as errors in the condition can also lead to unexpected results.

How can I improve my understanding of nested for loops when decrementing?

To improve your understanding of nested for loops when decrementing, it is helpful to practice writing and executing different examples of nested for loops. You can also try breaking down complex nested for loops into smaller parts and tracing the code to see how the control variables and conditions change with each iteration. Additionally, seeking out tutorials and resources on nested for loops can also aid in your understanding.

Similar threads

  • Programming and Computer Science
Replies
12
Views
1K
  • Programming and Computer Science
Replies
15
Views
2K
  • Programming and Computer Science
Replies
13
Views
1K
  • Programming and Computer Science
Replies
3
Views
734
  • Programming and Computer Science
Replies
22
Views
2K
  • Programming and Computer Science
2
Replies
66
Views
4K
  • Programming and Computer Science
Replies
1
Views
992
  • Programming and Computer Science
Replies
4
Views
785
  • Programming and Computer Science
Replies
1
Views
881
  • Programming and Computer Science
Replies
6
Views
8K
Back
Top