Home | | Programming and Data Structures I | Control statements

Chapter: Programming and Data Structures

Control statements

There are four types of control statements in C: 1. Decision making statements 2. Selection statements 3. Iteration statements 4. Jump statements

CONTROL STATEMENTS

 

Control statements enable us to specify the flow of program control; ie, the order in which the instructions in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to jump from one section of code to another.

 

There are four types of control statements in C:

 

1.     Decision making statements

2.     Selection statements

3.     Iteration statements

4.     Jump statements

 

1 Decision Making Statement: the if-else Statement

 

The if-else statement is used to carry out a logical test and then take one of two possible actions depending on the outcome of the test (ie, whether the outcome is true or false).

 

Example Discussed above.

 

 

 

 

2. Selection Statement: the switch-case Statement

 

A switch statement is used for multiple way selections that will branch into different code segments based on the value of a variable or expression. This expression or variable must be of integer data type

 

3. Iteration Statements

 

Iteration statements are used to execute a particular set of instructions repeatedly until a particular condition is met or for a fixed number of iterations.

 

 

THE FOR STATEMENT

 

The for statement or the for loop repeatedly executes the set of instructions that comprise the body of the for loop until a particular condition is satisfied.

 

Syntax:

 

for(initialization; termination; increment/decrement)

{

 

//statements to be executed

}

 

The for loop consists of three expressions:

The initialization expression, which initializes the looping index. The looping index controls the looping action. The initialization expression is executed only once, when the loop begins.

 

The termination expression, which represents a condition that must be true for the loop to continue execution.

 

The increment/decrement expression is executed after every iteration to update the value of the looping index.

 

The       following   program   uses   the   for   loop   to   pr

 

#include<stdio.h> int main()

 

{

int i,n, a, b, sum=0;

 

printf("Enter the number of terms:"); scanf("%d",&n); a=0; b=1; printf("%dn %d",a,b); for(i=2;i<n;i++)

 

{

sum=a+b;

 

printf("n%d",sum);

a=b;

 

b=sum;

}

 

return 0;

}

 

If the first two elements 0 and 1 are excluded, it is seen that each of the next elements in the series is the sum of the previous two elements. For example, the third element is a sum of the first two elements (0+1), the fourth element is a sum of the second and the third elements (1+1=2), the fifth element is a sum of the third and the fourth elements (1+2=3) and so on. Therefore, each time it is the sum of the previous two elements that is printed. The previous value of b becomes the new value of a and the previous value of sum becomes the new value of b. The newly calculated sum becomes the next element and is subsequently printed. These steps are executed repeatedly until the looping index i does not reach one less than the number of terms entered by the user. The looping index i begins from 2 as the first two terms have already been printed. It could also range from 0 to 1 less than n-2 as:

 

for(i=0;i<(n-2);i++) or from 1 to n-2 as: for(i=1; i<(n-2);i++)

 

The for statement is very versatile and can be written in four different ways:

 

 

1.     Omitting the Initialization Expression

 

In this case, the looping index is initialized before the for loop. Thus the for loop takes the following form:

 

i=0;/*initialization*/ for(; condition;increment/decrement expression) { //statements to be executed }

 

Notice that the semicolon that terminates the initialization expression is present before the condition expression.

 

2.     Omitting the Condition

 

 

In this case the condition is specified inside the body of the for loop, generally using an if statement. The while or do-while statements may also be used to specify the condition. Thus the for loop takes the following form:

 

for(initialization; ; increment/decrement expression)  { condition //statements to be executed }

 

Again, it can be seen that the semicolon that terminates the condition is present in the for statement. The following program explains how the condition can be omitted:

 

#include<stdio.h> int main()

 

{

int i,n, a, b, sum=0;

 

printf("Enter the number of terms:"); scanf("%d",&n);

 

a=0; b=1; printf("%dn %d",a,b); for(i=2; ;i++)

 

{

 

if(i==(n-1)) //condition specified using if statement break;

 

sum=a+b;

printf("n%d",sum);

 

a=b;

b=sum;

}

 

return 0;

}

 

 

3.     Omitting the increment /decrement Expression:

 

In this case the increment/decrement expression is written inside the body of the for loop.

 

Example:

 

#include<stdio.h> int main()

{

 

int i,n, a, b, sum=0;

 

printf("Enter the number of terms:"); scanf("%d",&n); a=0; b=1; printf("%dn %d",a,b);

for(i=2;i<n;)

 

{

sum=a+b;

 

printf("n%d",sum);

a=b;

 

b=sum;

i++;  //increment expression

 

}

return 0;

 

}

 

 

4. Omitting all Three Expressions:

It is also possible to omit all three expressions, but they should be present in the ways discussed above. If all three expressions are omitted entirely — ie, they are not mentioned in the ways discussed above — then the for loop becomes an infinite or never-ending loop. In this case, the for loop takes the following form:

 

for(;;)  { //statements }

 

2. The while statement

The while statement executes a block of statements repeatedly while a particular condition is true.

while (condition)  { //statement(s) to be executed }

 

The statements are executed repeatedly until the condition is true.

 

Example: Program to calculate the sum of the digits of a number (eg, 456; 4+5+6 = 15)

 

#include<stdio.h> int main()

 

{

int n, a,sum=0;

 

printf("n Enter a number:"); scanf("%d", &n); while(n>0)

{

 

a=n%10; //extract the digits of the number sum=sum+a; //sum the digits

 

n=n/10; //calculate the quotient of a number when divided by 10.

}

 

printf("n Sum of the digits=t %d",sum); return 0;

 

}

 

The above program uses the while loop to calculate the sum of the digits of a number. For example, if the number is 456, the while loop will calculate the sum in four iterations as follows. It would be helpful to remember that % gives the remainder and / the quotient.

 

Iteration 1: n>0  Condition is true(n=456)

 

a=n%10=6;

 

sum=sum+a=6;

 

n=n/10= 45;  New value of n is 45.

 

 

 

 

Iteration 2: n>0 Condition is true(n=45)

 

a=n%10=5;

 

sum=sum+a=6+5=11;

 

n=n/10= 4;  New value of n is 4.

 

Iteration 3: n>0 Condition is true(n=4)

 

a=n%10=4;

 

sum=sum+a=11+4=15;

 

n=n/10= 0;  ew value of n is 0.

 

Iteration 4: n>0 Condition is false(n=0).

 

After the fourth iteration control exits the while loop and prints the sum to be 15.

 

Example 2: Program to check whether the entered number is a palindrome or not.

 

A palindrome is a number which remains the same when its digits are read or written from right to left or vice versa, eg 343 is a palindrome, but 342 is not. The following program works on the logic that if the reverse of the number is same as the original number then the entered number is a palindrome, otherwise it is not.

 

#include<stdio.h> int main()

 

{

 

int a, n,m, reverse=0; printf("n Enter a number:"); scanf("%d", &n);

 

m=n;

while(n>0)

 

{

 

a=n%10; reverse=reverse*10 +a; n=n/10;

 

}

if (m== reverse)

 

{

printf(" n The number is a palindrome.");

 

}

else

 

{

printf("n The number is not a palindrome.");

 

}

return 0;

 

}

 

The above program uses almost the same logic as the program concerning the sum of digits. As was seen in that program, n becomes 0 in the last iteration. However, we need to compare the original value of n to the reverse of the number to determine whether it is a palindrome or not. Therefore, the value of n has been stored in m, before entering the while loop. The value of m is later compared with reverse to decide whether the entered number is a palindrome or not.

 

The while loop works in the following way: Let n=343;

 

Iteration 1: a= n%10=3; reverse=reverse*10+a=0*10+3=3; n=n/10=34;

 

Iteration 2: a= n%10=4; reverse=reverse*10+a=3*10+4=34;

 

n=n/10=3;

 

Iteration 3: a= n%10=3; reverse=reverse*10+a=34*10+3=343; n=n/10=0;

 

Iteration 4: n>0 condition false(n=0). Control exits from the while loop.

 

3. The do-while loop

The do-while statement evaluates the condition at the end of the loop after executing the block of statements at least once. If the condition is true the loop continues, else it terminates after the first iteration.

Syntax:

 

do

 

{

 

//statements to be executed; } while(condition);

 

Note the semicolon which ends the do-while statement. The difference between while and do-while is that the while loop is an entry-controlled loop — it tests the condition at the beginning of the loop and will not execute even once if the condition is false, whereas the do-while loop is an exit-controlled loop — it tests the condition at the end of the loop after completing the first iteration.

 

For many applications it is more natural to test for the continuation of a loop at the beginning rather than at the end of the loop. For this reason, the do-while statement is used less frequently than the while statement.

 

Most programs that work with while can also be implemented using do-while. The following program calculates the sum of digits in the same manner, except that it uses the do-while loop:

 

#include<stdio.h> int main()

 

{

int n, a,sum=0;

 

printf("n Enter a number:"); scanf("%d", &n);

 

do

{

 

a=n%10;

sum=sum+a;

 

n=n/10;

}while(n>0);

 

printf("n Sum of the digits=t %d",sum); return 0;

 

}

 

However, the do-while statement should be used only in situations where the loop must execute at least once whether or not the condition is true.

 

A practical use of the do-while loop is in an interactive menu-driven program where the menu is presented at least once, and then depending upon the choice of the user, the menu is displayed again or the session is terminated. Consider the same example that we saw in switch-case. Without using an iteration statement like do-while, the user can choose any option from the menu only once. Moreover, if a wrong choice is entered entering his choice again. Both these faults can be corrected by using the do-while loop.


Study Material, Lecturing Notes, Assignment, Reference, Wiki description explanation, brief detail
Programming and Data Structures : Control statements |


Privacy Policy, Terms and Conditions, DMCA Policy and Compliant

Copyright © 2018-2023 BrainKart.com; All Rights Reserved. Developed by Therithal info, Chennai.