 ## Conditionals

Conditionals are used to perform different computations or actions depending on whether a condition evaluates to true or false.

The if Statement
The if statement is called a conditional control structure because it executes statements when an expression is true. For this reason, the if is also known as a decision structure. It takes the form:

if (expression)
statements

The expression evaluates to either true or false, and statements can be a single statement or a code block enclosed by curly braces { }.
For example:

#include <stdio.h>

int main() {
int score = 89;

if (score > 75)
printf(“You passed.\n”);

return 0;

In the code above we check whether the score variable is greater than 75, and print a message if the condition is true.

## Relational Operators

There are six relational operators that can be used to form a Boolean expression, which returns true or false:
<    less than
<=  less than or equal to
>    greater than
>=  greater than or equal to
==  equal to
!=   not equal to

For example:

int num = 41;
num += 1;
if (num == 42) {
printf(“You won!”);

An expression that evaluates to a non-zero value is considered true.
For example:

int in_stock = 20;
if (in_stock)

NOTE!
Try out the outputs yourself using a code editor that is compatible with the C language.

## The if-else Statement

The if statement can include an optional else clause that executes statements when an expression is false.
For example, the following program evaluates the expression and then executes the else clause statement:

#include <stdio.h>

int main() {
int score = 89;

if (score >= 90)
printf(“Top 10%%.\n”);
else
printf(“Less than 90.\n”);

return 0;

NOTE!
Try it out using a code editor that is responsive to C language.

## Conditional Expressions

Another way to form an if-else statement is by using the ?: operator in a conditional expression. The ?: operator can have only one statement associated with the if and the else.
For example:

#include <stdio.h>

int main() {
int y;
int x = 3;

y = (x >= 5) ?  5 : x;

/* This is equivalent to:
if (x >= 5)
y = 5;
else
y = x;
*/

return 0;

NOTE!
Test the line of codes using a code editor that is responsive to C language.

 BACK NEXT