if statement” in C++ is used to check if a certain condition is true or false. If the condition is true, a block of code will be executed. If the condition is false, the block of code will be skipped.
Here’s an example of an if statement in C++:
int x = 5;
if (x > 3) {
cout << "x is greater than 3.";
}
In this example, the condition being checked is “x > 3”. The value of x is 5, which is greater than 3, so the condition is true. As a result, the text “x is greater than 3.” will be printed to the screen.
You can also use “else” to specify a block of code to be executed if the condition is false:
int x = 2;
if (x > 3) {
cout << "x is greater than 3.";
} else {
cout << "x is not greater than 3.";
}
In this example, the value of x is 2 which is not greater than 3, so the condition is false, the text “x is not greater than 3.” will be printed to the screen.
You can also use “else if” to check for multiple conditions:
int x = 2;
if (x > 3) {
cout << "x is greater than 3.";
} else if( x < 3) {
cout << "x is less than 3.";
} else {
cout << "x is equal to 3.";
}
In this example, x is 2, which is less than 3, so the second condition is true, the text “x is less than 3.” will be printed to the screen.
In C++, there are several operators that can be used in conditional statements (such as if and while) to check the relationships between values or variables. These operators are known as conditional operators.
Here are some of the most commonly used conditional operators in C++:
==
(equal to): This operator checks if two values or variables are equal to each other. For example,(x == 3)
would returntrue
if the value of x is 3.!=
(not equal to): This operator checks if two values or variables are not equal to each other. For example,(x != 3)
would returntrue
if the value of x is not 3.>
(greater than): This operator checks if one value or variable is greater than another. For example,(x > 3)
would returntrue
if the value of x is greater than 3.<
(less than): This operator checks if one value or variable is less than another. For example,(x < 3)
would returntrue
if the value of x is less than 3.>=
(greater than or equal to): This operator checks if one value or variable is greater than or equal to another. For example,(x >= 3)
would returntrue
if the value of x is greater than or equal to 3.<=
(less than or equal to): This operator checks if one value or variable is less than or equal to another. For example,(x <= 3)
would returntrue
if the value of x is less than or equal to 3.&&
(logical and): This operator checks if two conditions are true. For example,(x > 3 && x < 6)
would returntrue
if the value of x is greater than 3 and less than 6.||
(logical or): This operator checks if at least one of two conditions is true. For example,(x > 3 || x < 2)
would returntrue
if the value of x is greater than 3 or less than 2.
You can also use compound statements like !(x == 3)
to check if x is not equal to 3.
It is important to note that conditional statements are used to control the flow of execution in a program. These conditional operators and statements help you to make decisions based on certain conditions.
Here are a few more examples of how you can use conditional operators in C++:
int x = 4, y = 6;
if (x == y) {
cout << "x and y are equal.";
} else {
cout << "x and y are not equal.";
}
In this example, the value of x is 4 and the value of y is 6, so the condition (x == y)
is false, and the text “x and y are not equal.” will be printed to the screen.
int age = 25;
if (age >= 18) {
cout << "You are an adult.";
} else {
cout << "You are not an adult.";
}
In this example, the value of age is 25, which is greater than or equal to 18, so the condition (age >= 18)
is true, and the text “You are an adult.” will be printed to the screen.
bool isRainy = true;
bool isWindy = false;
if (isRainy && isWindy) {
cout << "It's raining and windy outside.";
} else {
cout << "It's not raining and windy outside.";
}
In this example, the value of isRainy is true and the value of isWindy is false, so the condition (isRainy && isWindy)
is false, and the text “It’s not raining and windy outside.” will be output to the screen.
The basic structure and use of if statements are generally similar across most programming languages, including C++. However, there may be some differences in syntax or specific features that are unique to certain languages.
For example, in C++, you can use the ternary operator (condition) ? (expression1) : (expression2)
as a shorthand for an if-else statement. This operator evaluates the condition and returns the value of expression1 if the condition is true, or the value of expression2 if the condition is false.
int x = 5;
int result = (x > 3) ? 1 : 0;
In this example, the value of x is 5, which is greater than 3, so the condition is true, the result will be 1.
Another example is that in some other languages like Python, the indentation level is used to indicate the scope of the code block, instead of curly braces like in C++. This can make the code more readable and easier to understand.
if x > 3:
print("x is greater than 3.")
else:
print("x is not greater than 3.")
In terms of logical operators, the basic logical operators such as &&
(and) and ||
(or) are also available in most languages, but the syntax for using them might be different. For example, in Python, the operator for “and” is and
, and the operator for “or” is or
.
In some languages like SQL, if statements are used to filter data from a table based on certain conditions, this is different from the usage of if statements in general programming languages where the main purpose is to control the flow of execution.
In summary, while the basic structure and use of if statements are similar across most programming languages, there may be some differences in syntax or specific features that are unique to certain languages.
Here are some practice exercises to help you solidify your understanding of if statements in C++:
- Write a program that prompts the user to enter two integers, and then checks if the first integer is divisible by the second integer. If it is, the program should print “The first number is divisible by the second number.” If it’s not, the program should print “The first number is not divisible by the second number.”
- Write a program that prompts the user to enter a temperature in Celsius, and then converts it to Fahrenheit and displays the result. If the temperature is below freezing (0 degrees Celsius), the program should print “The temperature is below freezing.”
- Write a program that prompts the user to enter a year, and then checks if it is a leap year. If it is a leap year, the program should print “The year is a leap year.” If it is not a leap year, the program should print “The year is not a leap year.”
- Write a program that prompts the user to enter three integers, and then checks if the third integer is between the first and second integers (inclusive). If it is, the program should print “The third number is between the first and second numbers.” If it’s not, the program should print “The third number is not between the first and second numbers.”
- Write a program that prompts the user to enter a letter, and then checks if it is a vowel. If it is a vowel, the program should print “The letter is a vowel.” If it is not a vowel, the program should print “The letter is not a vowel.”
Here are some resources:
https://www.w3schools.com/cpp/cpp_conditions.asp
https://www.programiz.com/cpp-programming/if-else
https://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm
https://www.cprogramming.com/tutorial/lesson2.html
https://en.cppreference.com/w/cpp/language/if
This is all for now!
Peace!