What are Control Structures in C++| How to use if, else, switch
{tocify} $title={Table of Contents}
The if
Statement
The if
statement is one of the most basic and commonly used control structures in programming. It allows you to execute a block of code only if a specified condition is true.
if (condition) {
// Code to execute if condition is true
}
For example, consider a simple scenario where we want to check if a number is positive:
#include <iostream> |
using namespace std; |
int main() { |
int number = 10; |
if (number > 0) { |
cout << "The number is positive." << endl; |
} |
return 0; |
} |
The else
Statement
The else
statement complements the if
statement. It provides an alternative block of code that executes if the if
condition is false.
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Continuing with our example, let's handle the case where the number is not positive:
#include <iostream> |
using namespace std; |
int main() { |
int number = -5; |
if (number > 0) { |
cout << "The number is positive." << endl; |
} else { |
cout << "The number is not positive." << endl; |
} |
return 0; |
} |
The else if
Statement
Sometimes, multiple conditions need to be checked sequentially. This is where else if
comes into play.
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if both conditions are false
}
For instance, checking if a number is positive, negative, or zero:
#include <iostream> |
using namespace std; |
int main() { |
int number = 0; |
if (number > 0) { |
cout << "The number is positive." << endl; |
} else if (number < 0) { |
cout << "The number is negative." << endl; |
} else { |
cout << "The number is zero." << endl; |
} |
return 0; |
} |
The switch
Statement
The switch
statement is an alternative to using multiple if else
statements. It evaluates an expression and executes code based on the matching case.
switch (expression) {
case value1:
// Code to execute if expression == value1
break;
case value2:
// Code to execute if expression == value2
break;
// Additional cases
default:
// Code to execute if no case matches
}
Let's use a switch
statement to determine the day of the week:
#include <iostream> |
using namespace std; |
int main() { |
int day = 3; |
switch (day) { |
case 1: |
cout << "Monday" << endl; |
break; |
case 2: |
cout << "Tuesday" << endl; |
break; |
case 3: |
cout << "Wednesday" << endl; |
break; |
case 4: |
cout << "Thursday" << endl; |
break; |
case 5: |
cout << "Friday" << endl; |
break; |
case 6: |
cout << "Saturday" << endl; |
break; |
case 7: |
cout << " Sunday" <<endl; |
default: |
cout << "Invalid day" <<endl; |
} |
return 0; |
} |
Conclusion
Understanding if, else, and switch statements is crucial for controlling the flow of a program. These control structures enable programmers to handle various conditions efficiently and create more dynamic and responsive applications. Practice these constructs to enhance your coding proficiency and tackle more complex problems with ease.
C++
- Introduction of C++ || Definition of C++
- Brief history of C++ || history of C++
- Features of C++ || why we use C++ || concept of C++
- Concept of OOP || What is OOP || Object oriented programming language
- Difference Between OOP And POP || Different Between C and C++
- Characteristics of C++
- Interesting fact about C++ || Top 10 interesting fact about C++
- C++ Program Structure
- Writing first program in C++ || how to write hello world in C++
- Basic Data Type And Variable In C++
- Identifier in C++
- Keywords in C++
- Token in C++
- Comment in C++
- Constant in C++
- Modifier in C++
- Taking User Input in C++ | User input in C++
- Input Output Operator In C++
- C++ Operators | Operator in programming language
- How to Add two number in C++
- Polymorphism in C++
- Compile Time Polymorphism in C++
- Function overloading in C++
- Operator Overloading in C++
- What are Control Structures in C++ || Understanding Control Structures in C++ | How to use if, else, switch
- What are Functions and Recursion in C++ | How to Defining and Calling Functions
- Class in C++
- Object in C++
Data science & data analyst
- Data Cleaning and Preprocessing in Data Science
- Advanced Data Analysis Techniques: Unlocking Insights from Data
- Data Visualization Techniques in Data Science
- Descriptive Statistics in Data Sci
- Data Science Tools and Techniques
- Scope of Data Science
- Why learn Data Science? | Why Data Science?
- Impact of Data Science
- The Importance of Data in Science | Introduction to Data Science
- What is Data Analysis | Data Analyst for Beginners