Programming in C: If Statements

During the last tutorial we took a look at incrementing and decrementing values, today we will look at using if statements. An if statement is a decision, if the condition is true, the the statement in the block will execute.

Before we start looking at the format of an if statement, here are some relational operators you need to know:

> - greater than (10 > 4 is TRUE)
< - less than (4 < 8 is TRUE)
>= - greater than or equal (4 >= 4 is TRUE)
<= - less than or equal (1 <= 4 is TRUE)
== - equal to (11 == 11 is TRUE)
!= - not equal to (5 != 84215 is TRUE)

If Statements

The general structure of an if statement is as follows:

if (condition)
{
/* Execute statements inside the braces. */
}

Here is an example of an if statement that is shown with the correct syntax:

if (4 < 7)
{
printf("4 is greater than 7, surprised?");
}

An if statement can have multiple parts/conditions, which we can write using else and else if statements. The else if statement takes another condition and executes what is in that block if that condition is true. The else statement, on the other hand, will execute if all the above if and/or else if statements fail, or evaluate to false.

Here is an example of a program that uses if, else if and else statements along with some previous topics we have covered:

#include <stdio.h>	

int main() 
{
 int age; 
 
 printf("Enter your age: "); 
 scanf( "%d", &age );

 if ( age < 100 ) 
 {
 printf ("You are pretty young!\n" );
 }
 else if (age == 100) 
 {
 printf("You are old\n"); 
 } 
 else 
 {
 printf("You are really old\n");
 }
 
   return 0;
}

Boolean Operators

Boolean operators allow you to create more interesting and complex if statements by using AND, OR, or NOT.

NOT: The NOT operator accepts one input. If that input is TRUE, it returns FALSE, and if that input is FALSE, it returns TRUE. NOT returns the negation of the previous state.

AND: AND returns TRUE if both inputs are TRUE (if 'this' AND 'that' are true). (1) AND (0) would evaluate to zero because one of the inputs is false (both must be TRUE for it to evaluate to TRUE). Written as && in C. Keep in mind that the AND operator is evaluated before the OR operator because it has a higher precedence. If you need the OR operator to evaluate first use brackets.

OR: If either (or both) of the two values it checks are TRUE then it returns TRUE. For example, (1) OR (0) evaluates to 1. (0) OR (0) evaluates to 0. The OR is written as || in C. Those are the pipe characters. On your keyboard, they may look like a stretched colon. On my computer the pipe shares its key with \. The OR operator will be evaluated after AND.

Test your knowledge by trying these three questions below. Leave your answer in the comments.

1. !( 1 || 0 ) 
2. !( 1 || 1 && 0 )
3. !( ( 1 || 0 ) && 0 )

More tutorials coming soon! Let me know how you like the tutorials by commenting below and if there is any specific topics or languages you would like covered. Follow me on Twitter to keep up-to-date on all the latest news and tutorials. 

Programming in C: For Loops

Programming in C: Increment and Decrement