Sunday, January 4, 2009

if else,switch case QUESTION of C PROGRAMMING LANGAGE



Control strcture:
if else
Syntax:
1.
If( )
{
Statement1;
Statement 2;
……………
…………….
}
2.If( )
{
Statement1;
Statement 2;
……………
…………….

}
else
{
Statement1;
Statement 2;
……………
…………….

}
3.
If( )
{
Statement1;
Statement 2;
……………
…………….
}
else If( )
{
Statement1;
Statement 2;
……………
…………….
}
else If( )
{
Statement1;
Statement 2;
……………
…………….
}
………………..
………………...

else
{
Statement1;
Statement 2;
……………
…………….
}

If is non zero then condition is true and if part of statement will execute , if is zero then only else part will execute.
e.g 
void main()
{
if(-12)
{
printf(“c”);
else
{
printf(“c++);
}
}
Output: c
Explanation: Here expression is -12 it is non zero so condition is true hence only if statement will execute .
Note. If there is only one statement is if or else part then { } is optional 
Above program can be written as:
void main()
{
if(-12)
printf(“c”);
else
printf(“c++);
}
Both program has same meaning.
Relation operator (>,>=,<,<=,==,!=) always return
0 if condition is true
1 if condition is false
e.g 
void main()
{
int a=0;
if(a==0)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: I know c very well.
Explanation : Relation expression a==0 i.e 0==0 is true so it will return 1 So only if part will execute.
Logical error : Suppose any good c programmer when he is writing a very long program by careless he forget to write two equal sign :

void main()
{
int a=0;
if(a = 0)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}

In this program there is not any compiler error but its out put will else part ie I am c programmer ,due to logical error.
To remove such type error try to write the if expression in the following way:
void main()
{
int a=0;
if(0==a)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: I know c very well.

Now if you will by mistake forget to write two equal sign then it is compiler error.
void main()
{
int a=0;
if(0 = a)
printf(“I know c very well”);
else
printf(“I am c programmer”);
}
Output: compiler error
What will be output:
void main()
{
int a=60;
clrscr();
if(a>20)
printf("c");
else if(a>40)
printf("c++");
else if(a>=60)
printf("java");
else
printf("pascal");
getch();
}

Output: c
Explanation : In if else control statement if if statement is true then it does not look else i part event that is true.So in this program all if condition is true but only statement of first if has executed.


If statement can be nested i.e we can write if statement within another if statement i.e
.if( )
{
Statement1;
Statement 2;
……………
…………….
.if( )
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
}
else
{
Statement1;
Statement 2;
……………
…………….
if( )
{
Statement1;
Statement 2;
……………
…………….
}
else
{
Statement1;
Statement 2;
……………
…………….
}
}
What will be output:

void main()
{
int a=4,b=6c=9;
if(a==4)
if(b==6)
if(9==c)
printf(“Yes”);
}
Output: Yes
It is better to use logical and (&&) operator instead of nested if for easier getable. 

void main()
{
int a=4,b=6c=9;
if(a==4&&6==b&&c==9)
printf(“Yes”);
}
Output: Yes


Switch case control statement :
It is used when we have to choose one option from list of many options. 
Syntax:
switch ()
{
case : 
Statement 1;
Statement 2;
………………
………………
case : 
Statement 1;
Statement 2;
………………
………………

……………………………………….
……………………………………….
default : 
Statement 1;
Statement 2;
………………
………………
}

Note :
1. Here default is optional i.e It is possible a switch case control statement which has not default.If not any case is matching then default will execute. Also all case is optional i.e it is possible a switch case which has not any case and not any default .
Example:
void main()
{
int a=6;
switch(a)
{
}
}
2. must be int type and unique 
Example:
void main()
{
int a=3;
clrscr();
switch(a)
{
case 1,2,3: printf("I knwon java");
break;
case 3,2,1: printf("I know c++");
break;
case 2,1,3: printf("I know c");
break;
default: printf("I know pascal");
break;
}
getch();
}
Output : compiler error
Explanation : Here in each constant expression has three integers which are seprated by comma operator .Comma operator always return right most expression ( here integer ).
So 1,2,3 will return 3
3,2,1 will return 1
2,1,3 will return 3
Since here two case has 3 but must be unique.

What will be output:

void main()
{
clrscr();
switch(2)
{
case 4: printf("I knwon java");
case 2: printf("I know c++");
case 5: printf("I know c");
default: printf("I know pascal");
}
getch();
}
Output :
I know c++
I know c
I know pascal
Explanation:
In switch case control statement after executing matching case is control dose not come outside of switch but it transfer to the next case and execute its statement till it does not encounter switch ending }. 
Note: To exit from switch use break keyword. 
Example;
void main()
{
clrscr();
switch(2)
{
case 4: printf("I knwon java");
break;
case 2: printf("I know c++");
break;
case 5: printf("I know c");
break;
default: printf("I know pascal");
}
getch();
}
Output: I know c++;

What will be output:
void main()
{
clrscr();
switch(2)
{
case 1: printf("I knwon java");
break;
case 2: printf("I know c++");
break;
case 3: printf("I know c");
continue;
default: printf("I know pascal");
continue;
}
getch();
}
Output:
Compiler error.
Explanation: continue keyword cannot be used in switch case. It is used in loop. If there is any loop in switch case then we can use continue.
Nesting of case is possible i.e a switch case can be inside another switch case.

(1) WAP to convert the following program in switch ?
void main()
{
int a=6;
if(a>27)
printf(“c);
else
printf(c++);
}

Ans: If else statement has only two case either true or false. Expression a>27 will return 1 if is true and 0 when expression is false .So we can write :
void main()
{
int a=6;
switch(a>27)
{
case 1: printf(“c”);
case 2: printf(“c++”);
}
}

No comments:

Blog List