Why does this infinite loop happen?

Asked

Viewed 98 times

0

I made this code but it keeps giving endless loop and I can’t fix...

#include <stdio.h>

int main (){
int x, z;

        while("x=100; x!=65; x-=5"){
            z=x*x;
            printf("%d --> %d\n", x, z);
        }
}

1 answer

3


I’m pretty sure it’s because of the quotes:

"x=100; x!=65; x-=5"

Remove them:

 while(x=100; x!=65; x-=5){

values between quotation marks in C are char *, which means you didn’t pass the variables, but yes char, and pro loop break needs to pass a value like "false", yet while does not work with ;, you probably want to use the for:

for (x=100; x!=65; x-=5){
    z=x*x;
    printf("%d --> %d\n", x, z);
}

Or if it’s the while you can use it like this:

x=100;

while (x!=65){
    z=x*x;
    printf("%d --> %d\n", x, z);
    x-=5;
}
  • I have tried to put it error when compiling and is asking for a ) after (x=100 6 15 [Error] expected ')' before ';' token

  • @Evelynndelrey edited the answer.

  • What you sent me worked, but like most of my class did x=100; and x-=5; inside while(), how would it look? wanted to know what I did wrong ...

  • @Evelynndelrey that as far as I know doesn’t exist in C, you must have been confused. Documentation MSDN https://msdn.microsoft.com/pt-br/library/y1tscb5y.aspx

  • probably man, Rigadão by help <3

Browser other questions tagged

You are not signed in. Login or sign up in order to post.