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:-
1 2 3 4 |
if (condition) { statement ; } |
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:
1 2 3 4 5 6 7 8 |
if (condition) { statement; } else { statement2 ; } |
C Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 |
#include <stdio.h> void main() { int a; printf("Enter number 1 or 0 "); scanf("%d",&a); if(a==1) {printf("\none");} else //optional_block {printf("\nzero");} } |
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:-
1 2 3 4 5 6 7 8 9 10 11 12 13 14 |
if(condition) {statement}; else if(condition) {statement}; else if(condition) {statement}; .. .. .. .. else {default statement}; |
C Code Example
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 |
#include <stdio.h> void main() { int a; printf("Enter number from 1 to 3\n"); scanf("%d",&a); if(a==1) {printf("\none");} else if(a==2) {printf("\nTwo");} else if(a==3) {printf("\nThree");} else {printf("\nYou Enter Wrong number");} } |
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 :
1 2 3 4 5 6 7 8 9 10 11 12 |
if (condition) { if(condition_2) { statement ; } } else { statement2 ; } |
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.
1 2 3 4 5 6 7 8 9 10 |
switch (expression) { case 1: statement-1; break; case 2: statement-2; break; default: statement-default; } |
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
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#include <stdio.h> void main() {int a; printf("Enter number from 1 to 3"); scanf("%d \n",&a); switch(a) { case 1: printf("one"); break; case 2: printf("Two"); break; case 3: printf("Three"); break; defaut: printf("you Enter wrong"); } } |
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.
1 2 3 4 5 6 7 8 |
goto label; label: ---------- statement; ---------- ---------- ---------- ---------- label: ---------- statement; goto label; Forward jump Backward jump |
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
1 2 3 4 5 6 7 8 9 10 11 |
#include <stdio.h> #include <math.h> void main() { float x,y; read: scanf("%f",&x); if (x<=0) {goto read;} y=sqrt(x); printf("%f %f \n",x,y); } |