What does while(x-) mean in C language?

Asked

Viewed 874 times

1

I came across this code and didn’t understand the tie while(a--), what it means?

int main(){
     char pisca[10], a = 3;
     short numero;

     while(a--){
         numero = 0;

         while(1){
             scanf("%s caw", pisca);

             if(!strcmp(pisca, "caw")) break;

             if(pisca[0] == '*') numero += 4;
             if(pisca[1] == '*') numero += 2;
             if(pisca[2] == '*') numero += 1;

         } 

         printf("%hd\n", numero);
     }

     return 0;
}

2 answers

6


Let’s start with the expression inside the while: a-- is the same as a = a - 1.

I imagine you know what the while does. Every time you pass it you will execute that expression, so every time the variable a will have subtracted 1. Until 0.

The while executes while it is true, or any number other than 0. When it reaches zero it considers false and therefore the loop must be closed.

-2

The implicit condition of while is "!= 0" the "a--" is not testing anything.

Browser other questions tagged

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