written 6.0 years ago by | • modified 6.0 years ago |
Subject : Structured Programming Approach
Title : Control Structures
Marks : 10M
written 6.0 years ago by | • modified 6.0 years ago |
Subject : Structured Programming Approach
Title : Control Structures
Marks : 10M
written 6.0 years ago by |
C language supports the following statements known as control or decision making statements.
if Statement
The if statement is used to control the flow of execution of statements and is of the form
if(test expression)
Statement;
It allows the computer to evaluate the expression first and then, depending on whether the value of the expression is ‘true’ or ‘false’, it transfers the control to a particular statement. Eg: if(bank balance is zero)
Borrow money;
The If…else Statement
The if….else statement is an extension of simple if statement.The general form is
if(test expression)
{
True-block statement(s)
}
else
{
False-block statement(s)
}
statement-x
If the test expression is true, then the true block statements are executed; otherwise the false block statement will be executed.
Nesting Of If…..else Statements
When a series of decisions are involved, we may have to use more than one if….else statements, in nested form as follows.
if(test condition 1)
{
if(test condition 2)
{
statement-1;
}
else
{
statement-2;
}
}
else { statement-3; } statement-x;
The Elseif Ladder
The general form is
if(condn 1)
statement-1;
else if (condn 2)
statement-2;
else if (condn 3)
statement-3;
else if (condn n)
statement-n;
else
default statement;
statement-x;
The Switch Statement
Switch statement is used for complex programs when the number of alternatives increases. The switch statement tests the value of the given variable against the list of case values and when a match is found, a block of statements associated with that case is executed.
The general form of switch statement is
switch(expression)
{
case value-1:
block-1
break;
case value-2:
block-2
break;
…….
…….
default:
}
statement-x;