TRUE OR FALSE

You are writing software code and into your code you put a condition, for example ‘if A is true or B is true then do X else do Y’.

You then test the code output by inputting various values, some will cause A to be TRUE and some will cause A to be FALSE, and you do the same with B. When comparing the output expected with the output the code has created, you have TRUE’s and FALSE’s everywhere so determining if your code is functioning correctly can quickly become confusing!

Emoji showing confused face

Emoji icons created by zafdesign – Flaticon

TRUE/FALSE table

The TRUE/FALSE table serves as a summary to help gauge the expected outcome of a AND/OR logical condition.

Example

Person A is 40 years old in 2022 being born on 1 January 1982.

AND

If Person A says they are 40 years old AND born in 1982. Then both sides of the ‘AND‘ sentence are true thereby making the whole statement true. Same applies in coding, if both sides of AND are TRUE then the whole statement is TRUE.

🌟TRUE AND TRUE is the only time AND will return TRUE.🌟

Person A says they are 21 years old AND born in 1950. Both sides of the ‘AND‘ condition are untrue so the overall statement is untrue. Same applies in coding, if both sides of AND are FALSE then the whole statement is FALSE.

Person A says they are 21 years old AND born in 1982. Is Person A 21 years old – no. The whole statement therefore is not true. It is irrelevant what is mentioned after that point in the sentence because as soon as you find an untruth, the whole statement is untrue. In coding the same applies, the program would go to the first expression and see that it is untrue (FALSE), it finds an AND condition, and as it returned FALSE to the first expression, it doesn’t need to read the other side of the AND – a FALSE with an AND makes the whole statement FALSE.

Person A says they are 40 years old AND born in 1950, again this is not true – as they were not born in 1950 – but in this case you need to read on past the ‘AND‘ before you find an untrue statement and can conclude the whole sentence to be untrue. Same applies in coding, this time the first expression, the age, is TRUE so the second part of the statement now needs to be considered. The year of birth is untrue (FALSE) thereby rendering the whole statement untrue (FALSE).

OR

If Person A says they are 21 years old OR born in 1950. Is Person A 21 years old – no. Was Person A born in 1950- no. So the whole sentence is untrue. The statement outputs FALSE when evaluated in code.

🌟FALSE OR FALSE is the only time OR will evaluate to FALSE.🌟

Person A says they are 40 years old OR born in 1982. Is Person A 40 years old – yes. Was Person A born in 1982 – yes. The whole sentence is true. The code evaluated outputs TRUE.

Let’s add more detail to our scenario to help with context for the next part. Say to qualify for an award, an individual either needs to be 40 years old OR born in 1982.

If Person A says they are 40 years OR born in 1950. Is Person A 40 years old – yes. Was Person A born in 1950 – no. Here, one of the two statements is true. Let’s look at qualification for an award, as the first part of the statement is true, Person A would qualify (TRUE). In coding, the first expression would be evaluated and found to be TRUE, the program would not continue onto the next part of the OR statement – it ceases execution as it has found a TRUE (remember it doesn’t matter whether or not the next part of the statement evaluates to TRUE or FALSE as returning TRUE for the first input to OR will result in an overall TRUE).

If Person A says they are 21 years OR born in 1982. Is Person A 21 years old – no. Was Person A born in 1982 – yes. Would Person A qualify for the award? The answer is yes; they would qualify as the second part of the statement is true. In coding, the first expression would be evaluated and found to be FALSE, the program would see the OR condition, and given that the first part is FALSE, the program would continue onto the next part of the statement. Here it would find the second expression to be TRUE making the overall output to be TRUE.

Think logically

The TRUE/FALSE input evaluated to TRUE/FALSE output is an area which can quickly become confusing.

My tip – in your native language write down in a sentence what you are trying to achieve making sure you include the relevant AND/OR conjunctions. Keep referring back to that sentence as you start testing your code to make sure you don’t loose sight of what you are trying to achieve.

Pin It on Pinterest

Share This