What Is the Purpose of a Boolean Operator?

What Is the Purpose of a Boolean Operator? thumbnail
Boolean operators help programs to respond to user interaction.

Computer programmers and Web developers use boolean operators to control the flow of execution in their sites and applications. Many programming languages use boolean variables and values. Conditional statements often use boolean operators to carry out tests on program data and the results of these tests determine what should happen at any particular point during execution. Boolean operators allow programmers to create complex chains of testing in their scripts.

  1. Boolean Values

    • Boolean values can only be either true or false. Most programming languages support boolean variables, as in the following JavaScript example:
      var isEven = false;

      Boolean variables often have names that indicate their purpose. For example, this variable could indicate whether a number input value is even, with a value of true if it is, false if it is not. Boolean variables and values give programmers a tool for handling unpredictable situations such as user input and imported data.

    Control Flow

    • Boolean operators and values are often involved in control flow. Control flow relates to programming language structures. Rather than a script executing a line at a time from the beginning of a file to the end, control structures let programmers dictate varied flows of execution to suit particular circumstances. For example, if a user enters data for an application function but the data is not valid for some reason, the application may need to prompt the user to adjust their input. A boolean test could check the input for validity. If the input is not valid, the program needs to carry out a different process.

    Conditionals

    • Programming languages use boolean operators as part of conditional statements. The following sample PHP code demonstrates a conditional test:
      if(strlen($user_name)>20) echo "Name is too long";

      This test checks the length of a string variable. If the length of the string is greater than 20, the content of the statement will execute. This means that the conditional test returned a true value. If the string is not longer than 20 characters, the test returns a false value.

    Syntax

    • Boolean operators allow programmers to chain conditional tests together. The following sample Java code demonstrates:
      if(someNumber<10 || otherNumber>5)

      This test will return true if either of the tests in it return true, as they are chained using the "or" operator. To test for both conditions being true, the program could use the following amended version:
      if(someNumber<10 && otherNumber>5)

      This test uses the "and" operator to chain the smaller tests. The test as a whole will only return true if both of these return true. To test if two numbers are equal, the following code applies:
      if(someNumber==otherNumber)

      To test for a number being either greater than or equal to another, the following syntax can be used:
      if(someNumber>=otherNumber)

      To test whether something is not the case, the program can use the following syntax:
      if(!someNumber<10)

      Programmers can chain together as many tests as they need.

Related Searches:

References

Resources

  • Photo Credit Jupiterimages/Pixland/Getty Images

Comments

Related Ads

Featured