07 - Conditionals & Logic Operators

Conditionals are likely the single most important part of programming. Without them, any code you write would basically run exactly the same every time and there would be no interaction.

Boolean Values

The C programming language does not have any built in Boolean values of "True" or "False" like Python. C does not even have a True or False Boolean data type, instead all of the conditional statements run basd on numerical values. C absolutely evaluates statements such as "5 > 3" as True or False, but it uses the below numerical values to indicate whether the statement is true or false.

True - Any number other than zero (usually 1, but could be any number other than zero)

False - Zero

Later on, if you are so inclined, there are many ways to define "True" and "False" in C. Possibly if we have time we could later touch on how to do this.

Boolean Operators

The first thing we need to establish, is which operators the C programming language has and how we can express them. For your reference, I included their analagos coding in Python which is for the most part the same or very similar.

IF, ELSE and ELSE IF

if and else are the foundations of computer programming. Simply put, an IF statement says: IF (this condition is true) then (run this code). Below is an example of a simple if statement written in C.

Example:

if ( a > b ) {

printf("a is greater than b\n");

}

Now there are a few notes about this:

Three Important Notes:

    • The line of code with IF (condition) does not have a semicolon after it.

    • The condition has to go in a set of parenthesis to clarify that this is the entire condition from start to finish.

    • Although a single line of code after the IF statement COULD be written without brackets, it is a good programming practice to always use brackets and indentation to organize your code.

Below is an example of an IF statement followed by an ELSE statement. And important note about this is that ELSE cannot be followed by a (condition). This will give you an error saying it expected a bracket "{". The ELSE is a catch-all basket that takes care of everything left over after the IF conditional

Example:

if ( a > b ) {

printf("a is greater than b\n");

}

else {

printf("a is not greater than b\n");

}

Two Important Notes:

  • The line of code with an ELSE statement does not have a semicolon after it

  • An ELSE statement does not have a condition in parenthesis after it. An ELSE statement catching everything else that did not trigger the original IF statement.

Another note about this, is that the ELIF in Python was actually reduntant. It makes the code more readable, but is logically equivalent to "else if". Think about it. Below is an example of an IF, ELIF, ELSE code.

Example:

if ( a > b ) {

printf("a is greater than b\n");

}

else if ( a < b ) {

printf("a is less than b\n");

}

else {

printf("a is equal to b\n");

}

Below is this code run with some simple user input. Play around with it.

Activity 1

Write a program that asks the user for an integer.

If the integer is even, print "The number <number> is even"

If the integer is odd, print "The number <number> is odd"

Solution: Link

Activity 2

Write a program that asks the user for three integers.

Print the greatest number of the three in a complete sentence.

Solution: Link

Activity 3

Write a program that asks the user for their age (integer).

Respond to the user's age with a fun comment - have at least 4 possible responses.

Note - No matter what number the user enters, they should receive a response.

Example: If they enter 3, respond with "Wow you are really young. You know how to use a computer?!"

If they enter your age, respond with "We're the same age!"

If they enter 80, respond with "Wow you're old"

If they enter a number over 200, respond with "You must have seen a lot over the centuries"

Solution: Link

No ELIF?!

Python had this wonderful ELIF - How can we survive without it?! It is actually exactly, logically equivalent to using ELSE IF. Below are three examples in four different languages (C and Java use the same syntax).

Snap!

Python

C and Java

Common Mistakes

Explanations coming soon

    1. Ending the "if (condition)" line of code with a semicolon rather than starting a set of brackets. This semicolon ends the IF conditional, essentially saying "if (condition is true) then (do nothing)". All of the lines of code inside the brackets then run regardless of whether or not the IF condition was true or false. - Example of this

    2. Misunderstanding the previous common mistake, and thinking that statements inside an IF statement brackets do not need semi-colons. Every command written in C needs a semicolon. The IF (condition) line doesn't need a semicolon because it isn't actually a command. It isn't running any code, it is deciding which commands to run. Look at the previous example

    3. Putting a condition after the ELSE statement. An ELSE must immediately follow and IF conditional and simply collects everything for which the IF condition was false. If you want to check a second condition, then use "ELSE IF (condition)". Example 1 (Broken, condition after the ELSE). Example 2 (Working with ELSE IF).

    4. Writing a chain of conditional checks with AND or OR, without parenthesis denoting an order of operations. The "AND" and "OR" operators can only be used between exactly two values. If you use 3 or more (example: True OR False AND True), some languages will give you errors while some languages will evaluate them differently than you expect. For example, the statement "True OR False AND False". If you place parenthesis around the the "OR" operation it becomes (True OR False) AND False which evaluates to True AND False which gives a final value of False. If you place parenthesis around the "AND" operation it becomes True OR (False AND False) which evaluates to True OR False which gives a final value of True. This is analogous to the order of operations in mathematics and the math expression 3 - 5 + 2. Without order of operations, this could be (3-5)+2 = (-2)+2 = 0 ... or ... 3-(5+2) = 3-7 = -4. Always use parenthesis so that each "AND" and "OR" operator is between exactly two values to be unambiguously clear (and not cause an error!). Example correct way to format it: ( True OR (False AND False) ) or ( ( True OR False ) AND False )

    5. Having more than one set of parenthesis after an IF. Example: if (blank1) && (blank2) {do this}. This will cause an error because the program reads it as "IF (blank1) then do '&&...' " ... To correctly write the previous statement, you need a single parenthesis around all of the conditions such as: if ( (blank1)&&(blank2) ) {do this}. Example of this

    6. Forgetting brackets. If you have an IF statements without brackets, the first line following the IF statement will be controlled by the IF statement, all of the lines after this will run regardless of whether the IF condition is true. If you later have an ELSE statement, you will get an error because an ELSE statement has to immediately follow an IF statement. Example of this

    7. Messing up with ELIF/ELSE-IF logic - Using IF, IF, IF, ELSE instead of IF, ELSE-IF, ELSE-IF, ELSE. This is not so much a programming error as a logical error. An ELSE is only ever referring to the IF condition immediately before it. If you are to use a string of IF statements culminating in a single ELSE statement, either the final IF or final ELSE will be triggered, regardless of what came before. Example of this (not working). Instead, if you only want a single one of the statements to be triggered, as in a test grade is either an A or B or C or D or F and never two of the grades, then you will change all of the IF statements following the first one to be ELSE IF. Example of this (fixed).