Switch is a composite statement used to make a decision between many alternatives. The selection alternatives, known as case label. Switch statement is an alternative to the if-else-if statement. Switch statement is used for menu driven programming.
Syntax:-
switch(<choice-number>)
{
Case <choice-number1>: statement;
Case <choice-number2>: statement;
default:statement;
Case <choice-number3>: statement;
}
In the switch statement we can give choice number as a integer or character. We cannot use float or double or string. For choice number we can use value or variable-name or expression. Choice-numbers we can specify in any order. We can give choice number as a integer or character. We cannot use float or double or string. For choice number we can use value or expression. Default is optional. Multiple defaults are not supported in a single switch. A switch can be write inside a switch. Switch is special selection statement also known as multiway selection statement.
Break statement:- Break statement is used to come out from the block. If you use Break statement in switch statement that causes the program to jump out of the switch statement. We can use break statement in the switch case, Controls statements(while, do-while, for) also.
/* Write a Program read color first character name and print corresponding color names */
#include<stdio.h>
main()
{
char c;
clrscr();
printf("Enter first character in color name :");
scanf("%c",&c);
switch(c)
{
case 'r':
case 'R':
printf("\nRed");
break;
case 'g':
case 'G':
printf("\nGreen");
break;
case 'b':
case 'B':
printf("\nBlue");
break;
default:
printf("\nNo Matching Colors");
}
}
/*WAP read a no or year from the user, perform the following operations based on corresponding choice.
1. Odd/Even
2. Leap Year (or) Not
3. +ve (or) –ve
4. Exit*/
#include<stdio.h>
main()
{
int ny,ch;
clrscr();
printf("\n1.Odd / Even\n");
printf("2.Leap Year (or) Not \n");
printf("3.+Ve (or) -Ve\n");
printf("4.Exit");
printf("\nEnter Your choice");
scanf("%d",&ch);
if(ch>3)
exit(0);
else
printf("\nEnter a number / Year");
scanf("%d",&ny);
switch(ch)
{
case 1:
if(ny%2==0)
printf("\n %d is Even.",ny);
else
printf("\n %d is Odd.",ny);
break;
case 2:
if(ny%4==0&&ny%100==0&&ny%400==0)
printf("\n %d is a Leap Year",ny);
else
printf("\n %d is not a Leap Year",ny);
break;
case 3:
if(ny>0)
printf("\n %d is +ve",ny);
else
printf("\n %d is -ve",ny);
break;
}
getch();
}
Leap Year Rules
There is a leap year every year whose number is perfectly divisible by four - except for years which are both divisible by 100 and not divisible by 400. The second part of the rule effects century years. For example: the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not.
{
Case <choice-number1>: statement;
Case <choice-number2>: statement;
default:statement;
Case <choice-number3>: statement;
}
In the switch statement we can give choice number as a integer or character. We cannot use float or double or string. For choice number we can use value or variable-name or expression. Choice-numbers we can specify in any order. We can give choice number as a integer or character. We cannot use float or double or string. For choice number we can use value or expression. Default is optional. Multiple defaults are not supported in a single switch. A switch can be write inside a switch. Switch is special selection statement also known as multiway selection statement.
Break statement:- Break statement is used to come out from the block. If you use Break statement in switch statement that causes the program to jump out of the switch statement. We can use break statement in the switch case, Controls statements(while, do-while, for) also.
/* Write a Program read color first character name and print corresponding color names */
#include<stdio.h>
main()
{
char c;
clrscr();
printf("Enter first character in color name :");
scanf("%c",&c);
switch(c)
{
case 'r':
case 'R':
printf("\nRed");
break;
case 'g':
case 'G':
printf("\nGreen");
break;
case 'b':
case 'B':
printf("\nBlue");
break;
default:
printf("\nNo Matching Colors");
}
}
/*WAP read a no or year from the user, perform the following operations based on corresponding choice.
1. Odd/Even
2. Leap Year (or) Not
3. +ve (or) –ve
4. Exit*/
#include<stdio.h>
main()
{
int ny,ch;
clrscr();
printf("\n1.Odd / Even\n");
printf("2.Leap Year (or) Not \n");
printf("3.+Ve (or) -Ve\n");
printf("4.Exit");
printf("\nEnter Your choice");
scanf("%d",&ch);
if(ch>3)
exit(0);
else
printf("\nEnter a number / Year");
scanf("%d",&ny);
switch(ch)
{
case 1:
if(ny%2==0)
printf("\n %d is Even.",ny);
else
printf("\n %d is Odd.",ny);
break;
case 2:
if(ny%4==0&&ny%100==0&&ny%400==0)
printf("\n %d is a Leap Year",ny);
else
printf("\n %d is not a Leap Year",ny);
break;
case 3:
if(ny>0)
printf("\n %d is +ve",ny);
else
printf("\n %d is -ve",ny);
break;
}
getch();
}
Leap Year Rules
There is a leap year every year whose number is perfectly divisible by four - except for years which are both divisible by 100 and not divisible by 400. The second part of the rule effects century years. For example: the century years 1600 and 2000 are leap years, but the century years 1700, 1800, and 1900 are not.
No comments:
Post a Comment