Tutorial 5.1: if Selection Structure

Introduction
Up until now, we have been writing a program that executes the statements one by one until the defined end. This type of program is called sequential structure. The functionality of this type of program is limited since it flows in single direction. Now we will be looking at one of the most powerful programming feature - the ability to change the flow of program execution. It is achieve by establishing the truth or falsity of an expression (condition). This type of program is called selection structure.

Selection structure is extensively used in programming because it allows the program to decide an action based upon user's input or other processes. The application of selection structure can be seen at systems that have password checking such as ATM Machine, handphone and etc.. The system allows user access only if the user input password is the same as the corresponding password stored in the system. In C, there are three types of selection structure can be implemented, namely if selection statement, if else selection statement and switch selection statement.

Before we go further into the selection structure, you need to know the meaning of True and False in programming. An expression that evaluates to non-zero (1) is considered true expression while false expression evaluates to zero (0). Refer to example of Tutorial 4

if Selection Statement
if is the primary tool of selection structure. if is used when we want a single or a group of statements to be executed if the condition is true. If the condition is false, the statement is skipped and program continues by executing the first statement after the if selection structure. The flowchart illustrates the function of if selection.


Lets look at an example that implement if selection structure. The program asks the user to input a number between -10 and 10. Then it message if the number is a positive number.
#include <stdio.h>

int main()
{
 int num;
 printf("Enter a number between -10 and 10: ");
 scanf("%d", &num);
 if (num > 0)
  printf("%d is a positive number\n", num);
 return 0;
}

Sample output:


Enter a number between -10 and 10: 6
6 is a positive number


Explanation:
int num;
printf("Enter a number between -10 and 10: ");
scanf("%d", &num);
We declare a variable called num and then we prompt the user to enter a number between -10 and 10. The scanf statement reads the user input and store it in variable num.
if (num > 0)
 printf("%d is a positive number\n", num);
The if statement compare the value in num with the value of 0. If num is greater than 0, the printf() will be executed which displays a message and continue with the next part of the program. If it is not greater than 0, the printf() will be skipped.

Next: Extending the if Statement

3 comments:

Unknown said...

thanks, it's really a big help for my assignment...

Unknown said...

yeah this basically saved my life lol...awesome examples!

khairie said...

nice example there. it's helped me so much.

Post a Comment