0% found this document useful (0 votes)
3 views

Lab 4

This lab focuses on control flow using switch statements in programming, detailing their syntax, usage, and practical applications. It includes examples demonstrating basic switch statements, character input handling, and the use of enums, along with in-lab and post-lab tasks for practice. The lab emphasizes the importance of mastering switch statements for efficient decision-making in code.

Uploaded by

zainabnovelsonly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Lab 4

This lab focuses on control flow using switch statements in programming, detailing their syntax, usage, and practical applications. It includes examples demonstrating basic switch statements, character input handling, and the use of enums, along with in-lab and post-lab tasks for practice. The lab emphasizes the importance of mastering switch statements for efficient decision-making in code.

Uploaded by

zainabnovelsonly
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

Programming Fundamentals

Lab Number: 4
Title: Control Flow with Switch Statements

Introduction
Control flow structures are essential for decision-making in programming. The switch statement
provides a structured and readable way to handle multiple conditions efficiently. This lab will
explore the syntax, usage, and practical applications of switch statements through examples and
hands-on tasks.

Basics of Syntax and Usage


The switch statement evaluates an expression and executes the corresponding case block based
on its value. It is often used as an alternative to multiple if-else statements when dealing with
discrete values.

General Syntax:
switch(expression) {
case value1:
// Code to execute if expression matches value1
break;
case value2:
// Code to execute if expression matches value2
break;
...
default:
// Code to execute if none of the cases match
}
Key Points:

• The expression must be an integer, character, or enumerated type.

• case values must be constant and unique.

• The break statement prevents fall-through to subsequent cases.

• The default case handles unexpected values (optional but recommended).

Examples Highlighting the Use of Switch Statements


Example 1: Basic Switch Statement
#include <stdio.h>
int main() {
int num = 2;
switch (num) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
case 3:
printf("Three\n");
break;
default:
printf("Invalid number\n");
}
return 0;
}
Output: Two

Example 2: Character Input Handling


#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
case 'C':
printf("Average\n");
break;
default:
printf("Invalid Grade\n");
}
return 0;
}
Example 3: Using Switch with Enums
enum Days { MON, TUE, WED, THU, FRI };
#include <stdio.h>
int main() {
enum Days today = WED;
switch (today) {
case MON:
case TUE:
case WED:
printf("Midweek\n");
break;
case THU:
case FRI:
printf("Weekend is near!\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
An enum (short for enumeration) in C is a user-defined data type that assigns names to a set of
integer constants, making the code more readable and manageable.

Example:
enum Days { MON, TUE, WED, THU, FRI };

Here, MON is assigned 0, TUE is 1, and so on. You can use enum in a switch statement for better
code organization:

Example 4: Switch without Break (Fall-through)


#include <stdio.h>
int main() {
int num = 1;
switch (num) {
case 1:
printf("Case 1\n");
case 2:
printf("Case 2\n");
break;
default:
printf("Default case\n");
}
return 0;
}
Output:

Case 1

Case 2

In-Lab Tasks
1. Basic Calculator: Write a program that takes two integers and an operator (+, -, *, /) as
input and performs the corresponding operation using a switch statement.

2. Days of the Week: Implement a switch statement that takes an integer (1-7) as input
and prints the corresponding day of the week.

3. Vowel or Consonant: Write a program that takes a single letter as input and determines
if it is a vowel or consonant using a switch statement.

4. Menu-Driven Program: Create a simple menu system with 4 options and a default case
to handle invalid inputs.

Post-Lab Tasks
1. Number to Words: Modify Example 1 to handle numbers from 1 to 10 and print their
English word representation.
2. Convert Temperature Units: Write a program where the user inputs a temperature
value and a choice (C for Celsius to Fahrenheit, F for Fahrenheit to Celsius), and the
program performs the conversion using a switch statement.

3. Grade System Enhancement: Extend Example 2 to include more grades (D, F) and print
appropriate feedback messages.

Conclusion

This lab introduced switch statements, their syntax, and practical applications through
examples and exercises. Mastering switch statements allows for efficient multi-way decision-
making in programs, improving both readability and maintainability.

You might also like