Statements and flow control

We have seen that a C program is a set of the statement which are normally executed sequentially in the order in which they appear. This happens when no option or no repetitions of certain calculations are necessary. However, in practice, we have a number of situations where we may have to change the order based on certain conditions, or repeat a group of statements until specified conditions are met. This involves a kind of decision-making to see whether a particular has occurred or not and then direct the machine to execute certain statements accordingly.

if-else

The if statement is a powerful decision-making statement and is used to control the flow of execution. It allows the computer to evaluate the expression first and then, depending on whether the value or the expression (condition) is ‘true’ (or non-zero) or ‘false’ (zero), it transfers the control to a particular statement.

The general form of a simple if is:-

If the expression is true, only then the statement will be executed otherwise the statement will be skipped.

when the condition is not fulfilled, by using the else keyword to introduce an alternative statement. Whose syntax is:

C Code Example

The Else if Ladder

There is another way of putting ifs together when multipath decisions are involved. A multipath decision is a chain of ifs in which the statement is associated with each else is an if. It takes the following general form:-

C Code Example

Nested if Statement

When a series of decision is involved, we may have to use more than one if
For example:- “if grade of A is more than grade of B and if grade of A is more than C.”

Syntax :

switch statement:

We have seen that when one of the many alternatives is to be selected, we can use an if statement to control the selection. However, the complexity of such a program increases dramatically when an alternative increases. The program becomes difficult to read and follow. At times, it may confuse even the person who designed it. Fortunately, C has a built-in multiway decision statement known as a switch.

The expression is an integar expression.1,2 are constants and are known as case labels. Each of these values should be unique within the switch statement.statement-1,statement-2 are statement lists and may contain more statements. Note that the case label ends with a colon(:).

When the switch is executed, the value of the expression is successfully compared against the value 1,2..if a case found whose value match then the block statement executed until break not found.

why we use default?

The default is an optional case. when present, it will executed if the value of expression does not match with any of the case values.If not present, no action takes place.

C Code Example

Goto

So far we have discussed ways of controlling the flow of execution based on certain specified conditions. Like any other language, C supports the goto statement to branch unconditionally from one point to another in the program. Although it may not be essential to use the goto statement in a highly structured language like C

The goto requires a label in order to identify the place where the branch is to be made. A label is any valid variable name and must be followed by a colon. The label is placed immediately before the statement where the control is to be transferred. The general forms of goto and label statements are shown below:-

The label can be anywhere in the program either before and after the goto label.

Note:- that a goto break the sequential flow of the program. If the label is before the statement goto label, a loop will be formed and some statements will be executed repeatedly. such a jump is known as backward jump On the other hand if the label is placed after the goto label; some statements will be skipped and jump is known as forward jump.

C Code Example