C Unit-2
C Unit-2
Introduction –
If statements - If-else statements – nested if-else –
break statement-continue statement-go to statement-Switch statements.
Looping statements: -While statements – Do-while statements - For Statements-
nested loop statement
Control Statements in C:
The different types of control statements in C language are:
if statement:
The if statement allows the programmer to select a set of instructions to be executed, based on a
condition.
There are four variations of the if statement. They are:
1. Simple if
2. if…else
3. Nested if
4. else if Ladder
Simple if :
The simple if allows the programmer to execute or skip a set of instructions based on the value of a
condition.
The simple if is a one-way selection statement.
If the condition is true, a set of statements will be executed. If the condition is false, the control will
continue with the next statement after the if statement.
if-else statement:
The if…else is a two-way decision-making selection statement.
If the condition evaluates to true, one set of instructions will be executed.
If the condition evaluates to false, another set of instructions will be executed.
Nested if:
else if Ladder:
The else if ladder in C is a multi-way selection operator.
It allows the programmer to select one set of instructions among various other sets of instructions.
The else if ladder is similar to the logical OR operator.
If first condition is true, then corresponding set of instructions will be executed.
If the condition is false, then the next condition is checked and so on.
If all the conditions fail, the statements in the default block will be executed.
Switch Statement
Although, C provides a multi way selection statement like else if, when the number of conditions
increases, the program will become less readable.
To solve this problem, C provides an easy-to-understand multi way selection statement
called switch statement.
Looping statements:
Loop is a task to repeat the execution of same set of statements desired number of times.
‘C’ supports 3 types of looping statements:
1. While loop
2. Do-while loop
3. For loop
while Loop
In C, the while loop is an entry-check looping statement.
The body of the while loops is only executed when the condition evaluates to true.
If the condition evaluates to false, the body of the loop is not executed.
Minimum execution of while statement is zero.
Syntax
initialization;
while(condition)
{
statement(s);
incr/decr;
}
Here, statement(s) may be a single statement or a block of statements.
The condition may be any expression.
The loop iterates while the condition is true.
When the condition becomes false, the program control passes to the line immediately following the loop.
Do-while loop:
For loop:
A for loop is used for executing a block of statements repeatedly until a given condition
returns false.
Syntax:
Step 1: First initialization happens and the counter variable gets initialized.
Step 2: In the second step the condition is checked, where the counter variable is tested for the given
condition, if the condition returns true then the C statements inside the body of for loop gets executed, if
the condition returns false then the for loop gets terminated and the control comes out of the loop.
Step 3: After successful execution of statements inside the body of loop, the counter variable is
incremented or decremented, depending on the operation (++ or –).