Ad Space
Control structures alter the normal sequential flow of execution in a program. They are categorized into decision-making and looping statements.
1. Decision-Making (Conditional) Statements:
These statements execute a block of code only if a certain condition is true.
if Statement: Executes code if the condition is true.
if (age > 18) {
printf("You are an adult.");
}
if-else Statement: Executes one block of code if the condition is true, and another block if it is false.
if (num % 2 == 0) {
printf("Even");
} else {
printf("Odd");
}
if-else-if Ladder: Used to test multiple conditions.
if (marks >= 90) {
printf("Grade A");
} else if (marks >= 80) {
printf("Grade B");
} else {
printf("Grade C");
}
switch Statement: Used to select one of many code blocks to be executed based on a single integer or character value.
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
default:
printf("Invalid day");
}
2. Looping (Iterative) Statements:
These statements execute a block of code repeatedly as long as a condition is true.
while Loop (Entry-Controlled): The condition is checked *before* executing the loop body. The loop runs 0 or more times.
int i = 1;
while (i <= 5) {
printf("%d ", i);
i++;
}
// Output: 1 2 3 4 5
do-while Loop (Exit-Controlled): The condition is checked *after* executing the loop body. The loop runs at least 1 time.
int i = 1;
do {
printf("%d ", i);
i++;
} while (i <= 5);
// Output: 1 2 3 4 5
for Loop (Entry-Controlled): A compact loop structure with initialization, condition, and update in one line.
int i;
for (i = 1; i <= 5; i++) {
printf("%d ", i);
}
// Output: 1 2 3 4 5
This program defines a Student structure, calculates percentage (as a proxy for GPA, since a true GPA/CGPA calculation system is complex and specific), and demonstrates its usage.
#include <stdio.h>
#include <string.h>
// i) Declare the structure
struct Student {
char name[100];
int age;
long roll_number;
int marks[5]; // Marks for 5 subjects
float percentage;
float gpa; // Using percentage as a proxy
};
// Function to calculate GPA/Percentage
void calculateGPA(struct Student *s) {
int total_marks = 0;
int num_subjects = 5;
for (int i = 0; i < num_subjects; i++) {
total_marks += s->marks[i];
}
s->percentage = (float)total_marks / num_subjects;
// ii) Calculate GPA (on a 10-point scale, as percentage/10)
// This is a simplification. Real GPA/CGPA involves credits.
s->gpa = s->percentage / 10.0;
}
int main() {
struct Student s1;
// --- Get Student Details ---
strcpy(s1.name, "Arun Kumar");
s1.age = 19;
s1.roll_number = 12345;
printf("Enter marks for 5 subjects for %s:\n", s1.name);
for (int i = 0; i < 5; i++) {
printf("Subject %d: ", i + 1);
scanf("%d", &s1.marks[i]);
}
// --- Calculate GPA/Percentage ---
calculateGPA(&s1);
// --- Display Student Details ---
printf("\n--- Student Information ---\n");
printf("Name: %s\n", s1.name);
printf("Age: %d\n", s1.age);
printf("Roll Number: %ld\n", s1.roll_number);
printf("Percentage: %.2f%%\n", s1.percentage);
printf("GPA (out of 10): %.2f\n", s1.gpa);
// CGPA is an average of GPAs over multiple semesters.
// We can only show GPA for this single set of marks.
printf("CGPA: (Requires marks from previous semesters)\n");
return 0;
}
This program defines a student structure and uses an array of structures to store records for 3 students (using 3 instead of 30 for easier testing).
#include <stdio.h>
#define NUM_STUDENTS 3 // Using 3 for testing. Change to 30 for full program.
#define NUM_SUBJECTS 5
struct student {
char name[100];
long reg_no;
int marks[NUM_SUBJECTS];
float percentage;
};
int main() {
struct student s[NUM_STUDENTS];
int i, j;
// --- Read Details for 30 Students ---
for (i = 0; i < NUM_STUDENTS; i++) {
printf("\n--- Enter Details for Student %d ---\n", i + 1);
printf("Enter Name: ");
scanf(" %[^\n]", s[i].name); // Read string with spaces
printf("Enter Register No.: ");
scanf("%ld", &s[i].reg_no);
printf("Enter Marks for 5 Subjects:\n");
int total_marks = 0;
for (j = 0; j < NUM_SUBJECTS; j++) {
printf("Subject %d: ", j + 1);
scanf("%d", &s[i].marks[j]);
total_marks += s[i].marks[j];
}
// --- Calculate Percentage ---
s[i].percentage = (float)total_marks / NUM_SUBJECTS;
}
// --- Display All Student Details ---
printf("\n\n*** Student Details ***\n");
printf("--------------------------------------------\n");
printf("Reg. No.\tName\t\tPercentage\n");
printf("--------------------------------------------\n");
for (i = 0; i < NUM_STUDENTS; i++) {
printf("%ld\t\t%s\t\t%.2f%%\n", s[i].reg_no, s[i].name, s[i].percentage);
}
printf("--------------------------------------------\n");
return 0;
}
Ad Space