C Control Statements

  • by

A program is a collection of a number of statements which are usually executed in sequence. We can do much more powerful operations with help of Programs if we can control the order in which statements are run using control statements in c.
In C Programming language there are three general types of statements. They are:
– Assignment, where values, usually the results of calculations, are stored in variables.
– Input / Output, data is read in or printed out.
– Control, the program makes a decision about what to do next.
We can use control statements for -:
looping:- Repeating sections of the program
Branching:- Selecting between optional sections of a program
Selection Statements (Branching), Nested switch, conditional expression
Branching is deciding what actions to take and looping is deciding how many times to take a certain action. Branching is so called because the program chooses to follow one branch or another.
if statement -:
This is the simplest form of the branching statements. It takes an expression in parenthesis and a statement or block of statements. if the expression is true then the statement or block of statements gets executed otherwise these statements are skipped.
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression is true */
}
else
{
/* statement(s) will execute if the Boolean expression is false */
}
If the Boolean_ expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
C programming language assumes any non-zero and non-null values as true, and if it is either zero or null, then it is assumed as false value.
if statements take the following forms -:
Simple ‘if’ with only one expression
if (expression)
statement;
‘if’ with more than one expressions
if (expression)
{
Block of statements;
}

Used for true and false type of statements: if…else -:
if (expression)
{
Block of statements;
}
else
{
Block of statements;
}
Used for branching: nested if -:
if (expression)
{
Block of statements;
}
else if(expression)
{
Block of statements;
}
else
{
Block of statements;
}
switch statement-:
A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.
Syntax:
switch(expression)
{
case constant-expression :
statements;
break; /* optional */
case constant-expression :
statements;
break; /* optional */
/* you can have any number of case statements */
default: /* Optional */
statements;
}
The following rules apply to a switch statement:
• The expression used in a switch statement must have an integral or enumerated type, or be of a class type in which the class has a single conversion function to an integral or enumerated type.
• You can have any number of case statements within a switch. Each case is followed by the value to be compared to and a colon.
• The constant-expression for a case must be the same data type as the variable in the switch, and it must be a constant or a literal.
• When the variable being switched on is equal to a case, the statements following that case will execute until a break statement is reached.
• When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.
• Not every case needs to contain a break. If no break appears, the flow of control will fall through to subsequent cases until a break is reached.
• A switch statement can have an optional default case, which must appear at the end of the switch. The default case can be used for performing a task when none of the cases is true. No break is needed in the default case.
Iterative Statements-:
Iterative statements in C are used to perform looping operations until the given condition is true. Control comes out of the loop statements once condition becomes false.
These types of task can be solved in programming using loops. There are 3 types of loop control statements in C language. They are,
1. for loop
2. while loop
3. do-while loop
for loop -:
The ‘for’ statement provides a compact way to iterate over a range of values or statements. Programmers often refer to it as the “for loop” because of the way in which it repeatedly loops until a particular condition is satisfied. The general form of the ‘for’ statement can be expressed as follows:
for(initialization; termination; increment)
{
statement(s);
}
When using this version of the ‘for’ statement, keep in mind that:
• The initialization expression initializes the loop; it’s executed once, as the loop begins.
• When the termination expression evaluates to false, the loop terminates.
• The increment expression is invoked after each iteration through the loop; it is perfectly acceptable for this expression to increment or decrement a value.

Leave a Reply