0

I'm sure this is a very basic question but I'm struggling with this. Why can't I make a decreasing "For" loop? My code is below. Why doesn't this work? I just want to output "Decreasing, 4" "Decreasing, 3", etc. Thanks in advance!

int h(void);

int main(void)

{

for (int h = 5; h <=0; h--)

{ printf("Decreasing, %i",(h-1));

}

}

1 Answer 1

1

let's read what the loop says, "run while the condition is true, ie while h is less or equal to zero" which obviously is not fulfilled. a for does not care if you increase or decrease the index, it is more does not know until the loop is executed, you think "the loop will stop executing when h becomes negative" but you must think in terms of truth or falsehood: if THE CONDITION IS CERTAIN THE LOOP IS EXECUTED. your loop will work if you do:

for ( int h = 5; h > 0; h--)

(good question)

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .