c language - switch and for loop
DESCRIPTION
Switch and For Loop Explained using C Language.TRANSCRIPT
Switch Statement SYNTAXswitch(variable||constant||character){case 1:statement1;statement2;break;case 2:statements;break;..default:statements;}
Example..void main(){char ch=‘x’;
switch(ch){case ’v’:printf(“In case v ”);break;case ’a’:printf(“In case a ”);break;case ’x’:printf(“In case x ”);break;default:printf(“I am in default\n”);}
}
•Switch statement can also contain Expressionseg switch(i+j*k) switch(45*j)
•Only constant expressions can be evaluated in caseeg case 5+9 is correct. case a+b is incorrect
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
The Loop Control Structure
For Loop While LoopDo-While Loop
for Loop
for Loop
• Syntax void main(){for(initialization; condition;increment/decrement){statements;}}
for LoopA 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);}
for LoopSome more formats.
• void main(){int i;
for(i=1;i<=10;i=i+1)printf(“%d\n”,i);
}
• void main(){int i;
for(i=1;i<=10;){printf(“%d\n”,i);i=i+1;}
}
for LoopSome more formats.
• void main(){int i=1;
for(;i<=10;i=i+1)printf(“%d\n”,i);
}
• void main(){int i=1;
for(;i<=10;){printf(“%d\n”,i);i=i+1;}
}
for LoopSome more formats.
• void main(){int i;for(i=0;i++<10;)printf(“%d\n”,i);}
• void main(){int i;for(i=0;++i<=10;){printf(“%d\n”,i);}}
Nested for loop• 1*1• 1*2• 2*1• 2*2• 3*1• 3*2
Nested for loopvoid main(){int i,j;for(i=1;i<4;i++){for(j=1;j<=2;j++){printf(“%d\n”,i*j)}}
Multiple Initializations in for loop
• for(i=1,j=2;j<=10;j++,i++)
Break Statement
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”);}
Continue statementvoid 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 5\n" );}
Output
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!!”);}
}
Predict the outputvoid 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();}
Predict the output..• void main(){• int ch='a'+'b';• printf("%c\n",ch);• switch(ch)• {• case 'a':• case 'b':• printf("u entered b\n");• case 'A':• printf("u entered A\n");• case 'b'+'a':• printf("u entered b and a\n");• default:• printf("Reached default");• }• }
Output???
• void main(){char ch='a';
printf("%d",ch);}
Output????
• 97
void main(){int ch=3;printf(“%c”,ch);}
Ans ♥
Output????void main(){char c=1;switch(c){case '1':printf("In '1'");break;case 1||2:printf(" In 1 or 2");break;default:printf("Not found");}}
• ANS • In 1 or 2
Output???void main(){int suite=1;
switch(suite);{case 0;printf("Club\n");case 1;printf("Diamond\n");}
}
Output
Output????void main(){
switch('A'+1){
case 66: printf("In 66"); break; case 1+'A': printf("In same") ; break; case 'B': printf("In B") ; break; default: printf("In default"); break;
}}