C easy program not working - “if”

后端 未结 5 1268
情书的邮戳
情书的邮戳 2020-12-22 00:30

I tried to write an easy program that compares 3 numbers and prints the biggest of them but it keeps printing all 3 of them and I don\'t get why. That\'s my code:

         


        
5条回答
  •  清歌不尽
    2020-12-22 00:53

    You have a semicolon after your if condition:

    if((x > y) && (x > z));
    

    The semicolon takes the place of the block or statement to be executed when the condition is true. It's as if you had written:

    if((x > y) && (x > z))
      {
        ;
      }
      {
        printf("%d",x);
      }
    

    You can hopefully see how this will execute the print statement unconditionally.

提交回复
热议问题