Control statements in C With Examples, sample outputs and list of sample programs here. List of C Tutorials for beginners. And, list of C programs. Our team has so far written 350+ sample program both in C and C++.
- What are control statements in C?
In C, we have 32 standard keywords and out of them 12 (the keywords in the second column of the following table) are control statements.
These statements control the flow of the program and out of them some are selection statements, some are iterative statements and some other are jumping statements.
List of Different control statements in C Programming: Do check it out here.
int | if | struct |
char | else | union |
float | switch | enum |
double | case | typedef |
void | default | volatile |
short | goto | sizeof |
long | while | const |
signed | do | |
unsigned | for | |
auto | break | |
register | continue | |
static | return | |
extern |
- The if, else, switch, case and default are used for selection purposes.
- The do, while and for are used for iterative purposes.
- The goto, break, continue and return are used for jumping purposes.
- if-else is used to select one out of two possibilities generally.
- switch is used to select one out of multiple possibilities generally.
- while is used to execute a set of statements repeatedly.
- for is used to execute a set of statements repeatedly.
- do-while is used to execute a set of statements repeatedly.
- break is used to come out of a switch/loop.
- continue takes us to next iteration of a loop by skipping the remaining statements in the current iteration.
- return is used to take the control back to the calling function from a function.
If – else In C : Control Statements
If-else is a selection statement.
It can be used to select a block for execution based on a condition. Based on the form we write, if-else is classified into different types like
- simple if-else
- Simple If
- nested if-else
- if-else ladder
- nested if, etc.
A simple if-else selects one out of two possibilities where as a nested if-else selects one out of multiple possibilities. With a simple if construction, we can choose whether a statement should be executed or not.
1 2 3 4 5 6 7 8 9 10 11 12 13 |
Simple if-else Syntax: if(condition) { } else { } |
In case the condition is true, if block is executed and in case the condition is false, the else block is executed.
The condition should always result in a value which would be treated as true (1) or false (0). There will never be a situation where both parts will be executed. Skipping both parts is also not possible.
Eg:
- if(4<9) is true (1)
- if(4==9) is false (0)
- if(4) is true (1)
- if(printf(“himani”) ) is true (1)
- if(sqrt(90) < 10 ) is true (1)
- if(pow(2,5) < 10 ) is false (0)
- if(pow(2,5) < 100 ) is true (1)
We can write any number of statements in an if/else block. Those statements should be enclosed within { and }. In case we have only one statement, then the curly braces are optional.
Simple If
In fact, the else part is optional in the if-else construction. Such constructions are known as simple if constructs.
1 2 3 |
If(condition) Statement1; |
Multiple conditions :
Sometimes we need to take decisions based on multiple conditions. In that case those conditions can be combined with logical operators like the following:
1 2 3 4 5 6 7 |
if(condition1 && condition2 && condition3 && condition4) Statement1; else Statement2; |
Nested If-Else
Sometimes we need to check one condition only when some other condition is satisfied. In that case we can have an if-else construction inside another if/else…
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 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 |
If(cond1) { If(cond2) { partA; } else { partB; } } else { If(cond3) { partC; } else { partD; } } |
In the above example,
- partA is executed when condition1 is true and condition2 is true.
- partB is executed when condition1 is true and condition2 is false.
- partC is executed when condition1 is false and condition3 is true.
- partD is executed when condition1 is false and condition3 is false.
This kind of if-else construction is known as nested if-else construction. With this usage, we can select one option out of multiple possibilities. There is no limit on the depth of nesting.
Nested If
Sometimes we write nested if-else constructions without any else parts. Such constructions are generally known as nested-if constructions.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
If(cond1) { If(cond2) { If(cond3) { } } } |
If-Else-If-Ladder
Sometimes we keep write if-else constructions inside the else parts only. These constructions are known as if-else ladder (or) else-if ladder (or) if-else-if ladder.
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 |
if(cond1) Statement1; else if(cond2) Statement2; else if(cond3) Statement3; else if(cond4) Statement4; else Statement5; |
The following code is not valid as there should not be any statements between if part and its else part. In the following code, the statement b=20; is between if part and else part and breaks the link between if and else parts.
1 2 3 4 5 6 7 8 9 |
If(2<3) a=10; b=20; else c=30; |
1 2 3 4 5 6 7 |
if(a<b) x=10; else (b<c) y=20; |
1 2 3 4 5 6 7 |
if 5<8 m=22; else n=44; |
The following code is not valid as parentheses should be closed properly.
In the following code, only 2<3 is considered as part of the condition and 5<9 will result in error.
1 2 3 4 5 6 7 |
if (2<3) && (5<9) a=88; else b=99; |
The proper construction should be like if(2<3 && 5<9) or it can be written like if( (2<3) && (5<9) ) also.
Switch:
- Switch is a statement used to select one out of multiple options.
- Switch is generally known as multi-way branch statement.
- Switch is more suitable when a single expression is compared for equality with multiple constants.
Generally switch is felt as ‘easier to use’ compared with an equivalent if-else construction.
Syntax:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
switch(expression) { case label1: ------------- case label2: ------------- case label3: ------------- ------------- ------------- default: ------------- } |
When control comes to a switch construction, it evaluates the expression in the header.
Eg:
- switch(2+3*4) will be evaluated to switch(14)
- switch(10<20) will be evaluated to switch(1)
- switch(10>20) will be evaluated to switch(0)
- switch(24/10) will be evaluated to switch(2)
- switch(4) will be evaluated to switch(4)
The expression should result in an int value.
The evaluated result is compared with the available case labels for an equality match. When a match is found the statements corresponding to that case are executed. A break is used to come out of a switch construction.
If no case label is matched with the switch expression, then the default block is executed.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch(4) { case 1: printf("A"); break; case 2: printf ("B"); break; case 3: printf ("C"); break; default: printf("D"); break; } |
In this example, the default part is executed and D is printed as 4 do not match with 1, 2 and 3.
- The default part is optional.
- If we don’t want any particular action in case no label is matched, then we can skip default part.
- In case no case label is matched and default part is not provided then no action will take place.
Generally a break statement is used at end of each case block. If break is not provided, the system automatically enters into the next case block and executes the statements there.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch(2) { case 1: printf("A"); case 2: printf("B"); case 3: printf("C"); break; default: printf("D"); break; } |
In this example, case 2 is matched.
- After printing “B”, it enters into case 3 and prints “C” as there is no break at case 2 block.
- After printing “C”, control comes out as a break is there.
Invalid Ways Of Using Switch
The following are not valid as two case labels exist with a same value.
Eg1:
1 2 3 4 5 6 7 8 9 |
switch(5) { case 5: statementA; case 5: statementB; } |
Eg2:
1 2 3 4 5 6 7 8 9 |
switch(7) { case 2+5: statementA; case 3+4: statementB; } |
Example – 3:
1 2 3 4 5 6 7 8 9 |
switch(65) { case 65: statementA; case ‘A’: statementB; } |
The following code is not valid as the case labels cannot have fraction part values.
Eg:
1 2 3 4 5 6 7 8 9 |
switch(5) { case 5.0: statement1; case 3: statement2; } |
The following code is not valid as variables are not allowed as case labels.
1 2 3 4 5 6 7 8 9 10 11 |
int a=4,b=6; switch(a) { case a: statementA; case b: statementB; } |
The default part can be placed anywhere. So the following code is valid.
Eg:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
switch(expression) { case 11: statementA; default: statementB; case 21: statementC; case 31: statementD; } |
Nested Switch Construction
Similar to nesting of if-else, we can nest switch construction also.
Syntax:
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 27 28 29 30 31 32 33 |
switch(expr1) { case labelA: switch(expr2) { case label1: xxxx case label2: xxxx } break; case labelB: switch(expr3) { case labelX: xxxx case labelY: xxxx } break; } |
We can have any number of statements in a case block and they do not need to be enclosed within curly braces.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 |
switch(25) { case 25: a=b; b=c*d; p=b/6+d; break; case 33: a=c; } |
If we want to execute a same code for different values then we can write code as follows.
Example:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 |
switch(month) { case 1: case 2: case 3: q=1; break; case 4: case 5: case 6: q=2; break; } |
But the following code is not valid…
1 2 3 4 5 6 7 8 9 |
switch(month) { case 1 || 2 || 3: q=1; break; case 4 || 5 || 6: q=2; break; } |
Limitations of switch
- Good for equality comparisons but not for range comparisons.
- Works good for int type data only and not good for other types of data.
- Works good with constants but not with variables (as case labels).