0% found this document useful (0 votes)
23 views4 pages

Practical No 4

Uploaded by

aratipatil3478
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)
23 views4 pages

Practical No 4

Uploaded by

aratipatil3478
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/ 4

Topic :Decision-Making Statements in C Programming

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.

Here's a practical objective for learning decision-making statements in C programming:

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) {

// Code to execute if condition is true

Example:

int number = 10;

if (number > 5) {

printf("The number is greater than 5.\n");

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) {

// Code to execute if condition is true

else {
// Code to execute if condition is false

Example:

int number = 3;

if (number > 5) {

printf("The number is greater than 5.\n");

} else {

printf("The number is 5 or less.\n");

Here, since number is not greater than 5, the second message will be printed.

3. if-else if-else Statement

Sometimes, you need to check multiple conditions. The if-else if-else statement allows you to check
several conditions in sequence.

Syntax:

if (condition1) {

// Code to execute if condition1 is true

} else if (condition2) {

// Code to execute if condition2 is true

} else {

// Code to execute if none of the above conditions are true

Example:

int number = 7;

if (number > 10) {

printf("The number is greater than 10.\n");

} else if (number == 7) {

printf("The number is 7.\n");

} else {

printf("The number is neither greater than 10 nor 7.\n");


}

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:

// Code to execute if expression == value1

break;

case value2:

// Code to execute if expression == value2

break;

// More cases...

default:

// Code to execute if none of the above cases match

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");

In this example, since day is 3, the output will be "Wednesday."

//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:

 Grade A: Marks >= 85

 Grade B: Marks between 70 and 84

 Grade C: Marks between 50 and 69

 Grade D: Marks between 35 and 49

 Grade F: Marks < 35

Steps:

1. Input: Take the marks as input from the user.

2. Decision-Making Process: Use the if-else if or switch statement to check the marks and assign
the corresponding grade.

3. Output: Print the grade.

Learning Outcomes:

 Understand how to use decision-making statements in C.

 Practice evaluating conditions and applying logical flow control.

 Gain experience writing a simple program using if-else constructs.

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.

 if-else if-else Statement: Allows checking multiple conditions in sequence.

 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.

You might also like