c language - switch and for loop

32

Upload: sukrit-gupta

Post on 25-May-2015

145 views

Category:

Education


2 download

DESCRIPTION

Switch and For Loop Explained using C Language.

TRANSCRIPT

Page 1: C Language - Switch and For Loop
Page 2: C Language - Switch and For Loop

Switch Statement SYNTAXswitch(variable||constant||character){case 1:statement1;statement2;break;case 2:statements;break;..default:statements;}

Page 3: C Language - Switch and For Loop

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”);}

}

Page 4: C Language - Switch and For Loop

•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

Page 5: C Language - Switch and For Loop

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

Page 6: C Language - Switch and For Loop
Page 7: C Language - Switch and For Loop

The Loop Control Structure

For Loop While LoopDo-While Loop

Page 8: C Language - Switch and For Loop

for Loop

Page 9: C Language - Switch and For Loop

for Loop

• Syntax void main(){for(initialization; condition;increment/decrement){statements;}}

Page 10: C Language - Switch and For Loop

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);}

Page 11: C Language - Switch and For Loop

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;}

}

Page 12: C Language - Switch and For Loop

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;}

}

Page 13: C Language - Switch and For Loop

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);}}

Page 14: C Language - Switch and For Loop

Nested for loop• 1*1• 1*2• 2*1• 2*2• 3*1• 3*2

Page 15: C Language - Switch and For Loop

Nested for loopvoid main(){int i,j;for(i=1;i<4;i++){for(j=1;j<=2;j++){printf(“%d\n”,i*j)}}

Page 16: C Language - Switch and For Loop

Multiple Initializations in for loop

• for(i=1,j=2;j<=10;j++,i++)

Page 17: C Language - Switch and For Loop

Break Statement

Page 18: C Language - Switch and For Loop

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”);}

Page 19: C Language - Switch and For Loop

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" );}

Page 20: C Language - Switch and For Loop

Output

Page 21: C Language - Switch and For Loop

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!!”);}

}

Page 22: C Language - Switch and For Loop

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();}

Page 23: C Language - Switch and For Loop

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");• }• }

Page 24: C Language - Switch and For Loop
Page 25: C Language - Switch and For Loop

Output???

• void main(){char ch='a';

printf("%d",ch);}

Page 26: C Language - Switch and For Loop

Output????

• 97

void main(){int ch=3;printf(“%c”,ch);}

Page 27: C Language - Switch and For Loop

Ans ♥

Page 28: C Language - Switch and For Loop

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");}}

Page 29: C Language - Switch and For Loop

• ANS • In 1 or 2

Page 30: C Language - Switch and For Loop

Output???void main(){int suite=1;

switch(suite);{case 0;printf("Club\n");case 1;printf("Diamond\n");}

}

Page 31: C Language - Switch and For Loop

Output

Page 32: C Language - Switch and For Loop

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;

}}