4. •Switch statement can also contain Expressions
eg switch(i+j*k)
switch(45*j)
•Only constant expressions can be evaluated in
case
eg case 5+9 is correct.
case a+b is incorrect
5. Limitation of switch
• A float Expression cannot be tested using a switch.
• Multiple cases cannot use same expression values.
eg
switch(var)
{
case 6:
…………..
.
.
case 3*2:
}
It is an Illegal Expression
• case a>2: is an illegal expression
10. for Loop
A Basic program:- Take 5 numbers from user and
find their sum.
void main()
{
int a,i,sum;
printf(“Enter Five number”);
for (i=0;i<5;i++)
{
scanf(“%d”,&a);
sum=sum+a; //
}
printf(“The sum is %d”,sum);
}
11. for Loop
Some more formats.
• void main()
{
int i;
for(i=1;i<=10;i=i+1)
printf(“%dn”,i);
}
• void main()
{
int i;
for(i=1;i<=10;)
{
printf(“%dn”,i);
i=i+1;
}
}
12. for Loop
Some more formats.
• void main()
{
int i=1;
for(;i<=10;i=i+1)
printf(“%dn”,i);
}
• void main()
{
int i=1;
for(;i<=10;)
{
printf(“%dn”,i);
i=i+1;
}
}
13. for Loop
Some more formats.
• void main()
{
int i;
for(i=0;i++<10;)
printf(“%dn”,i);
}
• void main()
{
int i;
for(i=0;++i<=10;)
{
printf(“%dn”,i);
}
}
18. Break Statement
• main()
{
int num,i;
printf(“Enter a number”);
scanf(“%d”,&num);
i=2;
while(i<=num-1)
{
if(num%i==0)
{
printf(“Not a prime number”);
break;
}
i++;
}
if(i==num)
printf(“Prime number”);
}
19. Continue statement
void main()
{
int x;
for ( x = 1; x <= 10; x++ ) {
if ( x == 5 )
continue; /* skip remaining code in loop only if x == 5 */
printf( "%d ", x );
}
printf( "nUsed continue to skip printing the value 5n" );
}
21. Predict the output:
void main()
{
float a=3;
switch(a)
{
case 1:
printf(“Value is 1”);
break;
case 2:
printf(“Value is 2”);
break;
case 3:
printf(“Value is 3”);
break;
default:
printf(“ERROR!!”);
}
}
22. Predict the output
void main(){
int x=0,y=0,i,j;
for(i=0;i<2;i++);
for(j=1;j<3;j++)
{
x++;
}
y++;
printf("x: %d, y: %d",x,y);
getch();
}
23. Predict the output..
• void main(){
• int ch='a'+'b';
• printf("%cn",ch);
• switch(ch)
• {
• case 'a':
• case 'b':
• printf("u entered bn");
• case 'A':
• printf("u entered An");
• case 'b'+'a':
• printf("u entered b and an");
• default:
• printf("Reached default");
• }
• }