Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
16 views

Use of IF-ELSE-IF in Programming Language

The document explains the use of 'if' and 'else if' statements in the C programming language, detailing their syntax and functionality for evaluating multiple conditions. It provides examples to illustrate how these conditional statements work, including a flowchart for better understanding. Key points highlight the importance of using curly braces and the boolean nature of conditions.

Uploaded by

mzainsafdarjutt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views

Use of IF-ELSE-IF in Programming Language

The document explains the use of 'if' and 'else if' statements in the C programming language, detailing their syntax and functionality for evaluating multiple conditions. It provides examples to illustrate how these conditional statements work, including a flowchart for better understanding. Key points highlight the importance of using curly braces and the boolean nature of conditions.

Uploaded by

mzainsafdarjutt
Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Understandi

ng If and
Else If
Statements
in C
Language
Introduction:
An "else if" statement is a conditional statement in
programming that allows you to evaluate multiple
A.

conditions in sequence. It's used when there are more


than two possible outcomes for a decision.
1. if Statement:

“The if statement is used to evaluate a condition,


If the condition is true, the block of code inside
the if statement is executed. If the condition is
false, the block is skipped.”
Syntax:
if(condition) { // if body // Statements to
execute if condition is true }
Example:

#include <stdio.h>

int main() {

int number = 10;

if (number > 0) {

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


}
return 0;}

Explanation: In this example, the condition number > 0 is true, so the program prints“ The number is
positive." If number were less than or equal to 0, the code inside the if block would not execute.
2. Else-If:
“The else if statement allows you to
check multiple conditions in sequence. It
is used after an if statement and before
an optional else statement.”

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 conditions are
true}
Example: Explanation:

#include <stdio.h>  If number > 0, the program prints "The number is


int main() { positive."
int number = 0;  If number < 0, the program prints "The number is
if (number > 0) { negative."
printf("The number is positive.\n");  If neither condition is true, the else block executes,
} else if (number < 0) { printing "The number is zero."
printf("The number is negative.\n");
} else {
printf("The number is zero.\n");
}
return 0;}
Key Points to Remember:

• The if statement executes a block of code if its condition evaluates to true.

• The else if statement allows checking multiple conditions in sequence.

• The else statement executes a block of code if none of the previous conditions are true.

• Conditions inside if and else if statements must evaluate to a boolean (true or false).

• Use curly braces {} to define blocks of code, even if the block contains a single statement, to

avoid logical errors.


Flowchart for if - else if Structure:

1. Start

2. Evaluate the first condition (if).

• If true, execute the corresponding block and skip remaining checks.

3. Evaluate the next condition (else if).

• If true, execute the corresponding block and skip remaining checks.

4. Continue checking conditions in sequence.

5. If no conditions are true, execute the else block (if present).

6. End.
Real-World Example: Explanation:
#include <stdio.h>
int main() {  Scores greater than or equal to 85 are categorized as
int score;
"Excellent."
printf("Enter the student's score: ");
scanf("%d", &score);  Scores between 70 and 84 are "Good."

if (score >= 85) {  Scores between 50 and 69 are "Average."


printf("Excellent\n");
 Scores below 50 are "Poor."
} else if (score >= 70) {
printf("Good\n");
} else if (score >= 50) { printf("Average\
n");
} else {
printf("Poor\n");
}
return 0;}

You might also like