Conditional Statements in C++
Conditional statements allow us to control the flow of the program based on certain conditions. It helps us to run a specific section of code based on a condition.
1. The if statement
If statement executes a block of code if and only if the given condition is true.
Syntax
if (condition) { // Code to be executed if the condition is true}
2. Nested if statement
Syntax
if (condition1) { // Code to be executed if condition1 is true if (condition2) { // Code to be executed if condition1 and condition2 are true }}
3. The if-else statement
If the condition inside the if statement is true, then the code inside the if block will get executed, otherwise code inside the else block will get executed.
Syntax
if (condition) { // Code to be executed if the condition is true} else { // Code to be executed if the condition is false}
4. The else-if statement
The else if statement allows you to check for multiple conditions sequentially.
Syntax
if (condition1) { // Code to be executed if condition1 is true} else if (condition2) { // Code to be executed if condition1 is false and condition2 is true} else { // Code to be executed if all conditions are false}
5. Short-hand if else ( Ternary Operator)
Short-hand if else also known as the Ternary operator (?:) works just like if-else statements that can be used to reduce the number of lines of code.
Syntax
(condition) ? expression1 : expression2;
If the condition is true, expression1 will be evaluated and it will become the result of the expression. Otherwise, if the condition is false, expression2 will be evaluated and it will become the result.
6. Switch statement
The switch statement evaluates the expression and compares the value of the expression with the cases. If the expression matches the value of any of the cases, the code associated with that case will be executed.
Break and default keywords are generally used with switch and cases.
7. Break and Default
Break: The break keyword is used to exit the switch statement when one of the cases matches.
Default: Default keyword is optional in switch statements and the code inside the default block is executed when none of the cases matches the value of the expression.
Syntax
switch (expression) {
case value1:
// Code to be executed if expression matches value1
break;
case value2:
// Code to be executed if expression matches value2
break;
// ...
default:
// Code to be executed if expression does not match any case
break;
}
C++ Cheatsheet
This is a C++ programming cheat sheet. It is useful for beginners and intermediates looking to learn or revise the concepts of C++ programming. While learning a new language, it feels annoying to switch pages and find different websites for different concepts that are easily understandable. You can learn C++ concepts very easily using this cheat sheet.
C++ is a high-level programming language. It was developed in 1983 by Bjarne Stroustrup at Bell Labs. It is used for developing various applications.
Contact Us