Technical Training
C TutorialsTable of Contents
C Programming - Decision Making - Branching
C Programming - Decision Making - Branching - Page 2
C Programming - Decision Making - Branching - Page 3C Programming - Decision Making - Branching
“Decision making” is one of the most important concepts of computer programming. Programs should be able to make logical (true/false) decisions based on the condition they are in; every program has one or few problem/s to solve; depending on the nature of the problems, important decisions have to be made in order to solve those particular problems.
In C programming “selection construct” or “conditional statement” is used for decision making. Diagram 1 illustrates “selection construct”.

Diagram 1 simple selection construct
Conditional statement is the term used by many programming languages. The importance of conditional statements should not be ignored because every program has an example of these statements. “IF statement” and “switch statement” are the most popular conditional statements used in C.
Branching
Branch is the term given to the code executed in sequence as a result of change in the program’s flow; the program’s flow can be changed by conditional statements in that program. Diagram 2 shows the link between selection (decision making) and branching (acting).

Diagram 2 "branch" depends on the condition of the selection code
Branching is the process of choosing the right branch for execution, depending on the result of “conditional statement”.
If Statement
“If statement” is the selection statement used to select course of action depending on the conditions given. Therefore programmers can use this statement to control the flow of their program. If the program’s condition matches “if statement” condition then the code will be executed otherwise it will be ignored and the rest of the program will be executed.
Definition:
- if (condition) { code here if “true”}
Tips :
{ } can be removed if only one line of code is to be executed. For example:
- If(a==1)
- Printf(“1”);
- Instead of:
- If(a==1)
- { printf(“1”); }
Example:
consider the case when we are asked to write a program for exforsys.com where users are asked to give ratings on each article; we want to output different messages depending on the ratings given. Messages are as follow: 1 is bad, 2 is average and 3 is good.
Code :
- int rate;
- printf("Please Enter Rating? 1 for bad , 2 for average and 3 for goodn");
- scanf("%d",&rate);
- if(rate==1)
- {
- printf("nit is poor");
- }
- if(rate==2)
- {
- printf("nit is average");
- }
- if(rate==3)
- {
- printf("nit is good");
- }

Figure 1 code 1 screen shot
Description of “code 1”: this code asks user to input in first 3 lines, once the user inputs an integer the program will then checks all the “if statements”. If any of them match the input “rate” then the output for that statement will be printed out. Diagram 3 shows the function of the code 1.

C Tutorials
- C Programming - An Overview
- C Programming - Data Types : Part 1
- C Programming - Data Types : Part 2
- C Programming - Constants and Identifiers
- C Programming - Operators
- C Programming - Expressions
- C Programming - Managing Input and Output Operations
- C Programming - Decision Making - Branching
- C Programming - Decision Making - Looping
- C Programming - Arrays
- C Programming - Handling of Character String
- C Programming - Functions (Part-I)
- C Programming - Functions (Part-II)
- C Programming - Structures and Unions
- C Programming - Pointers
- C Programming - Dynamic Memory allocation
- C Programming - Linked Lists
- C Doubly Linked Lists
- C Circular Linked Lists
- C Programming - File management in C
- C Language - The Preprocessor
- Call by Value and Call by Reference
- Concept of Pixel in C Graphics
- TSR in C - An Introduction







