click on this link......

LinkGrand.com

Friday, 25 January 2013

Decision Control Structure

In the start of learning C, We use sequence control structure, in which various steps are executed sequentially i.e. in the same order in which they appear in the program. In fact to execute the instructions sequentially, we don't have to do anything at all. By default instructions in a program are executed sequentially. However, in serious programming situations, seldom do we want the instructions to be executed sequentially. Many a times we want a set of instructions to be executed in one situation and an entirely different set of instructions to be executed in another situation. This kind of situation is dealt in C programs using a decision control instruction. As mentioned earlier a decision control instruction can be implemented in C using:
1) The if statement
2) The if - else statement

1) The if statement :

Like most languages, the keyword if to implement the decision control instruction. The general form of if statement looks like this:

if(this condition is true)
{
           execute this statement;
}

The keyword if tells compiler that what follows, is a decision control instruction. The condition following keyword if is always enclosed within a pair of parenthesis ( ). If the condition, whatever it is true then the statement is executed and if the condition is not true then the statement is not executed, instead the program skips it. 

As a general rule, we express a condition using C's relational operation. the relational operators allow us to compose 2 values to see whether they are equal to each other, unequal or whether one is greater than or less than the other. Here's how they look and how they are evaluated in C

Expression                        is trueif
x == y                           x is equal to y
x !=y                             x is not equal to y
x < y                             x is less than y
x > y                             x is greater than y
x <= y                           x is less than or equal to y  
x >= y                           x is greater than or equal to y

2) If  - else statement: 

If the condition is true, it executes one set of statements and if the condition is false, it executes another set of instructions.

if(condition)
{
        statements;
}
else
{
        statements;
}

No comments:

Post a Comment