2.conditional & Loops
2.conditional & Loops
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)
printf("Order received.\n");
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;
}
Nested if Statements
An if statement can include another if statement to form a nested statement. Nesting
an if allows a decision to be based on further requirements. Consider the following
statement:
if (profit > 1000)
if (clients > 15)
bonus = 100;
else
bonus = 25;
Appropriately indenting nested statements will help clarify the meaning to a reader.
However, be sure to understand that an else clause is associated with the closest if
unless curly braces { } are used to change the association. For example:
if (profit > 1000) {
if (clients > 15)
bonus = 100;
}else
bonus = 25;
Logical Operators
Logical operators && and || are used to form a compound Boolean expression that tests
multiple conditions. A third logical operator is ! used to reverse the state of a Boolean
expression.
The || Operator
The logical OR operator || returns a true result when any one expression or both
expressions are true. For example:
if (n == 'x' || n == 'X')
printf("Roman numeral value 10.\n");
Any number of expressions can be joined by && and ||. For example:
if (n == 999 || (n > 0 && n <= 100))
printf("Input valid.\n");
Parentheses are used for clarity even though && has higher precedence than || and
will be evaluated first.
The ! Operator
The logical NOT operator ! returns the reverse of its value. NOT true returns false, and
NOT false returns true. For example:
if (!(n == 'x' || n == 'X'))
printf("Roman numeral is not 10.\n");
In C, any non-zero value is considered true and a 0 is false. The logical NOT operator
therefore, converts a true value to 0 and a false value to 1.