Summary - Operators
- C++ course
- Beginner
- Lesson 29/116
- 5 min
Combine arithmetic, comparison and bitwise operators to build a grade calculator.
Summary - Operators
In this section you learned C++ operators:
- Arithmetic:
+ - * / %and++ -- - Assignment:
= += -= *= /= %= - Comparison:
== != < > <= >= - Logical:
&& || ! - Bitwise:
& | ^ ~ << >>
Now you will combine these operators in two tasks.
Practice Arithmetic and comparison
Inside main, declare int x = 8; and int y = 3;. Compute the sum with int sum = x + y;. Check whether the sum is greater than 10 with bool check = sum > 10;. Print sum and check on separate lines with std::cout using std::endl.
Starter code
#include <iostream>
int main() {
// Declare x and y
// Compute sum and check, print both
return 0;
}Sample solution
#include <iostream>
int main() {
// Declare x and y
int x = 8;
int y = 3;
// Compute sum and check, print both
int sum = x + y;
bool check = sum > 10;
std::cout << sum << std::endl;
std::cout << check << std::endl;
return 0;
}Expected output
11 1
Practice Logical and arithmetic together
Inside main, declare int a = 5; and int b = 10;. Check whether both are positive with bool result = (a > 0) && (b > 0);. Print result with std::cout using std::endl.
Starter code
#include <iostream>
int main() {
// Declare a and b
// Compute result and print it
return 0;
}Sample solution
#include <iostream>
int main() {
// Declare a and b
// Compute result and print it
int a = 5;
int b = 10;
bool result = (a > 0) && (b > 0);
std::cout << result << std::endl;
return 0;
}Expected output
1
Questions and answers
Review the lesson questions together with their answers. To test yourself, solve the lesson interactively.
1 What is the result of 7 > 3 && 2 < 1?
- 1 (true)
- 0 (false) Correct answer
- Error
- 7
Explanation: 7>3 is true but 2<1 is false; true && false = false.
2 Match operator categories
| Term | Meaning |
|---|---|
+ - * / |
Arithmetic |
+= -= |
Assignment |
== != |
Comparison |
&& || |
Logical |