If-statements
Contents
3.1. If-statements#
Let’s develop a decision-making program! We want to develop a program that prompts the user to enter their age. If it was below the legal age to work in Ontario, Canada, the program prints “You are not yet eligible to work in Ontario.”, else it prints “You are eligible to work in Ontario.”
In C, we use the if statement to make decisions. The if statement is a conditional statement that executes a block of code if a condition is true. The syntax of the if statement is as follows:
if (condition) {
// code to execute if condition is true
}
If we want to execute another block of code if the condition is false. We can have an else to the if-statement. The syntax of the if-else statement is as follows:
if (condition) {
// code to execute if condition is true
} else {
// code to execute if condition is false
}
3.1.1. What can this condition be?#
The
conditioncan be aboolvariable. Recallboolvariable takes either atrueorfalsevalue. Note: if theboolvariable isfalse, thetrueblock of code will never be executed. If theboolvariable istrue, thefalseblock of code will never be executed.Code
#include <stdbool.h> #include <stdio.h>
int main(void) { bool flag = true; if (flag) { printf("The flag is true."); } else { printf("The flag is false."); } return 0; }The
conditioncan be a numerical value. Recall thattrueis stored as1, andfalseis stored as0as we discussed in Boolean section. To be more accurate, C is only strict in the representingfalseas 0. Whiletruecan be any non-zero number. In other words, any non-zero value in the condition makes the conditiontrue. While a zero value in the condition makes the conditionfalse.Code
#include <stdio.h>
int main(void) { if (3) { printf("The condition is true."); } else { printf("The condition is false."); } return 0; }The
conditioncan be a “relational expression” that evaluates totrueorfalse. Relational expressions have relational operators summarized in the table below.Relational Operator
Meaning
==Equal to
!=Not equal to
<Less than
>Greater than
<=Less than or equal to
>=Greater than or equal to
For example, the code below is a program that prompts the user if they are eligible to work in Ontario, based on their age. Download
eligible-age.cto get the following code.Code
#include <stdio.h> int main(void) { int age = 0; printf("Enter your age: "); scanf("%d", &age);
if (age < 14) { // Condition checking if age is less than 14 printf("You are not yet eligible to work in Ontario."); } else { printf("You are eligible to work in Ontario."); } return 0; }Another example, let’s write a program in C that identifies if a shape is rectangle or square based on the two sides given by the user. Download
square-rectangle.cto get the following code.Code
#include <stdio.h> int main(void) { int height = 0, width = 0; printf("Please enter the height and width of your shape: "); scanf("%d %d", &height, &width);
if (height == width) { printf("The shape is a square."); } else { printf("The shape is a rectangle."); } return 0; }Equal to \(==\) Vs. Assignment \(=\)
One of the most common mistakes is that people confuse the relational operator
==with the assignment operator=. The relational operator==compares the right hand side with the left hand side and returnstrueif they are equal andfalseotherwise. The assignment operator=assigns the value on the right hand side to the variable on the left hand side.What would happen if you got confused and wrote
if (x = 5)instead ofif(x == 5), for example?In
if (x = 5),5is assigned tox, which returns5(recall Assignment operators). The condition here will always betrue, since the numerical value in place of the condition is5. This is not your intention indeed. Your intention is to check ifxis equal to5. To do this, you should writeif (x == 5).
3.1.2. What can we do with relational operators?#
Using relational operators, we can:
Compare the values of
intanddoublevariables, e.g.(3 >= 2)or(7.2 > 5.1)or(-3.2 <= 1),Mix arithmetic and relational operators, where arithmetic operations have higher precedence, e.g., in
(x + 2 == 5),x + 2is evaluated first and compared with5to see if they are equal, andCompare the values of two
charvariables, where the ASCII codes of the characters are compared, e.g.,'a' < 'b'\(\rightarrow\)truesince the ASCII code of'a'is lower than'b'.Compare the values of
charandintvalues, e.g.,('0' == 0)\(\rightarrow\)falsesince'0'has an ASCII code of 48, which is not equal to0.
Quiz
0 Questions