Tuesday, December 23, 2008

Condition Statement

Decision statement:-
The decision statement is the statement which is used to take certain decisions.
The general statement used is ‘IF’ statement. General form of this statement is
If(condition)
Execute the statement.
The other form is
If(condition)
Execute this statement
Else
Execute the other statement
The operators used in ‘IF’ statement are as follows:-
Let a & b be the statements for checking the conditions.

a= =b: - a equal to b
a! =b: - a not equal to b
a> b: - a greater than b
a< b: - a less than b
a<= b: - a less than or equal to b
a>= b: - a greater than or equal to b

‘If’ condition may contain other if conditions in it. Other ‘else if’ condition can also be used. i.e after if condition ‘else’ is used and if there is a condition other than the first condition then the ‘else if’ condition is used
For the other condition and at last else statement is used for the rest of the conditions.
We will see an example.

void main ()
{
Int x,y,z;
Printf(“Enter the mark of first subjects“);
Scanf(“%d”,&x);
Printf(“Enter the mark of second subject”);
Scanf(“%d”,&y);
If(x>y)
Printf(“x is greater than y”);
Else
Printf(“y is greater than x”);
Getch();
}
In the above program the marks of two subjects is taken. Then the greater number is calculated. Then it is displayed.
The other thing is that an else statement can have a ‘if’ or if…else statement.
Instead of using only a ‘if’ statement in a ‘else’ statement we can use a ‘else if’ statement. Condition should be given in the if statement as above.
Eg. If
Statement
Else if(condition)
Statement
Else if(condition)
Statement
Else
We can give a number of ‘else if’ statement .
Eg.
Void main()
{
Int x[2],y;
Printf(“Enter 2 values”);
For(y=0;y<2;y++)
{
Scanf(“%d”,&x[y]);
}
If(x == y)
{
printf(“X is equal to y”);
}
Else if(x < y)
{
Printf(“X is less than Y”);
}
Else
{
Printf(“Y is less than X”);
}
Getch();
}

No comments:

Watch tv!