Functions and Conditional Statements
Functions and Conditional Statements
A Conditional Statement in a Function
Introduction
Remember that a function is a section of code where you want to isolate one or more oprations. Some of those operations may require some conditions. You have amny options. At a minimum, in the body of a function, you can use one or more conditional statements.
Practical Learning: Introducing Conditional Statements
using static System.Console; double excess = 0.00; double taxAmount = 0.00; double grossSalary = 0.00; void CalculateTaxAmount() { /* https://tax.wv.gov/Documents/TaxForms/2020/it140.booklet.pdf * 1 - Single * 2 - Head of household * 3 - Married filing joint * 5 - Widow[er] with dependent child * Less than $10,000............3% of the taxable income * At least – But less than – * $ 10,000 $25,000 $300.00 plus 4% of excess over $10,000 * $ 25,000 $40,000 $900.00 plus 4.5% of excess over $25,000 * $ 40,000 $60,000 $1,575.00 plus 6% of excess over $40,000 * $ 60,000 $2,775.00 plus 6.5% of excess over $60,000 * */ if (grossSalary is < 10_000) { excess = grossSalary * 3.00 / 100; taxAmount = grossSalary * 3.00 / 100; } else if ((grossSalary is >= 10_000) && (grossSalary is < 25_000)) { excess = ((grossSalary - 10_000) * 4.00 / 100); taxAmount = 300.00 + ((grossSalary - 10_000) * 4.00 / 100); } else if ((grossSalary is >= 25_000) && (grossSalary is < 40_000)) { excess = ((grossSalary - 10_000) * 4.00 / 100); taxAmount = 900 + ((grossSalary - 25_000) * 4.50 / 100); } else if ((grossSalary is >= 40_000) && (grossSalary is < 60_000)) { excess = ((grossSalary - 40_000) * 6.00 / 100); taxAmount = 1_575 + ((grossSalary - 40_000) * 6.00 / 100); } else // if (grossSalary is >= 60_000) { excess = ((grossSalary - 60_000) * 6.50 / 100); taxAmount = 2_775 + ((grossSalary - 60_000) * 6.50 / 100); } } double GetSalary() { WriteLine("Enter the information to prepare the taxes"); Write("Gross Salary: "); double sal = double.Parse(ReadLine()); return sal; } bool Summarize() { WriteLine("============================================"); WriteLine(" - Amazing DeltaX - State Income Tax -"); WriteLine("--------------------------------------------"); WriteLine(" -=- West Virginia -=-"); WriteLine("============================================"); grossSalary = GetSalary(); CalculateTaxAmount(); double netPay = grossSalary - taxAmount; WriteLine("============================================"); WriteLine(" - Amazing DeltaX - State Income Tax -"); WriteLine("--------------------------------------------"); WriteLine(" -=- West Virginia -=-"); WriteLine("============================================"); WriteLine($"Gross Salary: {grossSalary:f}"); WriteLine("--------------------------------------------"); WriteLine($"Gross Salary: {excess:f}"); WriteLine($"Tax Amount: {taxAmount:f}"); WriteLine($"Net Pay: {netPay:f}"); WriteLine("============================================"); return true; } bool complete = Summarize();
============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- -=- West Virginia -=- ============================================ Enter the information to prepare the taxes Gross Salary: 3748.85 ============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- -=- West Virginia -=- ============================================ Gross Salary: 3748.85 -------------------------------------------- Tax Amount: 112.47 Excess: 112.47 Net Pay: 3636.38 ============================================ Press any key to close this window . . .
Conditionally Returning a Value
We are already familiar with the ability for a function to return a value. In some cases, the value you want to return is not a simple constant: It may depend on some condition. To make this happen, you have various options. As one option, in a function, you can create a conditional statement to validate a value. You can then store the result in a variable and, at the end of the function, return that variable.
Practical Learning: Calling a Function that Returns a Value
using static System.Console; double grossSalary = 0.00; double CalculateTaxAmount() { double taxAmount = 0.00; if (grossSalary is < 10_000) { taxAmount = grossSalary * 3.00 / 100; } else if ((grossSalary is >= 10_000) && (grossSalary is < 25_000)) { taxAmount = 300.00 + ((grossSalary - 10_000) * 4.00 / 100); } else if ((grossSalary is >= 25_000) && (grossSalary is < 40_000)) { taxAmount = 900 + ((grossSalary - 25_000) * 4.50 / 100); } else if ((grossSalary is >= 40_000) && (grossSalary is < 60_000)) { taxAmount = 1_575 + ((grossSalary - 40_000) * 6.00 / 100); } else // if (grossSalary is >= 60_000) { taxAmount = 2_775 + ((grossSalary - 60_000) * 6.50 / 100); } return taxAmount; } double CalculateTaxExcess() { double excess = 0.00; if (grossSalary is < 10_000) { excess = grossSalary * 3.00 / 100; } else if ((grossSalary is >= 10_000) && (grossSalary is < 25_000)) { excess = ((grossSalary - 10_000) * 4.00 / 100); } else if ((grossSalary is >= 25_000) && (grossSalary is < 40_000)) { excess = ((grossSalary - 10_000) * 4.00 / 100); } else if ((grossSalary is >= 40_000) && (grossSalary is < 60_000)) { excess = ((grossSalary - 40_000) * 6.00 / 100); } else // if (grossSalary is >= 60_000) { excess = ((grossSalary - 60_000) * 6.50 / 100); } return excess; } double GetSalary() { WriteLine("Enter the information to prepare the taxes"); Write("Gross Salary: "); double sal = double.Parse(ReadLine()); return sal; } bool Summarize() { Console.WriteLine("============================================"); Console.WriteLine(" - Amazing DeltaX - State Income Tax -"); Console.WriteLine("--------------------------------------------"); Console.WriteLine(" -=- West Virginia -=-"); Console.WriteLine("============================================"); grossSalary = GetSalary(); // Getting a value conditionally returned by a function double tax = CalculateTaxAmount(); // Getting another value conditionally returned by a function double exc = CalculateTaxExcess(); double netPay = grossSalary - tax; Console.WriteLine("============================================"); Console.WriteLine(" - Amazing DeltaX - State Income Tax -"); Console.WriteLine("--------------------------------------------"); Console.WriteLine(" -=- West Virginia -=-"); Console.WriteLine("============================================"); Console.WriteLine($"Gross Salary: {grossSalary:f}"); Console.WriteLine("--------------------------------------------"); Console.WriteLine($"Gross Salary: {exc:f}"); Console.WriteLine($"Tax Amount: {tax:f}"); Console.WriteLine($"Net Pay: {netPay:f}"); Console.WriteLine("============================================"); return true; } _ = Summarize();
============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- -=- West Virginia -=- ============================================ Enter the information to prepare the taxes Gross Salary: 18755.50 ============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- -=- West Virginia -=- ============================================ Gross Salary: 18755.50 -------------------------------------------- Tax Amount: 650.22 Excess: 350.22 Net Pay: 18105.28 ============================================ Press any key to close this window . . .
Topics on Conditionally Returning from a Function
Exiting Early From a Function
One of the goals of a function is to perform an action if a certain condition is met. In fact, by including a condition in a function, you can decide whether the action of a function is worth pursuing or completing. In the body of a function where you are checking a condition, once you find out that a certain condition is not met, you can stop checking the condition and get out of the function. This is done with the return keyword. To apply it, in the body of a conditional statement in a function, once you decide that the condition reaches the wrong outcome, type return and a semicolon. Here is an example:
using static System.Console; WriteLine("Water Distribution Company"); WriteLine("=========================="); void RegisterCustomer() { WriteLine("New Customer Account"); WriteLine("--------------------------"); Write("Enter Account Name: "); string name = ReadLine(); WriteLine("--------------------------"); WriteLine("Type of Account"); WriteLine("1 - Residential"); WriteLine("2 - Business"); WriteLine("3 - Government"); Write("Enter Account Type: "); int type = int.Parse(ReadLine()); /* In this version of our application, we are processing * only accounts for persons/families or residences. */ if(type != 1) { /* If the employee entered a type other than * that of a family, don't do nothing. */ return; } WriteLine("=========================="); WriteLine("Customer Details"); WriteLine("--------------------------"); WriteLine("Customer Name: {0}", name); WriteLine("Account Type: RES - Residential"); } RegisterCustomer(); WriteLine("==========================");
Here is an example of running the program:
Water Distribution Company ======================================= New Customer Account --------------------------------------- Enter Account Name: Madeleine Hernandez --------------------------------------- Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 1 ======================================= Customer Details --------------------------------------- Customer Name: Madeleine Hernandez Account Type: RES - Residential ======================================= Press any key to close this window . . .
Here is another example of running the program:
Water Distribution Company ======================================= New Customer Account --------------------------------------- Enter Account Name: Madeleine Hernandez --------------------------------------- Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 3 ======================================= Press any key to close this window . . .
In the same way, in a function, you can make various comparisons and, any time you want to stop processing anything in the function, stop the processing by typing return;. Here are examples:
using static System.Console; WriteLine("Water Distribution Company"); WriteLine("========================================================================================="); void PrepareWaterBill() { WriteLine("New Customer Account"); WriteLine("-----------------------------------------------------------------------------------------"); Write("Enter Account Name: "); string name = ReadLine(); WriteLine("-----------------------------------------------------------------------------------------"); WriteLine("Type of Account"); WriteLine("1 - Residential"); WriteLine("2 - Business"); WriteLine("3 - Government"); Write("Enter Account Type: "); int type = int.Parse(ReadLine()); /* In this version of our application, we are processing * only accounts for persons/families or residences. */ if(type != 1) { WriteLine("========================================================================================="); WriteLine("The current version of this application " + "processes water bills only for family residences."); return; } WriteLine("========================================================================================="); WriteLine("Enter the counter reading information"); Write("Counter Reading Start: "); int counterStart = int.Parse(ReadLine()); Write("Counter Reading End: "); int counterEnd = int.Parse(ReadLine()); if(counterStart > counterEnd) { WriteLine("========================================================================================="); WriteLine("You probaly typed the counter reading values in reverse."); WriteLine("The value for the counter reading end must be "); WriteLine("higher than the counter reading start."); return; } int gallons = counterEnd - counterStart; double HCFTotal = gallons * 748.05; WriteLine("========================================================================================="); WriteLine("Customer Details"); WriteLine("-----------------------------------------------------------------------------------------"); WriteLine("Customer Name: {0}", name); WriteLine("Account Type: RES - Residential"); WriteLine("========================================================================================="); WriteLine("Counter Reading"); WriteLine("-----------------------------------------------------------------------------------------"); WriteLine("Counter Reading Start: {0}", counterStart); WriteLine("Counter Reading End: {0}", counterEnd); WriteLine("========================================================================================="); WriteLine("Consumption"); WriteLine("-----------------------------------------------------------------------------------------"); WriteLine("Gallons: {0}", gallons); WriteLine("HCF Total: {0}", HCFTotal); } PrepareWaterBill(); WriteLine("=========================================================================================");
Here is an example of running the program:
Water Distribution Company ========================================================================================= New Customer Account ----------------------------------------------------------------------------------------- Enter Account Name: Marianna Cardon ----------------------------------------------------------------------------------------- Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 2 ========================================================================================= The current version of this application processes water bills only for family residences. ========================================================================================= Press any key to close this window . . .
Here is another example of running the program:
Water Distribution Company ========================================================================================= New Customer Account ----------------------------------------------------------------------------------------- Enter Account Name: Marianna Cardon ----------------------------------------------------------------------------------------- Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 1 ========================================================================================= Enter the counter reading information Counter Reading Start: 6162 Counter Reading End: 6144 ========================================================================================= You probaly typed the counter reading values in reverse. The value for the counter reading end must be higher than the counter reading start. ========================================================================================= Press any key to close this window . . .
Here is another example of running the program:
Water Distribution Company ========================================================================================= New Customer Account ----------------------------------------------------------------------------------------- Enter Account Name: Marianna Cardon ----------------------------------------------------------------------------------------- Type of Account 1 - Residential 2 - Business 3 - Government Enter Account Type: 1 ========================================================================================= Enter the counter reading information Counter Reading Start: 6144 Counter Reading End: 6162 ========================================================================================= Customer Details ----------------------------------------------------------------------------------------- Customer Name: Marianna Cardon Account Type: RES - Residential ========================================================================================= Counter Reading ----------------------------------------------------------------------------------------- Counter Reading Start: 6144 Counter Reading End: 6162 ========================================================================================= Consumption ----------------------------------------------------------------------------------------- Gallons: 18 HCF Total: 13464.9 ========================================================================================= Press any key to close this window . . .
Returning in a Condition
We have already learned that you can create a conditional statement in a function, store the result in a variable, and then return that variable. We also learned that you can early stop the operations in a function once you don't see any reason to continue the operations inside the function. You can combine these two concepts. In a function, if you create a conditional statement that will produce a value for a function, instead of first storing the value in a variable, you can return the value directly where it is produced. As always, you have many options.
Somewhere inside a function, you can create an if condition so that if it produces a True result, you can return a value from there. Another option, which is the most common, consists of creating a series of if and if...else conditions; in which case each section would return its value.
Practical Learning: Conditionally Returning a Value
using static System.Console; double grossSalary = 0.00; double CalculateTaxAmount() { if (grossSalary is < 10_000) { return grossSalary * 3.00 / 100; } else if ((grossSalary is >= 10_000) && (grossSalary is < 25_000)) { return 300.00 + ((grossSalary - 10_000) * 4.00 / 100); } else if ((grossSalary is >= 25_000) && (grossSalary is < 40_000)) { return 900 + ((grossSalary - 25_000) * 4.50 / 100); } else if ((grossSalary is >= 40_000) && (grossSalary is < 60_000)) { return 1_575 + ((grossSalary - 40_000) * 6.00 / 100); } else // if (grossSalary is >= 60_000) { return 2_775 + ((grossSalary - 60_000) * 6.50 / 100); } } double CalculateTaxExcess() { if (grossSalary is < 10_000) { return grossSalary * 3.00 / 100; } else if ((grossSalary is >= 10_000) && (grossSalary is < 25_000)) { return ((grossSalary - 10_000) * 4.00 / 100); } else if ((grossSalary is >= 25_000) && (grossSalary is < 40_000)) { return ((grossSalary - 10_000) * 4.00 / 100); } else if ((grossSalary is >= 40_000) && (grossSalary is < 60_000)) { return ((grossSalary - 40_000) * 6.00 / 100); } else // if (grossSalary is >= 60_000) { return ((grossSalary - 60_000) * 6.50 / 100); } } double GetSalary() { WriteLine("Enter the information to prepare the taxes"); Write("Gross Salary: "); double sal = double.Parse(ReadLine()); return sal; } bool Summarize() { WriteLine("============================================"); WriteLine(" - Amazing DeltaX - State Income Tax -"); WriteLine("--------------------------------------------"); WriteLine(" -=- West Virginia -=-"); WriteLine("============================================"); grossSalary = GetSalary(); double tax = CalculateTaxAmount(); double exc = CalculateTaxExcess(); double netPay = grossSalary - tax; WriteLine("============================================"); WriteLine(" - Amazing DeltaX - State Income Tax -"); WriteLine("--------------------------------------------"); WriteLine(" -=- West Virginia -=-"); WriteLine("============================================"); WriteLine($"Gross Salary: {grossSalary:f}"); WriteLine("--------------------------------------------"); WriteLine($"Gross Salary: {exc:f}"); WriteLine($"Tax Amount: {tax:f}"); WriteLine($"Net Pay: {netPay:f}"); WriteLine("============================================"); return true; } _ = Summarize();
============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- -=- West Virginia -=- ============================================ Enter the information to prepare the taxes Gross Salary: 117635 ============================================ - Amazing DeltaX - State Income Tax - -------------------------------------------- -=- West Virginia -=- ============================================ Gross Salary: 117635.000 -------------------------------------------- Gross Salary: 3746.275 Tax Amount: 6521.275 Net Pay: 111113.725 ============================================
|
|||
Previous | Copyright © 2001-2025, FunctionX | Sunday 17 February 2025, 22:05 | Next |
|