Explain to a beginner the query "if(! found)" in the code

Asked

Viewed 51 times

-1

I have an exercise that searches a number on a predetermined vector; and answers "was found and which vector position" or answers that "the number is not in the vector". I wanted the explanation of why to use the boolean achou = false and of query if (!achou). I know the program has to display 2 results whether or not you found the number, but I didn’t quite understand what the use of the boolean and his denial in if are doing.

public static void main(String[] args) {

    int vetor[] = { 1, 3, 5, 7, 9};  // Cria o vetor com valores pré-definidos
    int numero;
    boolean achou = false;  // Variável para armazenar o resultado da procura

    System.out.println ("Qual número deseja procurar? ");       
    numero = Integer.parseInt(JOptionPane.showInputDialog(null,"Qual número deseja procurar?"));
    
    
    
    for (int posicao = 0; posicao < 5; posicao++)
    {
        if (vetor[posicao] == numero)
        {
            System.out.println ("Encontrado na posição: "+ posicao+ "\n");
            achou = true;  
        }
    }
    
    if (! achou) 
    {
        System.out.println ("O número não está no vetor\n");
    }
}

}

  • !achou is the same thing as achou == false, is just a fancy way of writing

  • That one achou exists because in the case of not having found it can only know after having traversed the whole vector. I agree that it is unnecessary, a break would eliminate this and solve an inefficiency (if ever thought about why keep going?). In fact (being a little annoying because I turn around and commit this sin) the design is not good, there should be a function that only travels and returns if you thought or not, and another that uses this to give this information to the user. The ! is the logic negation operator that comes from Boole algebra (optimal concept for a beginner in programming to study).

  • I spoke break? I meant return true. :P Logic gave a failure now rs

1 answer

2


First of all, that’s not a query.

Second, this code is inefficient, if it thought it should stop searching, and done efficiently and organized, so the search done in a separate function, this variable would not even be necessary.

This type of variable is called flag, that is, it only exists as a flag to indicate a state, and almost always has a better way of doing.

So the code only controls whether you could find what you were looking for or not. It starts by considering that you didn’t, so achou is false. When he finds (entered the if) then change the state of the variable (the ideal would be to stop the search right there), and changing the state the variable indicates that he found what he was looking for.

In the end he checks whether or not he found it and decides whether to present the message saying he did not find it. Obviously he should only enter this if if the achou is fake, but to get into a if the result must be true, so how to make a false turn true? Applying the boolean operator of NO (NOT), which is the exclamation, ie this operator reverses the value of the boolean variable. If you didn’t find it then he goes in there.

Browser other questions tagged

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