Practical No 4
Practical No 4
Objective :
In C programming, decision-making statements allow the program to take different paths of execution
based on the conditions provided. The most common decision-making constructs are if, else, else if,
switch, and conditional (?:) operators. These statements are used to control the flow of the program
depending on the evaluation of expressions or conditions.
In C programming, decision-making statements allow your program to make choices based on certain
conditions. These statements help you control the flow of execution in your program. The primary
decision-making statements in C are if, if-else, and switch. Let's break them down one by one.
1. if Statement
The if statement evaluates a condition, and if the condition is true, the code inside the if block is
executed.
Syntax:
if (condition) {
Example:
if (number > 5) {
In this example, since number is indeed greater than 5, the message will be printed.
2. if-else Statement
The if-else statement provides an alternative set of instructions. If the condition in the if statement is
false, the code in the else block is executed.
Syntax:
if (condition) {
else {
// Code to execute if condition is false
Example:
int number = 3;
if (number > 5) {
} else {
Here, since number is not greater than 5, the second message will be printed.
Sometimes, you need to check multiple conditions. The if-else if-else statement allows you to check
several conditions in sequence.
Syntax:
if (condition1) {
} else if (condition2) {
} else {
Example:
int number = 7;
} else if (number == 7) {
} else {
In this case, since number is 7, the program will print "The number is 7."
4. switch Statement
The switch statement is used when you need to select one of many code blocks to be executed. It works
with discrete values like integers or characters.
Syntax:
switch (expression) {
case value1:
break;
case value2:
break;
// More cases...
default:
Example:
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
default:
printf("Invalid day\n");
//simple Code//
Write a C program that simulates a simple grading system based on student marks. The program should
take the marks of a student as input and print the corresponding grade based on the following
conditions:
Steps:
2. Decision-Making Process: Use the if-else if or switch statement to check the marks and assign
the corresponding grade.
Learning Outcomes:
Conclusion
if Statement: Executes code if a condition is true.
if-else Statement: Executes one block of code if the condition is true, and another block if it is
false.
switch Statement: Selects among multiple options based on the value of an expression.
Understanding and using these decision-making statements will help you write more complex and
responsive programs. Practice using them in different scenarios to get comfortable with controlling the
flow of your programs.