In this lesson, we’ll explore how to actually put values into variables and use those values.
int x;
// define an integer variable named x
int y, z; // define two integer variables, named y and z
As a reminder, here’s a short snippet that first allocates a single integer variable named x, then allocates two more integer variables named y and z:
#include <iostream>
int main()
{
int width;
width =
4; // copy assignment of value 4 into variable width
// variable width now has value 5
width =
7; // change value stored in variable width to 7
// variable width now has value 7
return 0;
}
When we assign value 4 to variable width,
the value 5 that was there previously is overwritten. Normal variables can only hold one value at a time.
Initializing multiple variables
int a
= 5,
b =
6; // copy initialization
int c(
7 ),
d( 8
); // direct initialization
int e{
9 },
f{ 10
}; // list initialization (preferred)
5.7 — Logical operators
ogical operators provide us with the capability to test multiple conditions.
C++ has 3 logical operators:
Operator |
Symbol |
Form |
Operation |
Logical NOT |
! |
!x |
true if x is false, or false if x is true |
Logical AND |
&& |
x && y |
true if both x and y are true, false otherwise |
Logical OR |
|| |
x || y |
true if either x or y are true, false otherwise |
Logical NOT
You have already run across the logical NOT unary operator in lesson 4.9 -- Boolean values. We can summarize the effects of logical NOT like so:
Logical NOT (operator !) |
Operand |
Result |
true |
false |
false |
true |
Logical NOT is often used in conditionals:
|
bool tooLarge
{ x
> 100
}; // tooLarge is true if x > 100
if (!tooLarge)
// do something with x
else
// print an error
|
#include
int main()
{
int x{ 5 };
int y{ 7 };
if (!(x > y))
std::cout << x << " is not greater than " << y << '\n';
else std::cout << x << " is greater than " << y << '\n'; return 0;
}
Logical OR
The logical OR operator is used to test whether either of two conditions is true. If the left operand evaluates to true, or the right operand evaluates to true, or both are true, then the logical OR operator returns true. Otherwise it will return false.
Logical OR (operator ||) |
Left operand |
Right operand |
Result |
false |
false |
false |
false |
true |
true |
true |
false |
true |
true |
true |
true |
For example, consider the following program:
#include
int main()
{
std::cout << "Enter a number: "; int value {};
std::cin >> value; if (value == 0 || value == 1)
std::cout << "You picked 0 or 1\n";
else std::cout << "You did not pick 0 or 1\n"; return 0;
}
In this case, we use the logical OR operator to test whether either the left condition (value == 0) or the right condition (value == 1) is true. If either (or both) are true, the logical
OR operator evaluates to true, which means the if
statement executes. If neither are true, the logical
OR operator evaluates to false, which means the else
statement executes.
Logical AND
The logical AND operator is used to test whether both operands are true. If both operands are true, logical AND returns true. Otherwise, it returns false.
Logical AND (operator &&) |
Left operand |
Right operand |
Result |
false |
false |
false |
false |
true |
false |
true |
false |
false |
true |
true |
true |
For example, we might want to know if the value of variable x is between 10 and 20. This is actually two conditions: we need to know if x is greater than 10, and also whether x is less than 20.
#include
int main()
{
std::cout << "Enter a number: "; int value {}; std::cin >> value;
if (value > 10 && value < 20) std::cout << "Your value is between 10 and 20\n";
else std::cout << "Your value is not between 10 and 20\n"; return 0;
}
In this case, we use the logical AND operator to test whether the left condition (value > 10) AND the right condition (value < 20) are both true. If both are true, the logical AND operator evaluates to true, and the if statement executes. If neither are true, or only one is true, the logical AND operator evaluates to false, and the else statement executes.
As with logical OR, you can string together many logical AND statements:
if (value > 10 && value < 20 && value != 16)
// do something
else
// do something else
If all of these conditions are true, the if
statement will execute. If any of these conditions are false, the else
statement will execute.