Need Help with My C for Loop Condition

0
16
Asked By CodingNinja123 On

Hey everyone, I'm a beginner and I'm really struggling with understanding why my C for loop isn't working as I intended. I want it to print the following:
aa
a

Instead, I'm seeing this:
aa
aa
aa

Here's the for loop I wrote:
for (int j = h; j > 0; j--)

I think there might be an issue with the loop condition j > 0, but I can't figure out what's wrong. Can anyone help me understand why it's not working as expected? Any advice would be greatly appreciated!

3 Answers

Answered By DevWhiz789 On

Actually, with the code you’ve shared, looping backwards over `h` might not even be necessary. Also, it looks like in your `main()` function, you’re passing a constant value (`h-1`) to the `print_row()` function, which would affect your output. Definitely worth checking!

Answered By TechieGuru456 On

Could you share more of your code? The loop condition itself looks fine, but I wonder what’s going on inside the loop. Are you sure you’re using `j` where you should instead of `h`? That might be the problem!

CodingNinja123 -

Sure thing, here’s what I’m working with: https://imgur.com/a/ZXHgp2D

Answered By CodeCrafter321 On

You might want to try using j = h - i instead. That could fix the issue you're running into.

Related Questions

LEAVE A REPLY

Please enter your comment!
Please enter your name here

This site uses Akismet to reduce spam. Learn how your comment data is processed.