TTS Module7-CC102
TTS Module7-CC102
MODULE
College
INFORMATION SHEET MD-2.1.1
“Switch Statement”
The Switch Statement
According to Herbert Schildt (1992), there are three important things to know about switch
statements:
1. The switch differs from if statements in such a way that switch can only test for equality
whereas if can evaluate a relational or logical expression.
2. No two case constants in the same switch can have identical values. Of course, a switch
statement enclosed by an outer switch may have case constants that are the same.
3. If character constants are used in the switch, they are automatically converted to their
integer values.
Note: The break statement is used to terminate the statement sequence associated with each case
constant. It is a Turbo C keyword which means that at that point of execution, you should jump to the
end of the switch statement terminated by the symbol }.
The Nested Switch Statement
switch (variable)
{
case constant1: {
switch (variable)
{
case constant1:
statement sequence;
break;
case constant2:
statement sequence;
break;
}
break;
}
case constant2;
statement sequence_2;
break;
:
:
default:
statement sequence;
}
Example 1.Rewrite a program using switch statement that will ask the user to input an integer then
output the equivalent day of the week. 1 is Sunday, 2 is Monday and so on. If the inputted number is not
within 1-7, output “Day is not available!”
#include<stdio.h>
main()
{
Sample Output:
int day; Enter an integer: 11
clrscr(); Day is not available!
printf(“Enter an integer:”);
scanf(“%d”, &day);
switch (day)
{
case 1:
printf (“Sunday”);
break;
case2:
printf (“Monday”);
break;
case3:
printf (“Tuesday”);
break;
case4:
printf (“Wednesday”);
break;
case5:
printf (“Thursday”);
break;
case6:
printf (“Friday”);
break;
case7:
printf (“Saturday”);
break;
default:
printf (“Day is not available!”);
}
getch();
}
Example 2.Write a program that will ask the user to input 2 integers and a character (A, S, M or D). If the
user inputs ‘A’ for the character, add 2 numbers, if ‘S’ subtract the 2 numbers, if ‘M’ multiply, and if ‘D’
divide the numbers. Output the computed value.
#include<stdio.h>
main()
{
intnum1, num2, ans;
char operation;
clrscr();
printf(“Enter number 1:”);
scanf(“%d”, &num1);
printf(“Enter number 2:”);
scanf(“%d”, &num2);
printf(“A- Addition \n”);
printf(“S- Subtraction \n”);
printf(“M- Multiplication \n”);
printf(“D- Division \n”);
printf(“Enter a character for the operation:”);
scanf(“%c”, &operation);
switch (operation)
{
case‘A’: Sample Output:
ans = num1 + num2 Enter number 1: 10
break; Enter number 2: 25
case‘S’: A – Addition
ans = num1 - num2 S – Subtraction
break; M – Multiplication
case‘M’: D – Division
ans = num1 * num2 Enter a character for the operation: A
break; The answer is: 35
case‘D’:
ans = num1 / num2
break;
}
printf (“The answer is : %d “, ans);
getch();
}
statement_sequence may either be a single Turbo C statement or a block of Turbo C statements that
make up the loop body
The for loop continues to execute until the condition is TRUE(1). Once FALSE(0), program execution
resumes on the statement following the for loop.
Note:
a. Never place a semi-colon right after the for header. This is a logical error.
b. Never change the value of the for loop’s counter inside the body of the loop. This will affect
the result of the program
c. The increment part of the for loop is execute after the first iteration of the loop
Examples
1. Write a program that will print the numbers 1 to 10 using a for statement.
#include<stdio.h>
Output:
1
2
3
4
5
main()
{
int x;
clrscr();
for (x=1;x<=10;x++)
printf(“%d\n”,x);
getch();
}
2. Write a program that will print the first 10 even number using a for statement.
Output:
#include<stdio.h>
2
main()
4
{
6
int x;
8
clrscr();
10
for (x=2;x<=20;x++)
12
printf(“%d\n”,x++);
14
getch();
16
}
18
20
3. Write a program that will get the sum of all integers from 1 to 10
#include<stdio.h>
Output:
intx,sum;
The sum of 1 to 10 is 55
main()
{
int x;
clrscr();
sum=0;
for (x=1;x<=10;x++)
sum=sum+x;
printf(“The sum of 1 to 10 is %d\n”,sum);
getch();
}
5. #include<stdio.h>
main()
{ Example Output:
int x,n,sum; Enter N: 5
clrscr(); The sum of 1 to 5 is 15
printf(“Enter N:”);scanf(“%d”,&n);
for (x=1;x<=n;x++)
sum=sum+x;
printf(“The sum of 1 to %d is %d\n”,n,sum);
getch();
}
6. #include<stdio.h>
main()
{ Example Output:
int x,y; 1
clrscr(); 12
for (x=1;x<=5;x++) 123
{ 1234
for (y=1;y<=x;y++) 12345
{
printf(“%d”,y);
}
printf(“\n”);
}
getch();
}
Reference:
Workbook in C Programming Computer Programming 1 by Paulino H. Gatpandan, Azenith M. Rollan
Write the screen output of the following program segment. Write your answer in the box provided.
#include <stdio.h>
main()
{
m = 6;
n = 0;
for (h=1;h<=m;h++)
{
z=2*h;
n=n+z;
printf(“H=%d”,h);
printf(“Z=%d”,z);
printf(“N=%d”,n);
}
getch();
}
STUDENT NAME: __________________________________ SECTION: __________________
5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed
_________________________________
TEACHER
Date: ______________________