Exforsys

Home arrow Technical Training arrow C Tutorials

C Programming - Decision Making - Branching

Page 1 of 3
Author: Exforsys Inc.     Published on: 4th Apr 2006    |   Last Updated on: 28th Aug 2011

“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.

Ads

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:

Sample Code
  1.   if (condition) {   code here iftrue}
Copyright exforsys.com


Tips :

{ } can be removed if only one line of code is to be executed. For example:

Sample Code
  1. If(a==1)
  2.         Printf(1);
  3.  
  4. Instead of:
  5.  
  6. If(a==1)
  7.         { printf(1); }
Copyright exforsys.com


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 :

Sample Code
  1. int rate;
  2.  
  3. printf("Please Enter Rating? 1 for bad , 2 for average and 3 for goodn");
  4. scanf("%d",&rate);
  5.       if(rate==1)
  6.         {
  7.         printf("nit is poor");
  8.         }
  9.       if(rate==2)
  10.         {
  11.                 printf("nit is average");
  12.         }
  13.  
  14.         if(rate==3)
  15.         {
  16.                 printf("nit is good");
  17.         }
Copyright exforsys.com


Figure 1 code 1 screen shot

Ads

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.



 
This tutorial is part of a C Tutorials tutorial series. Read it from the beginning and learn yourself.

C Tutorials

 

Comments