PDFVersion
No ads? No problem! Download the PDF book of this tutorial for just $24.99. Your support will help us reach more readers.
Thank You!
Decision Making In Programming:
In the realm of everyday life, decision-making is a ubiquitous aspect of our existence. We routinely navigate various tasks by making decisions, such as determining whether to pursue an interest in programming or choosing where to buy groceries. This decision-making process is not exclusive to human experiences; computer programs also exhibit intelligence through their ability to make selections and decisions.
In this comprehensive tutorial, we delve into the intricacies of decision-making in programming, focusing on the utilization of if, else, and nested if-else statements.
The if statement is a fundamental construct in programming that facilitates conditional execution of code. This statement consists of a Boolean expression, followed by one or more statements. By employing if statements, we can ascertain whether a given condition is true or false, allowing the program to execute specific blocks of code accordingly.
if (condition) { // Body of code to execute if the condition is true statement... statement... statement... }
It is noteworthy that if the statement consists of a single line, curly braces {}
are optional. However, when dealing with multiple statements, enclosing them within curly braces is imperative, defining the block of code to be executed.
Let's illustrate this with an example. Consider a program that determines whether a student passes or fails based on their percentage. If the percentage is greater than or equal to 60, the student passes; otherwise, they fail.
In this program, the user is prompted to input their percentage. After entering the percentage and pressing enter, the program evaluates the input to decide whether the user has passed or failed. If the user passes, both the code inside and outside the block will be executed. However, if the user fails, only the code outside the block will be executed.
#include <iostream> using namespace std; main() { int percentage; //variable to store student percentage: cout << " Enter the percentage of Student: " << endl; cin >> percentage; //if statement. if(percentage >= 60) { //This statement will be executed if the percentage is 60 or higher. cout << " Congratulations, you have passed. "<< endl; } return 0; }
While the if statement allows for the execution of code when a condition is true, the if and else structure extends this capability by providing an alternative block of code to execute when the condition is false.
if (condition) { // Block of code to execute if the condition is true } else { // Block of code to execute if the condition is false }
In this construct, only one block of statements will be executed, depending on whether the condition is true or false.
Building on our previous example, let's enhance the program to include an else statement. Now, if the percentage is less than 60, a specific set of statements within the else block will be executed
using namespace std; main() { int percentage; //variable to store student percentage: cout << " Enter the percentage of Student: "<< endl; cin >> percentage; if(percentage >= 60) { //Block of code to execute if the condition is true cout << " Congratulations, you have passed. "<< endl; } else { // Block of code to execute if the condition is false cout << " You have failed. "<< endl; } return 0; }
The nested if-else statement expands our decision-making capabilities by allowing us to test multiple conditions in a hierarchical manner. This is particularly useful when dealing with scenarios where more than one condition needs to be assessed.
if (1st condition) { // Block of code to execute if the 1st condition is true } else if (2nd condition) { // Block of code to execute if the 2nd condition is true and the 1st is false } else { // Block of code to execute if both conditions are false }
In a nested if-else statement, only one block of statements will be executed based on the first true condition encountered. If none of the conditions are met, the final else block is executed.
To illustrate this, consider a program prompting the user to enter their gender. Depending on the input ("M" or "F"), specific messages will be displayed. If an invalid character is entered, an error message will prompt the user to use capital letters.
#include <iostream> using namespace std; main() { char gender; cout << " Specify your gender: Input 'M' for male or 'F' for female using capital letters." << endl; cin >> gender; if(gender == 'M') { cout << " You are identified as male. "<< endl; } else if(gender =='F') { cout << " You are identified as Female. "<< endl; } else { cout << " You have provided an incorrect response. "<< endl; } return 0; }
The shorthand if-else, also called the conditional or ternary operator due to its three operands, serves to condense multiple lines of code into a single line. Commonly, it is employed to substitute straightforward if-else statements.
Instead of writing:
int time = 20; if (time < 18) { cout << "Good day."; } else { cout << "Good evening."; }
You can simply write:
int time = 20; string result = (time < 18) ? "Good day." : "Good evening."; cout << result;
Sardar Omar
I did my hardest to present you with all of the information you need on this subject in a simple and understandable manner. However, if you have any difficulties understanding this concept or have any questions, please do not hesitate to ask. I'll try my best to meet your requirements.