C programs are executed in a sequence, but we can control the execution of program by using any control mechanism by which we can compare things and come to a decision. This involves using some operations called Relational Operators and conditional statements called if-else and loops. We use fundamental operators to compare two values in our C programs.
These relational operators compare two values and return true or false after comparison. We can use the relational operators to compare values of any basic data type, so all we need is to modify the behavior of the program.
“The relational operators compare two values of any basic data type and return true or false after comparison.”
In C, there is another operator called ternary operator which is an expression instead of a statement i.e. you can have it on the right-hand side (RHS) of a statement. So you can write certain code statements more concisely.
The rest of the article is divided into the following main sections:
Decision Control Structure in C
The decision control structure in C can be implemented using the following statement types.
The if Statement
The if statement by itself will execute a single statement or a group of statements when the condition following if is true.
If the condition is false then a group of statements can be executed using else statement
The simple example of an if statement is:
if (varName == 20) printf ("Value of the variable is 20");
We can also use the code block to specify the statements to be pre-executed if the given condition is true i.e.
if (varName == 20){ printf ("Value of the variable is 20"); printf ("Print what ever you want!!!"); }
The general form of if statement looks like this:
if (this condition is true) execute this statement;
Here the keyword if tells the compiler that what follows, is a decision control instruction. The condition following the keyword if is always enclosed within a pair of parentheses. If the condition, whatever it is true, then the statement is executed. It the condition is not true then the statement is not executed instead the program skips past it. The condition in C is evaluated using C’s relational operators. The relational operators help us to build expression which are either true or false
This expression is true if
X == Y X is equal to Y X != Y X is not equal to Y X > Y X is greater than Y X <= Y X is less than or equal to Y X >= Y X is greater than or equal to Y
Demonstration of if statement in C
#include <stdio.h> int main( ) { int num; printf("Enter a number less than 10 \n"); scanf("%d", &num); if(num <= 10){ printf("The number is less than 10 \n"); } }
Multiple statements within an if statement
If it is desired that more than one statement is to be executed if the condition is satisfied, then such statements must be placed within pair of braces. for e.g.
The following program demonstrate that if year of service greater than 3 then a bonus of Rs. 2500 is given to employee. The program illustrates the multiple statements used within if
#include <stdio.h> int main( ) { int bonus, cy, yoj, yos; yos = 0; cy = 2018; yoj = 2013; yos = cy - yoj; if(yos > 3) { bonus = 2500; printf("Bonus = Rs. %d", bonus); } }
The if-else statement
The if statement by itself will execute a single statement or a group of statements when the condition following if is true. it does nothing when the condition is false. It the condition is false then a group of statements can be executed using else statement. The following program illustrates this
#include <stdio.h> /* Calculation of gross salary */int main( ) { float bs, gs, da, hra; bs = 23000; if(bs < 1500){ hra = bs * 10 / 100; da = bs * 90 / 100; } else{ hra = 500; da = bs * 98 / 100; } gs = bs+hra+da; printf("gross salary = USD. %f ", gs); }
Nested if – else statement
It we write an entire if – else construct within the body of the if statement or the body of an else statement. This is called ?nesting? of if. For e.g.
if (condition) { if (condition) { do this; else { do this; and this; } } else do this;
What are the examples of Conditional Operators?
- < Less than
- > Greater than
- == Equal to
- <= Less than or equal to
- >= Greater than or equal to
- != Not equal to
The Loop Control Structure in C
These are three methods by way of which we can repeat a part of a program. They are:
- The for Loop
- The while Loop
- The do-while Loop
- The break Statement
- The continue Statement
The for Loop
The for loop is started with the keyword
for
. There are three expressions which appear with in a for loop.
- Initializing Expression
- Test Expression
- Increment Expression
It has the following standard form.
for (int i = 0; i <= min; i++) ans += i;
The general form of for statement is as under:
for( initialise; condition; increment_counter){ statements(); }
Here the
initialise
step is executed first, and only once. Then
condition
is evaluates to see if it’s true or false. If true, the body of the loop is executed otherwise body of the loop does not execute and flow of control jumps to the next statement just after the for loop. After each time the body of the for loop executes, the
increment_counter
value is increased/decreased based on the increment counter.
Now let us write a simple interest problem using the for loop
#include <stdio.h> int main () { // for loop execution for( int a = 5; a <= 10; a ++ ) { printf("Value of a: %d \n", a); } return 0; }
When the above code is compiled and executed the result is as below:
Value of a: 5 Value of a: 6 Value of a: 7 Value of a: 8 Value of a: 9 Value of a: 10
The while Loop
The general from of while is as shown below:
while (test loop counter using a condition) { do this; and this; increment loop counter; }
The parentheses after the while contains a condition so long as this condition remains true all statements within the body of the while loop keep getting executed repeatedly for e.g.
#include <stdio.h> int main () { int a = 5; // while loop execution while(a <= 10) { printf("Value of a: %d\n", a); a++; } return 0; }
When the above code is compiled and executed the result is as below:
Value of a: 5 Value of a: 6 Value of a: 7 Value of a: 8 Value of a: 9 Value of a: 10
The do-while Loop
The do-while loop is similar to the while loop in that the loop continues as long as the specified loop condition remains true. The main difference is that the condition is checked at the end of the do-while statement. So do-while loop is always executed at least once. Its general form is
do { statements(s); } while (condition is true);
There is a minor difference between the working of while and do-while loops. The difference is the place where the condition is tested. The while tests the condition before executing any of the statements within the while loop. As against this the do-while tests the condition after having executed the statements within the loop. for e.g.
#include <stdio.h> int main () { int a = 11; // while loop execution while(a <= 10) { printf("Value of a: %d\n", a); a++; } return 0; }
In the above example the
printf()
will not get executed at all since the condition fails at the first time itself. Now let’s write the same program using a do-while loop.
#include <stdio.h> int main () { int a = 11; // while loop execution do { printf("Value of a: %d\n", a); a++; } while(a <= 10); return 0; }
In the above program the
printf()
would be executed once, since first the body of the loop us executed and then the condition is tested.
The break Statement in C
The keyword break allows us to jump out of a loop instantly without waiting to get back to the conditional test. When the keyword break is encountered inside any loop in C, control automatically passes to the first statement after the loop. For example the following program is to determine whether a number is prime or not.
To test a number is prime or not, is to divide it successively by all numbers from 2 to one less than itself. It the remainder of any of the divisions is zero, the number is not a prime. Following program implements this logic
#include <stdio.h> main( ){ int num, i; printf("Enter a number \n"); scanf("%d", &num); i = 2; while (i <= num -1){ if (num%i == 0){ printf("Number: %d is not a prime number \n", num); break; } i++; } if(i == num){ printf("Number: %d i Prime Number \n", num); } }
The continue Statement
The keyword continue allows us to take the control to the beginning of the loop bypassing the statements inside the loop which have not yet been executed. When the keyword continue is encountered inside any C loop control automatically passes to the beginning of the loop. For example:
#include <stdio.h> int main( ) { int i,j; for(i = 1; i <= 3; i++) { for(j = 1; j <= 3; j++) { if (i == j) continue; printf("%d %d \n", i, j); } } }
The output of the above C program would be….
1 2 1 3 2 1 2 3 3 1 3 2
when the value of i equal to that of j, the continue statement takes the control to the for loop (inner) bypassing rest of the statements pending execution in the for loop (inner).
The Case Control Structure
The Switch Statement
The switch statement causes a particular group of statements to be chosen from several available groups. The selection is based upon the current value of an expression that is included within the switch statement.
The form of switch statement is.
switch (integer expression) { case constant 1: do this; break; case constant 2: do this; break; case constant 3: do this; break; default; do this; }
Here are some of the rules which apply on switch statement in C language.
- The expression used in a switch statement should have an integral or enumerated type.
- There can be multiple case statement with in switch statement where case is followed by the value to be compared to and a colon.
- When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
- When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
- If there is no break statement appears within a case statement, the flow of the program continues until break statement is is reached or the switch statement end block reaches.
- A switch statement can have an optional default that can be used to execute some statements when no case statements are met before. As default is the last statement in switch so no break; statement is required to terminate this.
If you want to further understand conditional statements, then study the following C Programs:
- Print Fibonacci series in C
- Generating Random Number in C/C++
- Implementation of Base64 Encoding and Decoding in C
- Kruskal’s Algorithm Implementation in C
- Porter’s Algorithm in C