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

Comprog 11 Switch Statement

The document explains the C switch statement as an alternative to if-else-if, detailing its rules and providing an example program that outputs the day of the week based on user input. It also differentiates between if-else and switch statements, highlighting their respective functionalities and use cases. Additionally, it describes when to use each statement based on the evaluation of conditions and execution flow.
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)
9 views

Comprog 11 Switch Statement

The document explains the C switch statement as an alternative to if-else-if, detailing its rules and providing an example program that outputs the day of the week based on user input. It also differentiates between if-else and switch statements, highlighting their respective functionalities and use cases. Additionally, it describes when to use each statement based on the evaluation of conditions and execution flow.
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/ 3

Name: John Andrei H.

Rivera

Subject: CC-Comprog11

1. C Switch Statement :

The switch statement in C is an alternate to if-else-if ladder statement which allows us to execute

multiple operations for the different possibles values of a single variable called switch variable.

Rules for switch statement in C language

1) The switch expression must be of an integer or character type.

2) The case value must be an integer or character constant.

3) The case value can be used only inside the switch statement.

4) The break statement in switch case is not must. It is optional. If there is no break statement found

in the case, all the cases will be executed present after the matched case. It is known as fall through

the state of C switch statement.

2. Example of Switch statement

#include<stdio.h>

int main()

int daynumber;

printf("Enter number from 1 to 7:");

scanf("%d", &daynumber);

switch (daynumber){

case 1:

printf("Sunday");

break;

case 2:

printf("Monday");

break;

case 3:

printf("Tuesday");

break;

case 4:
printf("Wednesday");

break;

case 5:

printf("Thursday");

break;

case 6:

printf("Friday");

break;

case 7:

printf("Saturday");

break;

default:

printf("Invalid number");

return 0;

Output;

Enter number from 1 to 7:5

Thursday

...Program finished with exit code 0

Press ENTER to exit console.

3. Differentiate the If… Else statement and Switch statement.

If-else Switch
Definition Depending on the condition in the 'if' The user will decide which statement
statement, 'if' and 'else' blocks are is to be executed
executed.
Expression It contains either logical or equality It contains a single expression which
expression. can be either a character or integer
variable.
Evaluation It evaluates all types of data, such as It evaluates either an integer, or
integer, floating-point, character or character.
Boolean.
Sequence of execution First, the condition is checked. If the It executes one case after another till
condition is true then 'if' block is the break keyword is not found, or the
executed otherwise 'else' block default statement is executed.
Default execution If the condition is not true, then by If the value does not match with any
default, else block will be executed. case, then by default, default
statement is executed.

4. Explain when to use an If… Else statement and when to use the switch statement.

Functioning of switch case statement: First, the integer expression specified in the switch statement is evaluated. This
value is then matched one by one with the constant values given in the different cases. If a match is found, then all the
statements specified in that case are executed along with the all the cases present after that case including the default
statement. No two cases can have similar values. If the matched case contains a break statement, then all the cases
present after that will be skipped, and the control comes out of the switch. Otherwise, all the cases following the matched
case will be executed.

Functioning of "if-else" statement in programming executes different blocks of code based on whether a condition is
true or false. If the condition in the if statement is true, the code within the if block executes; otherwise, the code within
the else block executes.
Here's a breakdown:
 if statement: This initiates the conditional check, evaluating a specified condition.
 Condition: This is an expression that evaluates to either true or false.
 if block: This is the code that executes only if the condition is true.
 else statement: This is an optional part that specifies code to execute if the condition in the if statement is false.
 else block: This is the code that executes only if the condition in the if statement is false.

You might also like