The basic if statement in C++ evaluates a condition. If the condition is true, the code block within the if statement is executed.
#include <iostream>
using namespace std;
int main() {
int age = 18;
if (age >= 18) {
cout << "You are eligible to vote.";
}
return 0;
}
Console Output:
You are eligible to vote.
The if-else statement allows you to execute one block of code if the condition is true and another block if it is false.
#include <iostream>
using namespace std;
int main() {
int number = 10;
if (number % 2 == 0) {
cout << "The number is even.";
} else {
cout << "The number is odd.";
}
return 0;
}
Console Output:
The number is even.
Nested if statements are used when you need to check multiple conditions. An if statement inside another if statement is called nested if.
#include <iostream>
using namespace std;
int main() {
int number = 15;
if (number > 10) {
if (number < 20) {
cout << "The number is between 10 and 20.";
}
}
return 0;
}
Console Output:
The number is between 10 and 20.
The if-else-if ladder is used to check multiple conditions. It executes the block of code associated with the first true condition.
#include <iostream>
using namespace std;
int main() {
int score = 85;
if (score >= 90) {
cout << "Grade A";
} else if (score >= 80) {
cout << "Grade B";
} else if (score >= 70) {
cout << "Grade C";
} else {
cout << "Grade D";
}
return 0;
}
Console Output:
Grade B
The conditional operator (?:) is a shorthand for the if-else statement. It evaluates a condition and returns one of two values.
#include <iostream>
using namespace std;
int main() {
int a = 5, b = 10;
int max = (a > b) ? a : b;
cout << "The maximum value is " << max;
return 0;
}
Console Output:
The maximum value is 10
The switch statement allows you to execute different parts of code based on the value of a variable or expression. It's an alternative to if-else-if ladders when dealing with multiple conditions.
#include <iostream>
using namespace std;
int main() {
int day = 3;
switch(day) {
case 1:
cout << "Monday";
break;
case 2:
cout << "Tuesday";
break;
case 3:
cout << "Wednesday";
break;
default:
cout << "Invalid day";
}
return 0;
}
Console Output:
Wednesday
Logical operators are used to combine multiple conditions in an if statement. The most common logical operators are AND (&&), OR (||), and NOT (!).
#include <iostream>
using namespace std;
int main() {
int age = 20;
bool hasID = true;
if (age >= 18 && hasID) {
cout << "You can enter the club.";
} else {
cout << "Access denied.";
}
return 0;
}
Console Output:
You can enter the club.
Comparative operators are used to compare two values. Common operators include less than (<), greater than (>), equal to (==), and not equal to (!=).
#include <iostream>
using namespace std;
int main() {
int x = 10, y = 20;
if (x != y) {
cout << "x and y are not equal.";
}
return 0;
}
Console Output:
x and y are not equal.
You can combine multiple conditions in an if statement using logical operators to create complex decision-making structures.
#include <iostream>
using namespace std;
int main() {
int age = 25;
bool isStudent = false;
if (age >= 18 && age < 30 || isStudent) {
cout << "Eligible for a discount.";
} else {
cout << "Not eligible for a discount.";
}
return 0;
}
Console Output:
Eligible for a discount.
Newsletter
Subscribe to our newsletter for weekly updates and promotions.
Wiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWiki E-Learning
E-LearningComputer Science and EngineeringMathematicsNatural SciencesSocial SciencesBusiness and ManagementHumanitiesHealth and MedicineEngineeringWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWikiCode
Programming LanguagesWeb DevelopmentMobile App DevelopmentData Science and Machine LearningDatabase ManagementDevOps and Cloud ComputingSoftware EngineeringCybersecurityGame DevelopmentWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki News
World NewsPolitics NewsBusiness NewsTechnology NewsHealth NewsScience NewsSports NewsEntertainment NewsEducation NewsWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterWiki Tools
JPEG/PNG Size ReductionPDF Size CompressionPDF Password RemoverSign PDFPower Point to PDFPDF to Power PointJPEG to PDF ConverterPDF to JPEG ConverterWord to PDF ConverterCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesCompany
About usCareersPressCompany
About usCareersPressCompany
About usCareersPressLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesLegal
TermsPrivacyContactAds PoliciesAds Policies