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 a- boolvariable. Recall- boolvariable takes either a- trueor- falsevalue. Note: if the- boolvariable is- false, the- trueblock of code will never be executed. If the- boolvariable is- true, the- falseblock 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 that- trueis stored as- 1, and- falseis stored as- 0as we discussed in Boolean section. To be more accurate, C is only strict in the representing- falseas 0. While- truecan be any non-zero number. In other words, any non-zero value in the condition makes the condition- true. While a zero value in the condition makes the condition- false.- 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 to- trueor- false. 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 returns- trueif they are equal and- falseotherwise. 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 of- if(x == 5), for example?- In - if (x = 5),- 5is assigned to- x, which returns- 5(recall Assignment operators). The condition here will always be- true, since the numerical value in place of the condition is- 5. This is not your intention indeed. Your intention is to check if- xis equal to- 5. To do this, you should write- if (x == 5).
3.1.2. What can we do with relational operators?#
Using relational operators, we can:
- Compare the values of - intand- doublevariables, 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 with- 5to see if they are equal, and
- Compare 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 - charand- intvalues, e.g.,- ('0' == 0)\(\rightarrow\)- falsesince- '0'has an ASCII code of 48, which is not equal to- 0.
Quiz
0 Questions
