0% found this document useful (0 votes)
6 views11 pages

TTS Module7-CC102

COMPUTER PROGRAMMING

Uploaded by

darwinteberio0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
6 views11 pages

TTS Module7-CC102

COMPUTER PROGRAMMING

Uploaded by

darwinteberio0
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 11

SELF-PACED LEARNING

MODULE
College
INFORMATION SHEET MD-2.1.1
“Switch Statement”
The Switch Statement

The switch statement is a multiple-branch decision statement.


The general form of the switch statement is:
switch (variable)
{
case constant1:
statement sequence_1;
break;
case constant2;
statement sequence_2;
break;
:
:
default:
statementsequence_default;
}

In a switch statement, a variable is successively tested against a list of integer or character


constants. If a match is found, a statement or block of statements is executed. The default part of the
switch is executed if no matches are found.

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

The general form of the nested switch statement is:

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

INFORMATION SHEET MD-2.2.1


“Iterative Statements”
Iterative statements (loops) allow a set of instructions to be executed or performed several times until
certain conditions are met. It can be predefined as in the for loop, or open-ended as in while and do-
while.
The looping statement is a program instruction that repeats some statement or sequence of
statements in a specified number of times.
The three looping statements of C programming language are:
1. for loop
2. while loop
3. do-while loop

The FOR statement


The forstatement or forloop is considered as a predefined loop because the number of times it
iterates to perform its body is predetermined in the loop’s definition.
The forloop contains a counter whose values determine the number of times the loop iterates.
The iteration stops upon reaching the number of times specified in the loop

The general form of the for loop statements is:


for (initialization; condition; increment/decrement/acc)
{
statement_sequence;
}
where:
for is reserve word in Turbo C
initialization is an assignment statement that is used to set the loop’s counter
condition is a relational Boolean expression that determines when the loop will exit
increment defines how the loop’s counter will change each time the loop is repeated
Increment Ex: i++ or ++I, Decrement Ex: i—or --i Accumulator Formula: X = X+n

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

4. #include<stdio.h> Example Output:


main() Enter N: 5
{ 1
int x,n; 2
clrscr(); 3
4
5
printf(“Enter N:”);scanf(“%d”,&n);
for (x=1;x<=n;x++)
printf(“%d\n”,x);
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

SELF CHECK MD-2.1.1

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: __________________

PERFORMANCE TASK MD-2.1.1


PERFORMANCE TASK TITLE: Looping Program

PERFORMANCE OBJECTIVE: The learners independently demonstrate core competencies about


Iterative Statement
MATERIALS:
Computer / Android phone
Applications / Software:
Computer: TurboC2
Android phone: Coding C
Procedure:
Create the program of the following pattern output using for loop statement. (screenshot the
output and code)
A. 5
54
543
5432
54321
B. 12345
1234
123
12
1
C. 55555
4444
333
22
1
D. 1
22
333
4444
55555
PRECAUTIONS:
 Undertake final inspections to ensure the program conform to requirements
ASSESSMENT METHOD: PERFORMANCE TASK CRITERIA CHECKLIST
STUDENT NAME: __________________________________ SECTION: __________________

PERFORMANCE TASK CRITERIA CHECK LIST MD-2.1-1


SCORING
CRITERIA
1 2 3 4 5
1. Quality of Work – the ability to follow procedures with precision and the
art, skill and finality of work
2. Speed – efficiency of work
3. Proper use of statement – the ability to apply proper statement for a
given task
TEACHER’S REMARKS:  QUIZ  RECITATION  PROJECT
GRADE:

5 - Excellently Performed
4 - Very Satisfactorily Performed
3 - Satisfactorily Performed
2 - Fairly Performed
1 - Poorly Performed

_________________________________
TEACHER

Date: ______________________

You might also like