As you’ve already discovered, C accommodates the standard logical conditions found in mathematics:
These conditions can be employed to execute various actions based on different decisions.
C offers the following conditional statements:
Utilize the if statement to define a block of code for execution when a condition is true.
if (condition) { // block of code to be executed if the condition is tru } |
Please note that “if” should be in lowercase letters. Using uppercase letters (such as If or IF) will result in an error. |
In the following example, we evaluate two values to determine if 20 is greater than 18. If the condition is true, output some text:
if (20 > 18) { printf(“20 is greater than 18”); } |
Variables can also be tested:
int x = 20; |
In the example provided, we utilize two variables, x and y, to evaluate whether x is greater than y (employing the > operator). Given that x is 20 and y is 18, and considering that 20 is indeed greater than 18, we display the message “x is greater than y” on the screen.