Selections making Decisions
Decision making and branching in ‘C’
- Depending on circumstances, ‘C’ language must be able to perform different sets of actions.
- A ‘C’ program is a set of statements which are normally executed sequentially, in the order in which they appear. This happens, when no options or no repetitions of certain calculations are necessary.
- A number of situations, where the order of statement execution, that is based on certain conditions is repeated, until certain specified conditions are met with.
- This involves decision making to identify, whether a particular condition has occurred or not and then direct a computer to execute certain statements.
Branching statements in ‘C’
- C programming has four decision making statements. They are:
- if…else statement
- switch statement
- conditional operator statement
- goto statement
Branching instructions in ‘C’
‘if’ statement
The general form of ‘if’ statement is given below:
if (condition)
{
code to be executed if condition is true
}
‘if’ statement is frequently used in decision making and allows the flow of execution to change, depending on the outcome of a condition,
The condition is an expression which should evaluate a logical value which is either ‘TRUE’ or ‘FALSE’.
In C, FALSE is represented by 0 (value zero) and TRUE is represented by non-zero (+ve or –ve).
Example: Sample program to display “How are you?”
#include<stdio.h>
int main(void)
{
int num;
printf(“enter a number less than 10”);
scanf(“%d”,&num);
if(num<10)
printf(“How are you?”);
return 0;
}
OUTPUT:
enter a number less than 10:
9
How are you?