Control Statements In C – C Programming | Tutorials

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.

Control statements in C
CBeginners.com

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.

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.

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:

 

 

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…

 

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.

 

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.

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.

The following code is not valid as conditions are not allowed at else part.

The following code is not valid as the parentheses are required to enclose the condition.

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.

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:

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:

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:

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: 

Eg2:

Example – 3:

The following code is not valid as the case labels cannot have fraction part values.

Eg:

The following code is not valid as variables are not allowed as case labels.

The default part can be placed anywhere. So the following code is valid.

Eg:

Nested Switch Construction

Similar to nesting of if-else, we can nest switch construction also.

Syntax:

We can have any number of statements in a case block and they do not need to be enclosed within curly braces.

Example:

If we want to execute a same code for different values then we can write code as follows.

Example:

But the following code is not valid…

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).

Add a Comment

Your email address will not be published. Required fields are marked *