![]() |
Decision-making in C++ involves the usage of conditional statements (also called decision control statements) to execute specific blocks of code primarily based on given situations and their results. So basically, in decision-making, we evaluate the conditions and make a decision about which part of the code should be executed or not. It allows selective code execution which is crucial for controlling the flow of a program and making it more dynamic. ![]()
Types of Decision-Making Statements in C++In C++, the following decision-making statements are available:
1. if in C++In C++, the if statement is the simplest decision-making statement. It allows the execution of a block of code if the given condition is true. The body of the ‘if’ statement is executed only if the given condition is true. Syntax of if in C++if (condition) { Here if the condition is true then the code inside the if block will be executed otherwise not. Flowchart of if in C++![]()
Example of if in C++The below example demonstrates the use of the if statement by finding if the age of a person is greater than 18 or not. if the condition is true then he/she is allowed to vote. C++
Output
allowed to vote 2. if-else in C++The if-else decision-making statement allows us to make a decision based on the evaluation of a given condition. If the given condition evaluates to true then the code inside the ‘if’ block is executed and in case the condition is false, the code inside the ‘else’ block is executed. Syntax of if-else in C++if (condition) { Flowchart of if-else in C++![]() Flow diagram of if else Example of if-else in CThe below example demonstrates the use of an if-else statement to find if the given number is positive or nonpositive. C++
Output
number is positive. 3. if-else if Ladder in C++The if-else-if statements allow us to include additional situations after the preliminary if condition. The ‘else if’ condition is checked only if the above condition is not true. , and the `else` is the statement that will be executed if none of the above conditions is true. If some condition is true, then no only the associated block is executed. Syntax if-else-if Ladderif (condition1) { We can use multiple else if statements with an if-else pair to specify different conditions. Flowchart of if-else-if Ladder in C++![]() flow diagram of the if-else-if ladder Example of if-else-ladder in C++The below example demonstrates the use of an if-else-if ladder. In the program, you are given an age and if the age is less the 13 print child, if the age is between 13 to 18 print the growing stage, else print adult. C++
Output
Growing stage 4. Nested if-else in C++The Nested if-else statement contains an ‘if’ statement inside another ‘if’ statement. This structure lets in more complex selection-making by way of comparing multiple conditions. In this type of statement, multiple conditions are checked, and then the body of the last if statement is executed. Syntax of Nested if-else in C++if (condition1) { Flowchart of Nested if-else![]() Flow diagram of Nested if-else Example of Nested if-else in C++The below example demonstrates the use of nested if-else. In the below program, we are given a number and we have to check whether the number is positive or negative or zero if the number is positive check whether it is even or odd. C++
Output
positive and even number 5. Switch Statement in C++In C++, the switch statement is used when multiple situations need to be evaluated primarily based on the value of a variable or an expression. switch statement acts as an alternative to multiple if statements or if-else ladder and has a cleaner structure and it is easy for handling multiple conditions. Syntax of C++ switchswitch (expression) { Flowchart of switch in C++![]() Flowchart of switch in C++ Example pf switch in C++The below example demonstrates the use of switches in decision-making. In the below program, we are given a character and you have to give output as per the given condition: if the given input is A then print GFG, and if the given input is B print GeeksforGeeks else print invalid input. C++
Output
GeeksforGeeks 6. Ternary Operator ( ? : ) in C++The conditional operator is also known as a ternary operator. It is used to write conditional operations provided by C++. The ‘?’ operator first checks the given condition, if the condition is true then the first expression is executed otherwise the second expression is executed. It is an alternative to an if-else condition in C++. Syntax of Ternary Operator in C++condition ? expression1 : expression2 Flowchart of Conditional Operator in C++![]() Flow Diagram of Conditional operator Example of Ternary Operator in C++The below program demonstrates the use of a conditional operator to find the maximum of two numbers. C++
Output
40 7. Jump Statements in C++Jump statements are used to alter the normal flow of the code. If you want to break the flow of the program without any conditions then these jump statements are used. C++ provides four types of jump statements.
A) breakbreak is a control flow statement that is used to terminate the loop and switch statements whenever a break statement is encountered and transfer the control to the statement just after the loop. Syntax break;
break statement is generally used when the actual number of iterations are not predefined so we want to terminate the loop based on some conditions. Flowchart of break ![]() Flow diagram of break Example C++
Output
0 1 2 B) continueThe continue statement is used to skip the loop body for the current iteration and continue from the next iteration. Unlike the break statement which terminates the loop completely, continue allows us just to skip one iteration and continue with the next iteration. Syntax continue;
Flowchart of continue ![]() Flow diagram of continue Example C++
Output
0 1 2 4 C) gotoIt is a jump statement that is used to transfer the control to another part of the program. It transfers the control unconditionally to the labeled statement. Syntax goto label; Flowchart of goto ![]() Flow Diagram of goto Example The below example demonstrates the use of the goto statement. C++
Output
You are not eligible to vote!
D) returnThe return statement is used to exit the function immediately and optionally returns a value to the calling function. It returns to the function from where it was called and if it is the ‘main’ function then it marks the end of the execution. So basically, return is a mechanism used to communicate the results back to the calling function. Syntax return expression; Flowchart of return ![]() Flow diagram of return Example The below example demonstrates the use of a return statement to return a value from a function. C++
Output
The sum is: 8 ConclusionDecision-making is an essential factor of programming in C++, presenting flexibility and management over program execution. When used correctly, it enhances application performance and adaptability. However, overuse or mismanagement of decision-making statements can lead to code complexity, decreased clarity, and protection challenges. Striking a balance and using those statements properly is essential to enhance your code. |
Reffered: https://www.geeksforgeeks.org
C++ |
Related |
---|
![]() |
![]() |
![]() |
![]() |
![]() |
Type: | Geek |
Category: | Coding |
Sub Category: | Tutorial |
Uploaded by: | Admin |
Views: | 14 |