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.
📘 IT Tech Language
☁️ Cloud Computing - What is Cloud Computing – Simple Guide
- History and Evolution of Cloud Computing
- Cloud Computing Service Models (IaaS)
- What is IaaS and Why It’s Important
- Platform as a Service (PaaS) – Cloud Magic
- Software as a Service (SaaS) – Enjoy Software Effortlessly
- Function as a Service (FaaS) – Serverless Explained
- Cloud Deployment Models Explained
🧩 Algorithm - Why We Learn Algorithm – Importance
- The Importance of Algorithms
- Characteristics of a Good Algorithm
- Algorithm Design Techniques – Brute Force
- Dynamic Programming – History & Key Ideas
- Understanding Dynamic Programming
- Optimal Substructure Explained
- Overlapping Subproblems in DP
- Dynamic Programming Tools
🤖 Artificial Intelligence (AI) - Artificial intelligence and its type
- Policy, Ethics and AI Governance
- How ChatGPT Actually Works
- Introduction to NLP and Its Importance
- Text Cleaning and Preprocessing
- Tokenization, Stemming & Lemmatization
- Understanding TF-IDF and Word2Vec
- Sentiment Analysis with NLTK
📊 Data Analyst - Why is Data Analysis Important?
- 7 Steps in Data Analysis
- Why Is Data Analysis Important?
- How Companies Can Use Customer Data and Analytics to Improve Market Segmentation
- Does Data Analytics Require Programming?
- Tools and Software for Data Analysis
- What Is the Process of Collecting Import Data?
- Data Exploration
- Drawing Insights from Data Analysis
- Applications of Data Analysis
- Types of Data Analysis
- Data Collection Methods
- Data Cleaning & Preprocessing
- Data Visualization Techniques
- Overview of Data Science Tools
- Regression Analysis Explained
- The Role of a Data Analyst
- Time Series Analysis
- Descriptive Analysis
- Diagnostic Analysis
- Predictive Analysis
- Pescriptive Analysis
- Structured Data in Data Analysis
- Semi-Structured Data & Data Types
- Can Nextool Assist with Data Analysis and Reporting?
- What Kind of Questions Are Asked in a Data Analyst Interview?
- Why Do We Use Tools Like Power BI and Tableau for Data Analysis?
- The Power of Data Analysis in Decision Making: Real-World Insights and Strategic Impact for Businesses
📊 Data Science - The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science
🧠 Machine Learning (ML) - How Machine Learning Powers Everyday Life
- Introduction to TensorFlow
- Introduction to NLP
- Text Cleaning and Preprocessing
- Sentiment Analysis with NLTK
- Understanding TF-IDF and Word2Vec
- Tokenization and Lemmatization
🗄️ SQL
💠 C++ Programming - Introduction of C++
- Brief History of C++ || History of C++
- Characteristics of C++
- Features of C++ || Why we use C++ || Concept of C++
- Interesting Facts About C++ || Top 10 Interesting Facts About C++
- Difference Between OOP and POP || Difference Between C and C++
- C++ Program Structure
- Tokens in C++
- Keywords in C++
- Constants in C++
- Basic Data Types and Variables in C++
- Modifiers in C++
- Comments in C++
- Input Output Operator in C++ || How to take user input in C++
- Taking User Input in C++ || User input in C++
- First Program in C++ || How to write Hello World in C++ || Writing First Program in C++
- How to Add Two Numbers in C++
- What are Control Structures in C++ || Understanding Control Structures in C++
- What are Functions and Recursion in C++ || How to Define and Call Functions
- Function Parameters and Return Types in C++ || Function Parameters || Function Return Types
- Function Overloading in C++ || What is Function Overloading
- Concept of OOP || What is OOP || Object-Oriented Programming Language
- Class in C++ || What is Class || What is Object || How to use Class and Object
- Object in C++ || How to Define Object in C++
- Polymorphism in C++ || What is Polymorphism || Types of Polymorphism
- Compile Time Polymorphism in C++
- Operator Overloading in C++ || What is Operator Overloading
- Python vs C++ || Difference Between Python and C++ || C++ vs Python
🐍 Python - Why Python is Best for Data
- Dynamic Programming in Python
- Difference Between Python and C
- Mojo vs Python – Key Differences
- Sentiment Analysis in Python
🌐 Web Development
🚀 Tech to Know & Technology
- The History and Evolution of Data Science
- The Importance of Data in Science
- Why Need Data Science?
- Scope of Data Science
- How to Present Yourself as a Data Scientist?
- Why Do We Use Tools Like Power BI and Tableau
- Data Exploration: A Simple Guide to Understanding Your Data
- What Is the Process of Collecting Import Data?
- Understanding Data Types
- Overview of Data Science Tools and Techniques
- Statistical Concepts in Data Science
- Descriptive Statistics in Data Science
- Data Visualization Techniques in Data Science
- Data Cleaning and Preprocessing in Data Science

