0% found this document useful (0 votes)
32 views8 pages

Exp4 C Program

Uploaded by

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

Exp4 C Program

Uploaded by

VISHWARAJ MALAVE
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Jawahar Education Society’s

A.C. Patil College of Engineering, Kharghar


Department of Humanities and Basic Sciences
Academic Year 2025-26

Aim: Program to demonstrate Nested control structure-Switch statement, Continue statement, Break
statement, Goto statement

Objective: Write a program in C to implement Menu Driven Program for Students books record control
structure (Switch statement, Continue statement, Break statement, Goto statement)

Hardware /software Used: intel i7 processor/ turbo C

Theory:

Switch case statement evaluates a given expression and based on the


evaluated value(matching a certain condition), it executes the statements
associated with it. Basically, it is used to perform different actions based on
different conditions(cases).
 Switch case statements follow a selection-control mechanism and allow a
value to change control of execution.
 They are a substitute for long if statements that compare a variable to
several integral values.
 The switch statement is a multiway branch statement. It provides an easy
way to dispatch execution to different parts of code based on the value of
the expression.
Syntax of switch Statement in C
switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
.
.
.
case value_n: statement_n;
break;
default: default_statement;
}
Rules of the switch case statement
Following are some of the rules that we need to follow while using the switch
statement:
1. In a switch statement, the “case value” must be of “char” and “int” type.
2. There can be one or N number of cases.
3. The values in the case must be unique.
4. Each statement of the case can have a break statement. It is optional.
Jawahar Education Society’s
A.C. Patil College of Engineering, Kharghar
Department of Humanities and Basic Sciences
Academic Year 2025-26

5. The default Statement is also optional.


How switch Statement Work?
The working of the switch statement in C is as follows:
1. Step 1: The switch variable is evaluated.
2. Step 2: The evaluated value is matched against all the present cases.
3. Step 3A: If the matching case value is found, the associated code is
executed.
4. Step 3B: If the matching code is not found, then the default case is
executed if present.
5. Step 4A: If the break keyword is present in the case, then program
control breaks out of the switch statement.
6. Step 4B: If the break keyword is not present, then all the cases after the
matching case are executed.
7. Step 5: Statements after the switch statement are executed

Jump Statements in C
In C, jump statements are used to jump from one part of the code to another
altering the normal flow of the program. They are used to transfer the
program control to somewhere else in the program.
Types of Jump Statements in C
There are 4 types of jump statements in C:
 break
 continue
 goto
 return
Break Statement in C
The break statement exits or terminates the loop or switch statement based
on a certain condition, without executing the remaining iteration of the loop
or checking remaining cases in switch statement.
Jawahar Education Society’s
A.C. Patil College of Engineering, Kharghar
Department of Humanities and Basic Sciences
Academic Year 2025-26

Uses of break in C
The break statement is used in C for the following purposes:
1. To come out of the loop.
2. To come out of the switch case.
Note: If the break statement is used inside an inner loop in a nested loop, it
will break the inner loop without affecting the execution of the outer loop.
Continue Statement in C
The continue statement in C is used to skip the remaining code after the
continue statement within a loop and jump to the next iteration of the loop.
When the continue statement is encountered, the loop control immediately
jumps to the next iteration, by skipping the lines of code written after it within
the loop body.
Jawahar Education Society’s
A.C. Patil College of Engineering, Kharghar
Department of Humanities and Basic Sciences
Academic Year 2025-26

Goto Statement in C
The goto statement is used to jump to a specific point from anywhere in a
function. It is used to transfer the program control to a labeled statement
within the same function.
Jawahar Education Society’s
A.C. Patil College of Engineering, Kharghar
Department of Humanities and Basic Sciences
Academic Year 2025-26

Return Statement in C
The return statement in C is used to terminate the execution of a function
and return a value to the caller. It is commonly used to provide a result back
to the calling code.

Code:

#include <stdio.h>

#include <string.h>

int main() {

int choice, i, n = 0, id[100];

char title[100][50], author[100][50];

int searchID;

// Label for goto

menu:

printf("\n=== Student Book Record System ===\n");

printf("1. Add Book Record\n");

printf("2. Display All Books\n");

printf("3. Search Book by ID\n");

printf("4. Exit\n");

printf("Enter your choice: ");

scanf("%d", &choice);

switch(choice) {
Jawahar Education Society’s
A.C. Patil College of Engineering, Kharghar
Department of Humanities and Basic Sciences
Academic Year 2025-26

case 1:

printf("\nHow many books do you want to add? ");

scanf("%d", &i);

for(int j = 0; j < i; j++) {

printf("\nEnter Book ID: ");

scanf("%d", &id[n]);

printf("Enter Book Title: ");

scanf(" %[^\n]", title[n]);

printf("Enter Author Name: ");

scanf(" %[^\n]", author[n]);

n++;

printf("\nBooks added successfully!\n");

break;

case 2:

if(n == 0) {

printf("\nNo book records found!\n");

break;

printf("\n--- All Book Records ---\n");

for(int j = 0; j < n; j++) {

printf("Book ID: %d\n", id[j]);

printf("Title : %s\n", title[j]);

printf("Author : %s\n\n", author[j]);

break;
Jawahar Education Society’s
A.C. Patil College of Engineering, Kharghar
Department of Humanities and Basic Sciences
Academic Year 2025-26

case 3:

if(n == 0) {

printf("\nNo records to search!\n");

break;

printf("\nEnter Book ID to Search: ");

scanf("%d", &searchID);

for(int j = 0; j < n; j++) {

if(id[j] == searchID) {

printf("\nBook Found!\n");

printf("Book ID: %d\n", id[j]);

printf("Title : %s\n", title[j]);

printf("Author : %s\n", author[j]);

goto found; // jump to label if found

} else {

continue; // skip rest of loop if not found

printf("\nBook not found!\n");

found:

break;

case 4:

printf("\nExiting program... Goodbye!\n");

break;
Jawahar Education Society’s
A.C. Patil College of Engineering, Kharghar
Department of Humanities and Basic Sciences
Academic Year 2025-26

default:

printf("\nInvalid choice! Please try again.\n");

break;

if(choice != 4)

goto menu; // return to menu until user exits

return 0;

You might also like