Statements


Introduction

We have seen that a C program is a set of properly executed statements in the order in which they are displayed. This happens when no alternatives or repetitions of specific calculations are required. However, in reality, we have several situations where we may have to change the order in which our statements are executed based on certain conditions, or a group of statements may be repeated until certain conditions are met. It involves making a decision to see if there is a specific condition in it and then directing the computer to execute the specific statement.

C has the power to make such decisions in support of the following statements:

  1. if statement
  2. switch statement
  3. Conditional operator statement
  4. goto statement

These statements are known as decision-making statements. These statements ‘control’ the flow of execution, also known as control statements.

We have already used some of these statements in previous examples. Here we will discuss their features, capabilities, and applications in more detail.


Decision making with if statement

The 'if' statement is an effective decision-making statement and is used to control the flow of execution of a statement. It is basically a statement of a two-way decision and is used with an expression that takes the following form

if(test expression)

It allows the computer to evaluate the expression first and then, depending on whether the value of the impression is 'true or 'false', it transfers the control to a particular statement. This point of the program has two paths to follow, one for the true condition and the other for the false conditions shown in the right side picture.

Statemetns in C

The if statement may be implemented in different forms depending on the complexity of conditions to be tested. The different forms are:

  1. Simple if statement
  2. if....else statement
  3. Nested if....else statement
  4. else if ladder