Login

Username:

Password: 

Join us Now |  Forgot Password? | Forgot UserName?

C

Learn step by step



If statements

The ability to control the flow of your program, It make decisions what code to be execute in a particular condition, It is very valuable to the programmer. if statement allows you to control if a program enters a section of code or not based on a given condition is true or false. One of the most important functions of if statement is that it allows the program to select an action based upon the user's input. For e.g, when using an if statement to check a user-entered password, program can decide whether a user is allowed access the program or not.

Without a conditional statement such as the if statement, programs would run almost same way every time, and always follow the same sequence of function calls. The if statements allow the flow of the program to be changed, This leads to more interesting and valuable code.

Before discussing the actual structure of the if statement, let us examine the basic meaning of TRUE and FALSE in computer terminology. In technological term a true statement is one that evaluates to a nonzero number. And a false statement evaluates to zero number. When you perform any comparison with the relational operators, these operators will return 1 if the comparison is true, or will return 0 if the comparison is false. For e.g., the check 0 == 2 evaluates to 0. And check 2 == 2 evaluates to a 1. If this confuses you, try to use a print statement to output the result of those various comparisons (for example printf( "%d", 2 == 1 );)

When programming, the program will often require the checking of one value stored by a variable against another value to determine whether one is larger, smaller, or equal to the other.

Basic If Syntax

Structure of an if statement:

if ( statement is TRUE )
Execute this line of code

Given an example that shows the syntax:

if ( 5 < 10 )
printf( "hello TUTON, Five is less than ten" );

Here, we're just evaluating the statement, "hello TUTON, Five........", to see if it is true or not; You can also write your own full program including stdio.h (and other header files also) and put this into the main function and run it to test.

To have more than one statement execute after an if statement, use braces, like we did with the body of main function. Anything inside these braces are called a compound statement, or a block. When we are using an if statement, the code that depends on the if statement is called the body of the if statement(if statement body).

We recommend you always putting braces following if statements. If you do this, you will never have to remember to put them in when you want more than one statement to be executed, and also you make the body of the if statement more visually clear.

Else

Sometimes when the condition in an if statement evaluates to false, it would be nice to execute some block of code instead of the code executed only when the statement evaluates to true. The "else" statement effectively says that whatever code after it (whether a single line or code between brackets) is executed if the if statement is FALSE.

if ( TRUE ) {
/* Execute these statements if TRUE */
}
else {
/* Execute these statements if FALSE */
}
Else if

Another use of else is when there are multiple conditional statements that may all evaluate to true, yet we want only one if statement's body to be executed. You can use an "else if" statement following an if statement and its body; that way, if first statement of the else if ladder is true, the "else if" will be ignored, but if the statement is false, it will then check the condition of the below else if statement. If the first if statement was true the else statement will not be checked. It is also possible to use numerous else if statements to ensure that only one block of code is executed.


#include <stdio.h>
int main() /* Most important part of the program! */
{
int age = 63; /* Need a variable... */
if ( age < 60 ) { /* If the age is less than 100 */
printf ("You are pretty young!\n" ); /* Just to show you it works... */
}
else if ( age > 60 ) { /* I use else just to show an example */
printf( "You are old\n" );
}
else {
printf( "You are really old\n" ); /* Executed if no other statement is */
}
return 0;
} RUN



Related Videos