0% found this document useful (0 votes)
23 views15 pages

Special Control Statements in C

The document explains decision control statements in C programming, focusing on the continue, break, and switch-case statements. The continue statement skips the current iteration of a loop, while the break statement terminates the loop or exits a switch-case block. The switch-case statement allows for multiple options and executes corresponding tasks based on the input value, with the importance of using break statements highlighted to prevent fall-through behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
0% found this document useful (0 votes)
23 views15 pages

Special Control Statements in C

The document explains decision control statements in C programming, focusing on the continue, break, and switch-case statements. The continue statement skips the current iteration of a loop, while the break statement terminates the loop or exits a switch-case block. The switch-case statement allows for multiple options and executes corresponding tasks based on the input value, with the importance of using break statements highlighted to prevent fall-through behavior.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
You are on page 1/ 15

Basics of Programming – Decision control statements in C programming language 1

Special Control Statements in C


 Continue Statement
 Break Statement
 Switch Case

C – CONTINUE statement
The continue statement is used inside loops. When a continue statement is encountered
inside a loop, control jumps to the beginning of the loop for next iteration, skipping the
execution of statements inside the body of loop for the current iteration.

C – Continue statement
Syntax:

continue;

Flow diagram of continue statement


Basics of Programming – Decision control statements in C programming language 2

Example: continue statement inside for loop


#include <stdio.h>
int main()
{
for (int j=0; j<=8; j++)
{
if (j==4)
{
/* The continue statement is encountered when
* the value of j is equal to 4.
*/
continue;
}

/* This print statement would not execute for the


* loop iteration where j ==4 because in that case
* this statement would be skipped.
*/
printf("%d ", j);
}
return 0;
}

Output:

01235678

Value 4 is missing in the output, why? When the value of variable j is 4, the program
encountered a continue statement, which makes the control to jump at the beginning of the for
Basics of Programming – Decision control statements in C programming language 3

loop for next iteration, skipping the statements for current iteration (that’s the reason printf didn’t
execute when j is equal to 4).

Example: Use of continue in While loop


In this example we are using continue inside while loop. When using while or do-while loop you
need to place an increment or decrement statement just above the continue so that the counter
value is changed for the next iteration. For example, if we do not place counter– statement in the
body of “if” then the value of counter would remain 7 indefinitely.

#include <stdio.h>
int main()
{
int counter=10;
while (counter >=0)
{
if (counter==7)
{
counter--;
continue;
}
printf("%d ", counter);
counter--;
}
return 0;
}

Output:

10 9 8 6 5 4 3 2 1 0

The print statement is skipped when counter value was 7.


Basics of Programming – Decision control statements in C programming language 4

Another Example of continue in do-While loop


#include <stdio.h>
int main()
{
int j=0;
do
{
if (j==7)
{
j++;
continue;
}
printf("%d ", j);
j++;
}while(j<10);
return 0;
}
Output:

012345689

C – break statement in C programming


The break statement is used inside loops and switch case.

C – break statement
1. It is used to come out of the loop instantly. When a break statement is encountered inside
a loop, the control directly comes out of loop and the loop gets terminated. It is used
with if statement, whenever used inside loop.
Basics of Programming – Decision control statements in C programming language 5

Flow diagram of break statement

Syntax:

break;

Example – Use of break in a while loop


#include <stdio.h>
int main()
{
int num =0;
while(num<=100)
{
printf("value of variable num is: %d\n", num);
Basics of Programming – Decision control statements in C programming language 6

if (num==2)
{
break;
}
num++;
}
printf("Out of while-loop");
return 0;
}

Output:

value of variable num is: 0


value of variable num is: 1
value of variable num is: 2
Out of while-loop

Example – Use of break in a for loop


#include <stdio.h>
int main()
{
int var;
for (var =100; var>=10; var --)
{
printf("var: %d\n", var);
if (var==99)
{
break;
}
Basics of Programming – Decision control statements in C programming language 7

}
printf("Out of for-loop");
return 0;
}

Output:

var: 100
var: 99
Out of for-loop

Example – Use of break statement in switch-case


#include <stdio.h>
int main()
{
int num;
printf("Enter value of num:");
scanf("%d",&num);
switch (num)
{
case 1:
printf("You have entered value 1\n");
break;
case 2:
printf("You have entered value 2\n");
break;
case 3:
printf("You have entered value 3\n");
break;
Basics of Programming – Decision control statements in C programming language 8

default:
printf("Input value is other than 1,2 & 3 ");
}
return 0;
}

Output:

Enter value of num: 2


You have entered value 2

You would always want to use break statement in a switch case block, otherwise once a case
block is executed, the rest of the subsequent case blocks will execute. For example, if we don’t
use the break statement after every case block then the output of this program would be:

Enter value of num: 2


You have entered value 2
You have entered value 3
Input value is other than 1, 2 & 3

C – switch case statement in C Programming


The switch case statement is used when we have multiple options and we need to perform a
different task for each option.

C – Switch Case Statement


Before we see how a switch case statement works in a C program, the syntax of it is.
Basics of Programming – Decision control statements in C programming language 9

switch (variable or an integer expression)


{
case constant:
//C Statements
break;
case constant:
//C Statements
break;
default:
//C Statements
break;
}

Flow Diagram of Switch Case


Basics of Programming – Decision control statements in C programming language 10

Example of Switch Case in C


Let’s take a simple example to understand the working of a switch case statement in C program.

#include <stdio.h>
int main()
{
int num=2;
switch(num+2)
{
case 1:
printf("Case1: Value is: %d", num);
case 2:
printf("Case1: Value is: %d", num);
case 3:
printf("Case1: Value is: %d", num);
default:
printf("Default: Value is: %d", num);
}
return 0;
}

Output:

Default: value is: 2

Explanation: In switch I gave an expression, you can give variable also. I gave num+2, where
num value is 2 and after addition the expression resulted 4. Since there is no case defined with
value 4 the default case is executed.
Basics of Programming – Decision control statements in C programming language 11

Introducing Break statement


Before we discuss more about break statement, guess the output of this C program.

#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
case 2:
printf("Case2 ");
case 3:
printf("Case3 ");
case 4:
printf("Case4 ");
default:
printf("Default ");
}
return 0;
}

Output:

Case2 Case3 Case4 Default

I passed a variable to switch, the value of the variable is 2 so the control jumped to the case 2,
However there are no such statements in the above program which could break the flow after the
execution of case 2. That’s the reason after case 2, all the subsequent cases and default
statements got executed.

How to avoid this situation?


We can use break statement to break the flow of control after every case block .
Basics of Programming – Decision control statements in C programming language 12

Break statement in Switch Case

Break statements are useful when you want your program-flow to come out of the switch body.
Whenever a break statement is encountered in the switch body, the control comes out of the
switch case statement.

Example of Switch Case with break


I’m taking the same above that we have seen above but this time we are using break.

#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
case 1:
printf("Case1 ");
break;
case 2:
printf("Case2 ");
break;
case 3:
printf("Case3 ");
break;
case 4:
printf("Case4 ");
break;
default:
printf("Default ");
}
return 0;
}
Output:

Case 2
Basics of Programming – Decision control statements in C programming language 13

Why didn’t I use break statement after default?


The control would itself come out of the switch after default so I didn’t use it, however if you
want to use the break after default then you can use it, there is no harm in doing that.

Few Important points regarding Switch Case

1) Case doesn’t always need to have order 1, 2, 3 and so on. They can have any integer value
after case keyword. Also, case doesn’t need to be in an ascending order always, you can specify
them in any order as per the need of the program.

2) You can also use characters in switch case. for example –

#include <stdio.h>
int main()
{
char ch='b';
switch (ch)
{
case 'd':
printf("CaseD ");
break;
case 'b':
printf("CaseB");
break;
case 'c':
printf("CaseC");
break;
case 'z':
printf("CaseZ ");
break;
default:
printf("Default ");
}
return 0;
}
Output:

CaseB
Basics of Programming – Decision control statements in C programming language 14

3) The expression provided in the switch should result in a constant value otherwise it would not
be valid.

For example:
Valid expressions for switch –

switch(1+2+23)
switch(1*2+3%4)
Invalid switch expressions –

switch(ab+cd)
switch(a+b+c)

4) Nesting of switch statements are allowed, which means you can have switch statements inside
another switch. However nested switch statements should be avoided as it makes program more
complex and less readable.

5) Duplicate case values are not allowed. For example, the following program is incorrect:
This program is wrong because we have two case ‘A’ here which is wrong as we cannot have
duplicate case values.

#include <stdio.h>
int main()
{
char ch='B';
switch (ch)
{
case 'A':
printf("CaseA");
break;
case 'A':
printf("CaseA");
break;
case 'B':
printf("CaseB");
Basics of Programming – Decision control statements in C programming language 15

break;
case 'C':
printf("CaseC ");
break;
default:
printf("Default ");
}
return 0;
}
6) The default statement is optional, if you don’t have a default in the program, it would run just
fine without any issues. However it is a good practice to have a default statement so that the
default executes if no case is matched. This is especially useful when we are taking input from
user for the case choices, since user can sometime enter wrong value, we can remind the user
with a proper error message that we can set in the default statement.

You might also like