If Statements

The ability to control the flow of your program, letting it make decisions on what code to execute. The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false.

Example 1

#include <stdio.h>    
 
int main()                            /* Most important part of the program!
*/
{
    int age;                          /* Need a variable... */
 
    printf( "Please enter your age" );  /* Asks for age */
    scanf( "%d", &age );                 /* The input is put in age */
    if ( age < 50 ) 
    {                  /* If the age is less than 100 */
        printf ("You are pretty young!\n" ); /* Just to show you it works... */
    }
    else if ( age == 100 ) 
   {            
     printf( "You are old\n" );       
   }
  else {
    printf( "You are really old\n" );     /* Executed if no other statement is
    */
  }
  return 0;
}
Unless otherwise stated, the content of this page is licensed under Creative Commons Attribution-Share Alike 2.5 License.