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

UNIT -1 EL

Uploaded by

kasithangamcse92
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)
5 views

UNIT -1 EL

Uploaded by

kasithangamcse92
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
You are on page 1/ 6

UNIT – 1

Experiential Learning

In C, decision-making statements such as `if`, `if-else`, `nested if-else`, and `switch-case` are
used to control the flow of the program based on different conditions. Below are examples of
decision-making statements with practical applications in C.

### **1. `if` Statement: Checking Student Grade**

```c
#include <stdio.h>

int main() {
int grade = 85;

if (grade >= 90) {


printf("Grade: A\n");
}

return 0;
}
```

**Application**: This checks if a student's grade is 90 or above and assigns an "A".

### **2. `if-else` Statement: Checking Attendance**

```c
#include <stdio.h>

int main() {
int attendance_percentage = 75;

if (attendance_percentage >= 75) {


printf("Eligible to sit for exams\n");
} else {
printf("Not eligible to sit for exams\n");
}

return 0;
}
```

**Application**: This determines whether a student meets the minimum attendance


requirement to be eligible for exams.

### **3. `nested if` Statement: Verifying Identity and Access**


```c
#include <stdio.h>
#include <string.h>

int main() {
char username[20] = "student123";
char password[20] = "password456";

if (strcmp(username, "student123") == 0) {
if (strcmp(password, "password456") == 0) {
printf("Access granted\n");
} else {
printf("Invalid password\n");
}
} else {
printf("Invalid username\n");
}

return 0;
}
```

**Application**: This checks both username and password for access control.

### **4. `if-else if-else` Statement: Categorizing GPA**

```c
#include <stdio.h>

int main() {
float gpa = 3.4;

if (gpa >= 3.7) {


printf("Honor Roll\n");
} else if (gpa >= 3.0) {
printf("Good Standing\n");
} else if (gpa >= 2.0) {
printf("Probation\n");
} else {
printf("Dismissal Warning\n");
}

return 0;
}
```

**Application**: This categorizes students based on their GPA into different academic
standings.

### **5. `switch-case` Statement: Course Registration System**


```c
#include <stdio.h>

int main() {
int course_code = 101;

switch(course_code) {
case 101:
printf("Registered for: Introduction to Computer Science\n");
break;
case 201:
printf("Registered for: Calculus II\n");
break;
case 301:
printf("Registered for: Shakespearean Literature\n");
break;
case 202:
printf("Registered for: Genetics\n");
break;
default:
printf("Course not found\n");
break;
}

return 0;
}
```

**Application**: This uses a `switch-case` statement to register a student for a course based
on the course code.

### **6. Conditional (Ternary) Operator: Checking Exam Eligibility**

```c
#include <stdio.h>

int main() {
int attendance = 80;
char *eligibility = (attendance >= 75) ? "Eligible" : "Not Eligible";
printf("%s\n", eligibility);

return 0;
}
```

**Application**: This compactly checks if a student is eligible to sit for exams based on
attendance.

### **7. `if-else` in Loops: Updating Student Grades**


```c
#include <stdio.h>

int main() {
int grades[] = {85, 92, 78, 64, 89};
int updated_grades[5];
int i;

for (i = 0; i < 5; i++) {


if (grades[i] < 70) {
updated_grades[i] = grades[i] + 5;
} else {
updated_grades[i] = grades[i];
}
}

for (i = 0; i < 5; i++) {


printf("Updated Grade %d: %d\n", i+1, updated_grades[i]);
}

return 0;
}
```

**Application**: This loop iterates through a list of grades and applies a 5-point boost to any
grade below 70.

### **8. `nested if-else` in Function: Student Performance Analysis**

```c
#include <stdio.h>

char* performance_analysis(int grade, int attendance) {


if (grade >= 90) {
if (attendance >= 75) {
return "Excellent";
} else {
return "Good, but poor attendance";
}
} else if (grade >= 70) {
if (attendance >= 75) {
return "Satisfactory";
} else {
return "Needs Improvement in attendance";
}
} else {
if (attendance >= 75) {
return "Poor";
} else {
return "At Risk";
}
}
}

int main() {
int grade = 85;
int attendance = 80;
printf("%s\n", performance_analysis(grade, attendance));

return 0;
}
```

**Application**: This function provides an analysis of student performance based on both


academic performance and attendance.

### **9. `if` Statement with Logical Operators: Course Eligibility Check**

```c
#include <stdio.h>

int main() {
int math_grade = 85;
int science_grade = 90;

if (math_grade >= 80 && science_grade >= 85) {


printf("Eligible for advanced courses\n");
} else {
printf("Not eligible for advanced courses\n");
}

return 0;
}
```

**Application**: This checks whether a student’s grades in both Math and Science meet the
criteria for advanced course enrollment.

### **10. `if-else` with Complex Conditions: Scholarship Eligibility**

```c
#include <stdio.h>

int main() {
float gpa = 3.8;
int extra_curricular_activities = 1; // 1 means true, 0 means false
int volunteer_hours = 50;

if (gpa >= 3.5 && extra_curricular_activities && volunteer_hours >= 40) {


printf("Eligible for scholarship\n");
} else {
printf("Not eligible for scholarship\n");
}

return 0;
}
```

**Application**: This determines eligibility for a scholarship based on GPA, extracurricular


activities, and volunteer hours.

### **Conclusion**
These examples demonstrate how decision-making statements can be applied in various
practical applications using C. Whether managing grades, checking eligibility, or controlling
access, these statements are fundamental in writing programs that can make decisions and
react accordingly.

You might also like