C++ Conditions
C++ has the following conditional statements:
Use
if
to specify a block of code to be executed, if a specified condition is trueUse
else
to specify a block of code to be executed, if the same condition is falseUse
else if
to specify a new condition to test, if the first condition is falseUse
switch
to specify many alternative blocks of code to be executed
The if Statement
Use the if
statement to specify a block of C++ code to be executed if a condition is true
.
Syntax
if (condition) { // block of code to be executed if the condition is true }
Example
#include <iostream>
using namespace std;
int main() {
int x = 20;
int y = 18;
if (x > y) {
cout << "x is greater than y";
}
return 0;
} //result x is greater than y
The else Statement
Use the else
statement to specify a block of code to be executed if the condition is false
.
Syntax
if (condition) { // block of code to be executed if the condition is true } else { // block of code to be executed if the condition is false }
Example
#include <iostream>
using namespace std;
int main() {
int time = 20;
if (time < 18) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
} //result Good evening.
C++ Else If
Use the else if
statement to specify a new condition if the first condition is false
Syntax
if (condition1) { // block of code to be executed if condition1 is true } else if (condition2) { // block of code to be executed if the condition1 is false and condition2 is true } else { // block of code to be executed if the condition1 is false and condition2 is false }
Example
int main() {
int time = 22;
if (time < 10) {
cout << "Good morning.";
} else if (time < 20) {
cout << "Good day.";
} else {
cout << "Good evening.";
}
return 0;
} // result Good evening.
C++ Switch
Use the switch
statement to select one of many code blocks to be executed.
Syntax
switch(expression) { case x: // code block break; case y: // code block break; default: // code block }
This is how it works:
The
switch
expression is evaluated onceThe value of the expression is compared with the values of each
case
If there is a match, the associated block of code is executed
The
break
anddefault
keywords are optional and will be described later in this chapter
#include <iostream>
using namespace std;
int main(){
int day=4;
switch (day)
{
case 1:
cout<<"mmonday";
break;
case 2:
cout<<"tuesday";
break;
case 3:
cout<<"wednesday";
break;
case 4:
cout<<"thersday";
break;
case 5:
cout<<"friday";
break;
case 6:
cout<<"satutrday";
break;
case 7:
cout<<"sundat";
default:
break;
}
return 0;
}// result thersday
Subscribe to my newsletter
Read articles from Aditya Gupta directly inside your inbox. Subscribe to the newsletter, and don't miss out.
Written by
Aditya Gupta
Aditya Gupta
Hello, everyone! My name is Aditya Gupta, and I'm thrilled to welcome you to my programming language blog and project. As a passionate web developer, I've dedicated myself to exploring the vast world of programming languages and sharing my knowledge with fellow enthusiasts like you. Through my blog, I aim to provide informative and engaging content that will help you navigate the intricacies of various programming languages, including Python, JavaScript, Java, and more. Additionally, I'm actively working on an exciting project that combines my expertise in web development and programming languages. Join me on this thrilling journey as we delve into the fascinating realm of code and unleash our creativity together. Let's write beautiful and functional programs that empower us to shape the digital landscape.