When you deal with data, you may want to do a specific processing in case that a it meets a certain condition. For example, the processing depend on whether a person is older than 20 years or not. In order to realize such function, if can be used.
Fundamental structure of if-statement
if (condition) { statement to be executed when condition is true (multi-line statements are possible to list) }
When condition is satisfied, it can be expressed as true. On the other hand, when it is not satisfied, false can be used. In the case of "true", value "1" is returned, and value "0" is returned in "false" case.
In the below, operators are listed, which are often used in conditional equations.
Operator | Usage | Meaning |
---|---|---|
== | a == b | if value of a is equal to value of b, 1 will be returned. Otherwise, 0 will be returned |
!= | a != b | if value of a is not equal to value of b, 1 will be returned. Otherwise, 0 will be returned |
< | a < b | if value of a is less than value of b, 1 will be returned. Otherwise, 0 will be returned |
> | a > b | if value of a is more than value of b, 1 will be returned. Otherwise, 0 will be returned |
<= | a < b | if value of a is less than or equal to value of b, 1 will be returned. Otherwise, 0 will be returned |
>= | a >= b | if value of a is more than or equal to value of b, 1 will be returned. Otherwise, 0 will be returned |
Examples of if-statement
if (a > b) { printf("a is more than b\n"); }
if (a < b) { printf("a is less than b\n"); }
if (a == b) { printf("a is equal to b\n"); }
if (a != b) { printf("a is not equal to b\n""); }
We can set different conditions in a series of processing.
if (condition) { statement to be executed when condition is true (multi-line statements are possible to list) } else { statement to be executed when condition is not true (multi-line statements are possible to list) }
This can be extended to complex conditional equations
if (condition A) { statement when condition A is true } else if (condition B) { statement when condition B is true } else { statement when conditions A and B are false }
Assume that we have a variable "age" which represents age, and that we want to have conditional equations as follows.
Conditional expression | Meaning |
---|---|
A | older than 60 years old |
B | older than 20 years old and younger than 60 |
Others | younger than 20 years old |
In order to realize above condtional expressions, we can write as follows.
if (age >= 60) { statement when age is older than or equal to 60 } else if (age >= 20) { statement when age is older than or equal to 20 and younger than 60 } else { statement when age is younger than 20 }
if (conditional equation A &&conditional equation B) { statement when both conditional equation A and B are satisfied }
if (conditional equation A || conditional equation B) { statement when either conditional equation A or B is satisfied }
For example, when we want to consider both age and sex (e.g. 1 for male and 2 for female), below expression is possible
if (age >= 60 && sex == 1) { statement when age is older than or equal to 60 and sex is male (1) }
if (age >= 60 || sex == 1) { statement when age is older than or equal to 60 or sex is male (1) - female whose age is older than or equal to 60, and - male whose age is younger than 60 are also "true" for these conditional equations }