learnprogramming123

LEARN PROGRAMMING 123
your resources to programming and free tutition



Navigation

 
C
 
C++ soon here!

Search

 

 

 

 

Next Back

 

Lesson 5: Decision structures

This lesson will show you how to:

  • Use an if statement
  • Use an else statement
  • Use an else if statement

    Q. What do a blonde and President Gorbachev have in common?
    A. They both get ****** by eight men while on holiday.
    Q. What's the difference between Gorbachev and a blonde ?
    A. Gorby knows the names of the eight people that ****** him !

In previous tutorials, all the code in the examples executed, that is, from the first line of the program to the last, every statement was executed in the order it appeared in the source code (from top to bottom). This may be correct for some programs, but others need a way to choose which statements will be executed or run. Conditional processing extends the usefulness of programs by allowing the use of simple logic or tests to determine which blocks of code are to be executed. In this lesson, a sentence will be printed stating whether a candidate has passed his exam or failed it, depending on the grade which he obtained.

The if statement is used to conditionally execute a block of code based on whether a test condition is true. If the condition is true the block of code is executed, otherwise it is skipped.

#include <stdio.h>

int main(void)
{

    int number = 75;
    int mark;

    printf("Your examination mark\n");
    printf("Enter your score, please\n");
    scanf("%d",&mark);

    if (mark >= number)
    {
        printf("Incredible, you passed with a merit\n");
    }

    return 0;
}

Please try compiling and executing the above. The "==" is called a relational operator. Relational operators, ==, !=, >, >=, <, and <=, are used to compare two values.

The else statement provides a way to execute one block of code if a condition is true, another if it is false.

#include <stdio.h>

int main(void)
{

    int number = 75;
    int mark;

    printf("Your examination mark\n");
    printf("Enter your score, please\n");
    scanf("%d",&mark);

    if (mark >= number)
    {
        printf("Incredible, you have passed with a merit\n");
    }
    else
    {
        printf("You failed, unlucky\n");
    }

return 0;
}

This is a big improvement. Regardless of whether the examination is passed or not, the user gets some response. But what if the marking scheme also had another category. What if the user obtained a grade higher than pass, e.g. a merit or distinction. C has an if/else if construct that can be used to implement this functionality in our program.

#include <stdio.h>

int main(void)
{

    int number = 75;
    int mark;

    printf("Your examination mark\n");
    printf("Enter your score, please\n");
    scanf("%d",&mark);

    if (mark >= number)
    {
        printf("Incredible, you have passed with a merit\n");
    }
    else if (mark >= 65)
    {
        printf("You have passed\n");
    }
     else
    {
    printf("You have failed\n");
    }

    return 0;
}

It is interesting that note that there is no C keyword "else if", as may exist in other languages. For instance, Pearl has a keyword "elsif", visual basic has a keyword else if. The if/else if construct is created out of if and else statements.

back to top        go to next lesson


 






1