Chapter 6


 

Different types of control statements

 

If statement

If – else statement

Nested if statement

Switch statement

Break statement

 

If- statement

 

The if statement, as its name implies is used to make decisions. If a certain condition is true, we direct the computer to take one course of action. If the condition is false, we direct to do something else.

 

e.g.

if(test expression)

{

statement 1;

statement 2;

statement 3;

}

Fig. Syntax of if statement 

First, test expression is evaluated, if it is found to be true, the statement inside the curly braces are executed. If it is false, the statements inside curly braces are skipped.

 

 

//program to illustrate if statement.

 

int n;

printf(“Enter a number(0-10):\n”);

scanf(“%d”, &n);

if(n==7)

{

printf(“Congratulation\n”);

printf(“You won”);

}

printf (“Thank you”);

}

 

Output:

Enter a number (0-10):

7

Congratulation

You won

Thank you

 

Enter a number (0-10):

5

Thank you.

 

 

If else statement

 

If else statement is used for the execution of one decision out of two decisions.

 

if(test expression)

{

true block statement;

}

else

{

false block;

}

statement  x;

 

Fig. Syntax

If the test expression is found to be true, true block statement is executed and it will skip the false block statement.

 

If the expression id found to be false, it will skip the block statements and execute false block statement.

 

Here, either true or false block will be executed, but not both. In both the case, the control is transferred to the statement x.

 

 

// program to illustrate if- else

 

 

int n;

printf(“Enter a positive no. \n”);

scanf(“%d”,&n);

if(n%2==0)

{

printf”\n You entered %d\n”,n);

printf(“The number is odd\n\n”);

}

printf(“Thank you”);

}

 

Output:

 

Enter a positive no. 7

You entered 7

The number is odd

Thank you

 

Enter a positive no. 8

You entered 8

The number is even

Thank you

 

 

Nested if- else statement

 

When a series of decision are involved, we may use more than one if-else statement in nested form.

 

if (test expression 1)

{

      if (test expression 2)

      {

            statement-1;

      }

      else

      {

      statement-2;

      }

}

else

{

statement 3;

}

statement x

 

Syntax of nested if – else



 

Flowchart of nested if- else statement

 

// program to show nested if- else

 

int x, y, z;

printf(“Enter any three num:\n”);

scanf(“%d %d %d”, &x, &y, &z);

if(x>y)

{

if(x>z)

      printf(“Largest value is %d”, x);

else

printf(“Largest value is %d “,z);

}

else

{

if(y>z)

printf(“Largest value is %d”, y);

else

printf(“Largest value is %d”, z);

}

 

Output

 

Enter any three num

65 35 23

Largest value is 65.

 

 

 

The else-if ladder

 

When multiple decisions are involved and if it is to be solved using if-else statement. The program becomes more and more difficult to read. The only way is else – if ladder.

 

if(test expression 1)

      statement 1;

else if(test expression 2)

      statement 2;

else if(test expression 3)

      statement 3;

else if(test expression n)

statement n;

else

default statement;

 

statement x

 

Fig. Syntax of else-if ladder

 

 

 


//program to show else if ladder

 

int marks;

printf (“Enter your marks of any subject”);

scanf (“%d”, &marks);

if (marks>79)

printf (Grade A”);

else if(marks>59)

printf (“Grade B”);

else if(marks>39);

printf(“Grade C”);

else if(marks >31)

printf(“Grade D”);

else

printf(“Fail\n”);

}

 

Output:

Enter your marks of any subject

65

Grade B

 

 

 

Switch Statement

 

The control statement which allows us to make a decision from the number of choices is called a switch-case statement.

 

C has a multiple branch selection statement called switch, which successively tests the value of an expression against a list of integer or character constants. When a match is found, the statement associated with that constant are executed.

 

 

 

 

 

 

 

 

switch (expression)

{

case constant 1:

statement;

break;

case constant 2:

statement;

break;

case constant 3:

statement;

break;

 

.

.

default:

statement;

}

 

The expression must evaluate to an integer type. Thus, you can use character or integer values but floating point expression is not allowed. The value of expression is tested against the values, one after another, of the constants specified in the case statement sequence associated with that case is executed until the break statement or the end of the switch statement reached. The default statement is executed if no match is found.

 

The break statement is one of the C’s jump statements.

 

e.g.

 

int a=2;

switch(a)

{

case 1:

printf(“\n Case 1”);

break;

 

case 2:

printf(“\n Case 2”);

break;

 

case 3:

printf(“\n Case 3”);

break;

 

default:

printf(“\n default”);

}

}

 

 

Output

 

case 2

 

 

 

e.g.

 

int day;

printf(“Enter a no. (1-7)”);

scanf(“%d”, & day);

switch(day)

{

case 1:

printf(“\n Sunday\n”);

break;

 

case 2:

printf(“\n Monday”);

break;

 

case 3:

printf(“\n Tuesday”);

break;

 

case 4:

printf(“Wednesday”);

break;

 

 

default:

printf(“Invalid choice”);

}

 

Printf(“\n**Thank You**”);

 

Output:-

Enter a number(1-7)

3

Tuesday

**Thank You**

 

 

//program to illustrate the use of switch statement

 

int choice,n1,n2,result;

printf(“Electronic calculator\n”);

printf(“1.Add\n”);

printf(“2.Subtract\n”);

printf(“3.Multiply\n”);

printf(“Enter your choice:\n”);

scanf(“%d”,&choice);

switch(choice)

{

case 1:

printf(“Enter the two no. “);

scanf(“%d%d”,&n1,&n2);

result=n1+n2;

printf(“\n%d+%d=%d”,n1,n2,result);

break;

 

 

case 2:

printf(“Enter the two no. “);

scanf(“%d%d”,&n1,&n2);

result=n1-2;

printf(“\n%d-d=%d”,n1,n2,result);

break;

 

case 3:

 

printf(“Enter the two no. “);

scanf(“%d%d”,&n1,&n2);

result=n1*2;

printf(“\n%d*d=%d”,n1,n2,result);

break;

 

default:

printf(“\nInvalid choice”);

}

 

printf(“\n ***Thank You***”);

 

Output-

 

Electronic Calculator

1.  Add

2.  Subtract

3.  Multiply

Enter your choice

1

Enter the two no.

1   4

1+4=5

***Thank You**

 

 

The choice 1 is stored in integer variable choice. Since, choice has value 1 the control jumps to the case 1 of the switch statement. As a result, user is asked to enter two numbers. The two numbers are added and the result is printed out. After that control encounters break statement and control goes out of the switch statement. The last printf statement is outside the switch statement so it is always executed.

 

The goto statement

 

The goto requires a label in order to identify the place where the branch is to made. A label is any valid variable name and must be followed by a colon. The label is placed before the statement where the control is to be transferred.

 

Syntax:

goto label;

-------

-----

label:

statement;

 

 

Forward jump

 

 

label:

statement;

---------

---------

goto label;

 

Backward jump

 

e.g. int x;

printf(“\nEnter the value of x”);

scanf(“%f”,&x);

while(x<=100)

{

 

------

if(x<0)

goto errorcheck;

}

 

errorcheck:

{

printf(“Enter negative value for x”);

------

}

 

Looping

 

To solve certain problems, C provides iteration statements (also called loop). Loops allow a set of instructions to be repeatedly executed as long as expression is true. When the expression becomes false, the loop terminates and control passes on to the statement following the loop. In brief, when statements are to be repeated again and again, we make the use of looping.

Looping may be

  1. Counter controlled loops
  2. Sentinel controlled loops

 

Counter controlled repetition is sometimes called definite repetition because we know in advance exactly how many times the loop will be executed.

Sentinel controlled repetition is sometimes called indefinite repetition because it is not known in advance how many times the loop will be executed.

 

C provides three kinds of looping statement by which both counter control repetition and sentinel controlled repetition can be achieved. They are:

            A. while

            B. do... while

            C. for

 

 

A. while statement

            First, test expression is evaluated and if it is found to be true, control enters the body of the loop and executes the statement contained in it. After that control statements again goes back to the test expression in while statement to check whether it is true or false. If it is again found to be true, control will enter inside loop and execute statement and this process will continue as long as the test expression evaluates to true. As soon as test expression evaluates to false, control will come out of the loop.

 

            while(test expression)

     

{

statement 1;

statement 2;

.

.

}

Syntax of while


Fig. Flowchart of while statement

 

// program using while loop to calculate the sum of 100 natural numbers.

 

#include<stdio.h>

#include<conio.h>

 

void main ()

{

int sum=0, i=1;

while (i<=100)

{

sum+=i;

i++;

}

printf (“The sum of 100 natural numbers is %d.\n”, sum);

getch ();

}

 

Output:

The sum of 100 natural numbers is 5050.

 

 

This program calculates the sum of 100 natural numbers starting from 1 to 100 without declaring 100 variables. In the declaration section, sum and I integer variables are initialized to 0 and 1 respectively. Since 1 is smaller than 100, test expression evaluates to true and control enters inside the body of while loop.

 

The looping used in above program is called counter controlled repetition because we know in advance how many times the loop is going to be executed.

 

// program to display first 10 natural number using while loop

 

int i=1;

while(i<=10)

{

printf (“%d,”, i);

i++;

 

}

}

 

 

Output:

1, 2,3,4,5,6,7,8,9,10

 

 

 

 

 

//program to show sentinel controlled repetition

 

int flag=1, choice;

printf(“1. Addition\n”);

printf(“2. Subtraction\n”);

printf(“3. Multiplication”);

printf(“4.Exit”);

 

while(flag==1)

{

printf(“Enter your choice\n”);

scanf(“%d”, &choice);

switch (choice)

{

case 1:

printf(“Your choice is addition\n”);

break;

case 2:

printf(“Your choice is subtraction\n”);

break;

case 3:

printf(“Your choice is Multiplication\n”);

break;

case 4:

printf(“Bye\n”);

flag=0;

 

break;

}

}

printf(“\n***Thank you***\n”);

}

 

Here termination of program depends upon the choice of user. If user choose 4, value of variable flag is set to zero such that test condition of while evaluates to false and loop terminates.

 

2. Do... while statement

            The while loop is top tested i.e. it evaluates the condition before executing any of the statements in its body. The do... while loop, on the other hand, evaluates the condition after the execution of the loop once. So, the do…while loop is called a bottom- tested loop. In do…while loop, since while statement is placed at the end of do... while block, while statement is always terminated with semicolon (;).

 

do

{

statement 1;

statement 2;

.

.

statement n;

}

while(test expression);

 

Syntax of do. While statement


// program to sum only positive integer.

 

int n, sum=0;

printf(“Enter 0 to end\n\n”);

do {

printf(“Enter a positive integer :\n”);

scanf(“%d”,&n);

if(n>0)

sum=sum+n;

}

while(n!=0)

printf(“The sum is %d”,sum);

}

 

//program to reverse a number

 

int n, rev=0, rem;

printf(“Enter an integer”);

scanf(“%d”,&n);

 

do

{

rem=n%10;

rev=rem+rev*10;

n=n/10;

}

while(n>0);

printf(“\nThe reverse is %d”,rev);

}

 

 

 

//program to sum a series of integer input until input is 0

 

int n,sum=0;

do

{

printf(“Enter an integer number:\n);

scanf(“%d”,&n);

sum=sum+n;

}

while(n!=0);

printf(“\n The sum is %d”,sum);

}

 

Output:

Enter an integer number:

1

Enter an integer number:

2

Enter an integer number:

0

The sum is 3

 

 

3. for statement

 

Most of the while loops we have seen is as follows. First, the variable used in the loop condition is initialized. A test is made at the beginning of each iteration. The body of the loop ends with a statement that modifies the value of the test variable.

C provides a sort of abbreviated version of while loop called for loop in which all the three scattered expressions are combined together.

 

for (initialization exp; test exp; update exp)

 

{

statement 1;

statement 2;

}

 

Fig. Syntax of for statement


In for loop, initialization of variables is done in the first step. After that, test expression is evaluated. If it is true, control enters inside body of loop and statements contained in it are executed. Then update expression is evaluated. After the execution of update expression, again the test expression is evaluated. If it is true, body of loop is executed. This expression continues as long as test expression evaluates to true. At any instant, if the test expression evaluates to false the control comes out of the loop.

 

//program to display the multiplication table of a number using for loop

 

int i, mult, n;

printf(“Enter a number:\n”);

scanf(“%d”, &n);

for(i=1;i<=10;i++)

{

mult=i*n;

printf(“%d*%d=%d\n”, n, i, mult);

}

}

 

 

Jumps in loops(break statement):-

 

Loops perform a set of operations repeatedly as long as the test expression evaluates to true. Sometimes, when executing a loop it becomes desirable to break the loop forcibly even when the test expression of the loop evaluates to true.

 

An early exit from the loops can be done by using the break statement. The break statement can be used within while, do while and for loops.

 

while(test expression)

 

{

statement;

if(condition)

break;

statement;

}

 

 

do

{

statement;

if(condition)

break;

statement;

}

while(test expression);

 

 

for(init exp; test exp; updated exp)

{

statement;

if(condition)

break;

statement;

}

 

Fig. use of break statement in while, do.. while and for loop.

 

 

 

 

//program to show use of break statement in for loop

 

 

int i;

for(i=1;i<=10;i++)

{

printf(“%d,”, i);

if(i==5)break;

}

printf(“You are out of loop”);

}

 

 

 

//program to show break statement in while loop

 

 

int count=1;

float x, sum=0.0,average;

printf(“Enter the first number\n”);

scanf)”%f”, &x);

while(count<=100)

{

sum=sum+ x;

count-count+1;

printf(“Enter the next number\n”);

scanf(“%f”, &x);

if(x==0)

break;

}

average=sum/(count-1);

printf(“The summation is %f”, sum);

printf(“The average is %f”, average);

}

 

 

 

Skipping a part of a loop (continue statement)

 

During the loop operation, sometimes, it may be necessary to skip a certain statements or part of the body of the loop under certain condition.

 

// program to add only positive numbers using for loop and continue statement

 

int sum=0,i,n;

for(i=1;i<=5;i++)

{

printf(“Enter a number:\n”);

scanf(“%d”, &n);

if(n<0)

continue;

else

sum+=n;

}

printf(“The sum of positive number is %d”, sum);

}

 

 

Nested for statement

 

Normally any loop can be placed inside other loop. Here we are discussing about for loop inside another for loop.

 

for(init exp; test exp; update exp)

{

for(init exp; test exp; update exp)

{

 statement 1;

 statement 2;

}

}

 

 

Control enters into init exp of outer for loop where variable is initialized to some value.

Test expression is evaluated at step 2

If it is true, control goes to the initialization exp of inner for loop.

If it is false, control come out of the outer for loop.

Then expression of inner for loop is evaluated, if it is found to be true, control enters inside body of inner for loop and statements contained in it are executed as shown in 5. Then the control goes to update exp. of inner for loop for updating variable. Again, test expression of inner for loop is evaluated and if it is true, same process repeats. If test expression of inner for loop is false it will come out of it and goes to update expression of outer for loop. Test expression of outer for loop is evaluated. If it is true, control again goes to initialization exp. of inner for loop where init. Variable is again reinitialized and this process repeats.

 

 

In brief, if outer loop works n times inner loop will work n2 times.

 

// program to illustrate nested for loop

 

int i, j, product;

for(i=1;i<=5;i++)

{

      for(j=1;j=5;j++)

{

product=i*j;

printf(“%3d”,product);

}

printf(“\n---------\n”);

}

}

 Full Width




Post a Comment

0 Comments