PROBLEM with FOR loop (my program does not run, it must start a count in 10 and end in 0)

1

How about friends, I have a very big question with the FOR loop, why does not this code give me results ??? That is, it does not run

#include<stdio.h>

int main() 
{
   int i=0;

   for (i=10;i<=0;i--)

    printf(" %d ",i);

}

However, doing a bit of research, I found the following, which works perfectly.

#include<stdio.h>

int main() 
{
   signed int i=0;

  for (i=10;i>-1;i--)

  printf(" %d ",i);

}

My question is: why the first code does not work and the second yes, because C does not understand the parameter <=0 ? Thanks in advance, I am a self-taught beginner and I have some doubts.

    
asked by GabrielMT 04.01.2018 в 19:53
source

5 answers

8

Basically the conditions you have, you should be more careful when creating your algorithms.

In The First:

for(i=10;i<=0;i--)

your condition looks this way for(i=10;10<=0;i--) condition that if you notice is false ( false ) from the beginning, what you should do is

for(i=10;i>=0;i--)

which looks like: for(i=10;10>=0;i--) which is true (true)

In The Second

for (i=10;i>-1;i--)

your condition becomes true and that's it.

    
answered by 04.01.2018 / 20:08
source
0

In conclusion, your code should work as follows

#include<stdio.h>

int main() 
{
   int i=0;

   for (i=10;i>=0;i--)

    printf(" %d ",i);

}

to be functional only changes the < (less than) by > (greater than)

so that for read as follows "iterate variable i with initial value of 10, provided it is greater than or equal to 0 and after each iteration subtracts 1 from the value of i"

    
answered by 05.01.2018 в 00:58
0

for (i=10;i<=0;i--) This is a logical error. You are saying that Para i = 10 to 0 increased and decreasing i to 1. One moment, you can not go from one major number to another minor increasing, therefore the comparison is inadequate. Consequently, it is wrong is a sign of a minor. As other users commented, the correct thing would be: for (i=10;i>=0;i--)

    
answered by 25.02.2018 в 15:22
0

As it says @eyllanesc

  

the first field is the one that initializes variables, the second is a conditional, and the third is a task that is carried out when the content is finished

In the first one it does not work since you are saying that you go 10 times the variable i until i (10) is less or = 0 by logic 10 it will never be less or equal to zero, and the second one may work for you already that logic 10 is a positive number and -1 is negative, but it would not make sense to go over the for in that way, possibly it will not show you anything.

    
answered by 04.01.2018 в 20:13
0

Your error is in the condition of the for where it says i

answered by 27.02.2018 в 20:45