0% found this document useful (0 votes)
27 views15 pages

14 Group

The document describes a program that calculates an employee's salary based on years of service and basic pay using nested if-else statements. It prompts the user to enter the number of service years and basic pay. If service years are less than 5, salary is increased by 6% if basic pay is less than 12,000 or 8% if basic pay is more than or equal to 12,000. If service years are 5 or more, salary is increased by 8% or 10% depending on whether basic pay is less than or more than 12,000 respectively. The updated salary is then displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
27 views15 pages

14 Group

The document describes a program that calculates an employee's salary based on years of service and basic pay using nested if-else statements. It prompts the user to enter the number of service years and basic pay. If service years are less than 5, salary is increased by 6% if basic pay is less than 12,000 or 8% if basic pay is more than or equal to 12,000. If service years are 5 or more, salary is increased by 8% or 10% depending on whether basic pay is less than or more than 12,000 respectively. The updated salary is then displayed.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 15

MALLA REDDY ENGINEERING

COLLEGE FOR WOMEN


(UGC AUTONOMOUS)

FACULTY:-VS NARAYANA.M
SUBJECT:-PROGRAMMING FOR PROBLEM SOLVING
BRANCH:-CSIT
BY:
• K Sai Sahasra:-23RH1A3340
• L Tejasri:-23RH1A3341
• M.Sreeja:-23RH1A3342
PRESENTATION ON
NESTED IF….
CONTENTS

 WHAT IS IF STATEMENT
 NESTED IF
 ALGORITHM AND FLOWCHART
 PROBLEM STATEMENT
 SOURCE PROGRAM
 OUTPUT
WHAT IS IF STATEMENT:

 "if statement" is a control structure that allows you to make decisions in your code based on a condition. It is used to control
the flow of a program by executing certain code only if a specified condition is true.
 You can also extend the if statement with an optional else block to specify what should happen if the condition is false.
 Eg: if (condition) { =>Condition is an expression that evaluates to either true
// Code to be executed if the condition is true (non-zero) or false (zero).
=>If the condition is true, the code block enclosed in curly
} else { braces {} following the if statement will be executed.
// Code to be executed if the condition is false =>If the condition is false, the code block of if statement will
be skipped, and the program will execute code block of else
}
statements.

 “if statements” can be extended to “Nested if ”


NESTED IF:

• Nested if statements consist of one if statement within another.


• Nested if statements create a hierarchical structure where the inner if is contained within the block of the
outer if.
• Conditions in nested if statements are evaluated sequentially: the inner if is checked only if the outer if
condition is true.
• Nested if statements are used to implement complex decision-making based on multiple conditions.
• Care should be taken when using nested if statements, as they can make code more intricate. Alternatives
like logical operators and else if clauses can be considered for simpler, more readable code.
ALGORITHM AND FLOWCHAT
EXPLANATION
 Outer If Statement: This is the first if statement that is encountered. It checks a specific condition, let's call it "Condition 1"
 If "Condition 1" is true, the program proceeds to the inner if statement (nested if).
 Nested If Statement (Inner If): Inside the outer if statement, there is another if statement. This inner if statement checks a different condition,
"Condition 2."
 If "Condition 2" is true, the program executes a specific block of code associated with "Condition 2."
 If "Condition 2" is false, the program executes an else block (if present) or moves on to the next part of the program.
 End of Nested If: After evaluating "Condition 2" and its associated code, the program returns to the outer if statement.
 " If "Condition A" is false, it means that the condition specified in the outer if statement is not satisfied.
 Else Block (Optional): If there is an else block associated with the outer if statement, the program will execute the code within this block. The else
block contains instructions that are executed when "Condition A" is false. It provides an alternative course of action.
 Nested If Statement (Inner If): If "Condition 3" is true, the program executes a specific block of code associated with "Condition 3."
 If "Condition 3" is false, the program executes an else block (if present) or moves on to the next part of the program.
 End of Outer If: After executing the else block (if present) or moving on from the outer if statement, the program continues its execution .
PROBLEM STATEMENT:

 An organization incrementing salary based on number of service years and the basic payIf no of service years < 5,
basic pay is <12000 salary is increased by 6%, if basic pay is >12000 salary is increased by 8%.If no of service
years >5, basic pay is <12000 salary is increased by 8%, if basic pay is >12000 salary is increased by 10% .
PROGRAM:

#include <stdio.h>
int main() {
int serviceYears;
float basicPay, salary;

// Input the number of service years and basic pay


printf("Enter the number of service years: ");
scanf("%d", &serviceYears);

printf("Enter the basic pay: ");


scanf("%f", &basicPay);
PROGRAM:

// Check the conditions and calculate the salary increment


if (serviceYears < 5) {
if (basicPay < 12000) {
salary = basicPay * 1.06;
} else {
salary = basicPay * 1.08;
}
} else {
if (basicPay < 12000) {
salary = basicPay * 1.08;
} else {
salary = basicPay * 1.10;
}
}
// Display the calculated salary
printf("The updated salary is: %.2f\n", salary);
return 0;
}
CODE EXPLAINATION:

Step 1:
include the necessary header file:
#include <stdio.h>
This line includes the standard input/output library, which provides functions like printf and scanf for reading and displaying data .
Step 2:
Declare variables to store service years, basic pay, and salary:
int serviceYears;
float basicPay, salary;
Here, we declare three variables to store the number of service years, basic pay, and the calculated salary.
Step 3:
Prompt the user to enter the number of service years and basic pay:
printf("Enter the number of service years: ");
scanf("%d", &serviceYears);

printf("Enter the basic pay: ");


scanf("%f", &basicPay);
These lines display prompts to the user and use scanf to read their input into the serviceYears and basicPay variables.
CODE EXPLANATION:

Step 4:
Calculate the salary increment based on the conditions you specified:
if (serviceYears < 5) {
if (basicPay < 12000) {
salary = basicPay * 1.06;
} else {
salary = basicPay * 1.08;
}
} else {
if (basicPay < 12000) {
salary = basicPay * 1.08;
CODE EXPLANATION:

} else {
salary = basicPay * 1.10;
}
}
=>The code uses nested if-else statements to determine the salary increment. It first checks the number of service
years.
=>If it's less than 5, it checks the basic pay and increases the salary by 6% if the basic pay is less than 12000, or by
8% if the basic pay is greater than or equal to 12000.
=>If the number of service years is 5 or more, it applies different percentages (8% and 10%) based on the basic pay .
CODE EXPLANATION:

Step 5:
Display the calculated salary:
printf("The updated salary is: %.2f\n", salary);
Finally, the code uses printf to display the updated salary with two decimal places.
Output:
=> Enter the number of service years: 3
Enter the basic pay: 11000
The updated salary is: 11660.00
=> Enter the number of service years: 7
Enter the basic pay: 14000
The updated salary is: 15400.00
THANK
YOU

You might also like