Find the Errors (Switch Case)
These code snippets will help you understand the requirement of debugging a program.
Without a program being debugged, they generate errors. And the heart of debugging lies in the process to find out what causes these errors.
//1.
main()
{
int suite=1;
switch (suite);
{
case 0:
printf("\nClub");
case 1:
printf("\nDiamond");
}
}
//2.
main()
{
int temp;
scanf ("%d", &temp);
switch (temp)
{
case (temp<=20):
printf("\nOooooooohhhhh! Damn Cool!");
case (temp>20 && temp<=30):
printf("\nRain rain here again! ");
case (temp>30 && temp<=40):
printf("\nWish I am on Everest");
default:
printf("\nGood old nagpur weather");
}
}
//3.
main()
{
float a=3.5;
switch (a)
{
case 0.5:
printf("\nThe art of C");
break;
case 1.5:
printf("\nThe spirit of C");
break;
case 2.5:
printf("\nSee through C");
break;
case 3.5:
printf("Simply C");
}
}
//1)the error lies in the semicolon after the switch statement.
//2)This program cannot execute cuz a condition cannot be specified within a case
//statement.
//3)floating point numbers are not allowed in case statements.
The File can be downloaded at:
Download File
Without a program being debugged, they generate errors. And the heart of debugging lies in the process to find out what causes these errors.
//1.
main()
{
int suite=1;
switch (suite);
{
case 0:
printf("\nClub");
case 1:
printf("\nDiamond");
}
}
//2.
main()
{
int temp;
scanf ("%d", &temp);
switch (temp)
{
case (temp<=20):
printf("\nOooooooohhhhh! Damn Cool!");
case (temp>20 && temp<=30):
printf("\nRain rain here again! ");
case (temp>30 && temp<=40):
printf("\nWish I am on Everest");
default:
printf("\nGood old nagpur weather");
}
}
//3.
main()
{
float a=3.5;
switch (a)
{
case 0.5:
printf("\nThe art of C");
break;
case 1.5:
printf("\nThe spirit of C");
break;
case 2.5:
printf("\nSee through C");
break;
case 3.5:
printf("Simply C");
}
}
//1)the error lies in the semicolon after the switch statement.
//2)This program cannot execute cuz a condition cannot be specified within a case
//statement.
//3)floating point numbers are not allowed in case statements.
The File can be downloaded at:
Download File
Comments
http://www.a2zhacks.blogspot.com to learn C,C++,VB programming.