How to make the if of multiple variables with "or" work?

Asked

Viewed 2,386 times

-3

When I run this program, I choose the whole 1 and enter the two if's. Why are you in n==1 and in the n!=1 at the same time?

#include<stdio.h>

int main(){

    int n; 
    puts("ESCOLA UMA OPCAO");
    puts("1. opcao1");
    puts("2. opcao2");

    scanf("%d", &n);

    ///////////////////////////////////////////////
    if(n==1){
        puts("opcao1");
    }
    ///////////////////////////////////////////////
    if(n==2){
        puts("opcao2");

    }
    ///////////////////////////////////////////////
    if( n!=1 || n!=2)
    {
        puts("erro");
    }

    return 0;
}
  • Has not else some in this code.

  • 2

    I don’t understand what your problem is, you could edit the question and clarify it a little more?

  • I think the intention there was to make E-if, not several ifs.

  • 3

    @user3903094 will always enter the if( n!=1 || n!=2), because the condition is always true. There is no way n was 1 and 2 at the same time. If n=1, the part n!=2 is true, therefore the expression is true. If n=2, the part n!=1 is true, therefore the expression is true. If n other than 1 and 2, both parts are true, therefore the expression is true. The error is in the implementation of logic, the operator "or" (||) makes no sense in this context.

2 answers

1

Another option, besides the previous answer, is to use a structure if-else chained:

#include<stdio.h>

int main(){

    int n;
    puts("ESCOLA UMA OPCAO");
    puts("1. opcao1");
    puts("2. opcao2");

    scanf("%d", &n);

    if(n==1){
        puts("opcao1");
    } else if(n==2) {
        puts("opcao2");
    } else {
        puts("erro");
    };
    return 0;
}

Update: a structure switch also works for this case and, eventually, can make the code more readable:

#include<stdio.h>

int main(){

    int n;
    puts("ESCOLA UMA OPCAO");
    puts("1. opcao1");
    puts("2. opcao2");

    scanf("%d", &n);

    switch (n) {
        // Caso n seja 1
        case 1:
            puts("opcao1");
            break; // termina

        // Caso n seja 2
        case 2:
            puts("opcao2");
            break; // termina

        // Caso contrário
        default:
            puts("erro");
            break; // termina
    };
}
  • thank you I prefer the first option, yet I didn’t miss out on why putting 1 enters in the if (n=1) and also in the if(n!= 1 || n!=2)

  • @user3903094: Because of the operator OR (||). In your code, with n=1, at last ifthe condition n!=1 is false, however, the condition n!=2 is true. FALSO OU VERDADEIRO = VERDADEIRO, so he enter the last if.

0


For your logic to work, what you want has to put in the last if the E

that is to say

int n; 
puts("ESCOLA UMA OPCAO");
puts("1. opcao1");
puts("2. opcao2");

scanf("%d", &n);

///////////////////////////////////////////////
if(n==1){
    puts("opcao1");
}
///////////////////////////////////////////////
if(n==2){
    puts("opcao2");

}
///////////////////////////////////////////////
if( n!=1 && n!=2)
{
    puts("erro");
}

return 0;
  • James, to format code, select it click on the icon {}

  • that worked but it doesn’t make sense, to enter if you wouldn’t have to check the 2 conditions n!=1 and n!=2 at the same time? i think with the || operator it would make more sense. I don’t understand why whoever did C did this. and then if I wanted to check the 2 conditions at the same time n!=1 and n!=2 as it would be?

  • @user3903094 the compiler already tests the two conditions for you n != 1 e n != 2, in the case of C would look this way n!=1 && n!=2, as well as the implementation of the && means And, that is to say that the two conditions must be true to meet this criterion, namely the value of n must be different from 1 and of 2.

Browser other questions tagged

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