Why boolean are popularly used in conditional statements




















Boolean logic combines multiple statements that are either true or false into an expression that is either true or false. Suppose M is the set of all mystery books, and C is the set of all comedy books. Notice here that or is not exclusive. This is a difference between the Boolean logic use of the word and common everyday use. Suppose we are searching a library database for Mexican universities.

Express a reasonable search using Boolean logic. To account for this, we could revise our search to read:. In most internet search engines, it is not necessary to include the word and ; the search engine assumes that if you provide two keywords you are looking for both. Quotes around a phrase indicate that the entire phrase should be looked for. The search from the previous example on Google could be written:. Sometimes statements made in English can be ambiguous. For this reason, Boolean logic uses parentheses to show precedent, just like in algebraic order of operations.

The last grouped conditions tell us to find elements of this set that are also either a multiple of 3 or a multiple of 5. When combined with the last condition, though, this set expands without limits:. Be aware that when a string of conditions is written without grouping symbols, it is often interpreted from the left to right, resulting in the latter interpretation.

Beyond searching, Boolean logic is commonly used in spreadsheet applications like Excel to do conditional calculations. A statement is something that is either true or false.

When an action is taken or not depending on the value of a statement, it forms a conditional. A statement is either true or false. If the condition is true, then we will follow the first course of action, and go to the mall. If the condition is false, then we will use the alternative, and go for a hike.

Conditional statements are commonly used in spreadsheet applications like Excel. In Excel, you can enter an expression like. Notice that after the IF, there are three parts. The first part is the condition, and the second two are calculations. However, it is left associative in PHP. Associativity refers to the direction in which the operation is performed. To avoid errors of this type, parentheses should be used to specify operator precedence. This code sets Discount to 0.

A more efficient way to write this instruction would be to use a conditional operator, as below:. A nested if-then-else statement could be used to accomplish the task, as follows:. Conditional operators provide an alternative to the if-then-else construct that can reduce the number of lines of code needed to create branching logic, particularly in complicated nesting scenarios. This reduces the time needed to write, debug, and maintain code and makes the code easier to understand. The time saved can be significant, as conditional expressions are among the most common constructs used in computer programming.

Branching logic is at the core of many algorithms, making it a core capability required of any computer language. Thus, conditional operators will continue to be relevant as new computer languages are developed and existing languages are improved. Friedman, Daniel P. Essentials of Programming Languages.

Haverbeke, Marijn. MacLennan, Bruce J. Schneider, David I. Van Roy, Peter, and Seif Haridi. Concepts, Techniques, and Models of Computer Programming. MIT P, In many programming languages, the conditional operator uses the following syntax:. However, using a conditional operator could reduce the lines of code required from nine to one:.



0コメント

  • 1000 / 1000