Control Flow – Decision Making in C

Control Flow (or flow of control) refers to the order in which the individual statements of a program are executed. In C language, statements are executed sequentially i.e. one after another. However, sometimes the nature of computation demands that the statements be executed out of order based on some condition. In our daily life we make such decisions every second. As an example, when a student leaves for the institute he makes a decision whether to carry an umbrella or not based on the weather conditions. So, if it is raining or is expected to rain, he may pick up one else he won’t. Right now, while you are reading this text, based on your comprehension you will make a decision whether to read further or not. In both cases, you will make a decision and will do things out of sequence. Same is true with computer programs. This is very common in programs to execute certain statements based on some condition. As an example, suppose you are asked to write a simple program that will read a number from keyboard and will display on screen whether the number is even or odd. How will you do that? The answer is very simple. You will declare some variable as integer or float, call scanf() function to read from keyboard, use some logic to decide whether the number is even or odd. The logic to decide whether a number is even or odd is simple: Divide the number by 2 and if there is some remainder then it is odd else it is even. You can use modulus operator (%) to do that. Finally, you will make a decision on run time whether to display “The number is even.” or “The number is odd.”. But what should you use in your C program to make a decision? In lesson 5 we have discussed one such operator (?:). However, this operator is limited in its capability and thus, C language provides certain Decision Making Flow Control statements.