Saturday 11 February 2012

Conditional statements (else, if, else-if, switch case)


Conditional statement

It is an instruction to the computer to execute an instruction based on truthness and falsity of the condition provided in the statement.
c\c++ provides a variety of conditional control statements:
1)if-statement
2)if-else statement
3)nested-if-statement
4)swith statement

If statement

This is used to execute a statement or a set of statements conditionally.it is simple if-statement.
Syntax:                  if(condition)
                         {
                             Statement;
                                 }
Where
condition : is a logical expression that results in true or false
statement: a simple or a compound statement
in this case you just check  for a condition to be true or false based on a single condition.
Example:
#include<stdio.h>
main()
{
Int number;
Printf(“enter the number\n”);
Scanf(“%d”,&number);
If((number%2)!=0)
{
Printf(“%d,is an odd number\n”,number);
}
}
Output of the program will be:
Enter the number
13
13,is an odd number

<h3>If-else statement</h3>
As stated earlier if statement is used to execute only one action.if there are two statements to be executed alternatively then if-else statement is used.
Syntax:        
   if(condition)
{
Statement1;
}
Else
{
Statement2;
}
In this case you check for a condition to be true , if it is then it execute statement1 otherwise it execute statement2. This is also based on a single condition. But difference is that if else statement execute else part  when condition is not true but in if-statement  when the condition is not true then processor skip that instruction or program will terminate.
Example:
#include<stdio.h>
main()
{
Int m,n;
Printf(“enter the values of m and n\n”);
Scanf(“%d%d”,&m,&n);
If(m==n)
{
Printf(“m and n are not equal\n”);
}
Else
{
Printf(“m and n are equal\n”);
}
}
Output of the program will be:
enter the values of m and n
26
23
m and n are not equal

<h3>nested if -statement</h3>
if there are more than two alternatives to  select then nested if-statement are used. Like we are comparing numbers to find largest then we have to compare a number to all other number then there will be use of nested if- statement.
Syntax:
If(condition1)
{
If(condition2)
{
Statement1;
}
Else
{
Statement2;
}
}
Else if(condition3)
{
Statement3;
}
Else
{
Statement4;
}
In the above syntax, statement1 is executed if condition1 and condition2 are true.if condition1 is true and condition2 is false then  statement2 will executed. if condition1 is false then control move to else3 if part of the program and tests for the condition3. And if condition3 is true then statement3 will executed, otherwise statement4 is executed.if both condition 1 and condition3 are false, control com out of nested if-statement.
Example:
 #include<stdio.h>
main()
{
Int a,b,c;
Printf(“enter the values of a,b,c\n”);
Scanf(“%d%d%d”, &a, &b, &c);
If(a>b)
{
If(a>c)
{
Printf(“a is largest\n”);
}
Else
{
Printf(“c is largest\n”);
}
Else if(b>c)
{
Printf(“b is largest\n”);
}
Else
{
Printf(“c is largest\n”);
}
}
Output of the program will be:
enter the value of a,b,c
34
56
78
c is largest

Switch case statement

It is a multiple branch selection statement,which successively tests the value of an expression againt a list of integer a constants.
In this case we are comparing  situation with the situation stored in blocks of switch case. It is just like a linear search in an array where situations are stored in the form of cases in switch case  and comparing situation is compared with all the cases of switch case.
Syntax:
Switch(expression)
{
Case value1:
Statement;
Statement;
Break;
Case value2:
Statement;
Statement;
Break;
.
.
.
Case value n:
Statement;
Statement;
Break;
Default;
Statement;
Statement;
}
Note: the constants in the case of statement can be character or integer  only.
The control will be in that case until the break statement or end of switch statement is reached.there is default block  for all those cases which are not match with any one of the cases provided in switch cases.

Break statement:

This statement is used to control flow of the case labels, when provided situation is match with any of the situation stored in cases of switch cases then to execute that particular statement and get terminate from  the switch case statement that is move out of curly braces at the end of switch statement.
If there is no break statement then program will execute statement of all the cases of switch case.
Example of a switch case statement:
#include<stdio.h>
#include<string.h>
#include<ctype.h>
main()
{
Char remarks[15];
Char grade;
Printf(“enter the grade\n”);
Scanf(“%c”,&grade);
Grade=upper(grade);
Switch(grade)
{
Case ‘S’:
Strcpy(remark,”SUPER”);
Break;
Case ‘A’:
Strcpy(remark,”VERY GOOD”);
Case ‘B’:
Strcpy(remark, “FAIR”);
Break;
Case ‘Y’:
Strcpy(remark,”ABSENT”);
Break;
Default:
Strcpy(remark, “ERROR IN GRADE”);
}
Printf(“RESULT: %s\n”,remark);
}
Output of the program will be:
enter the grade
S
RESULT: SUPER

Go to statement:

This statement is to transfer the control from one point to another program. goto is a branching statement requires a label.
Syntax:
goto label;
where
goto  is a keyword
label is a symbolic constant written either in upper case or in lower case.
Example :
#include<stdio.h>
main()
{
Int number,rev=0,digit,temp;
Printf(“enter trhe number\n”);
Scanf(“%d”,&number);
Temp=number;
START: digit=number%10;
rev=rev*10+digit;
number/10;
if(number>0)
goto start;
printf(“input number=%d\n”, temp);
}
Output of the program:
9876
Input number = 9876
Output number = 6789

No comments:

Post a Comment