They can be applied to any comparable data types, and the result is an integer: 0 (false) or 1 (true).
Operator | Meaning |
---|---|
< | Less than |
<= | Less than or equal to |
> | Greater than |
>= | Greater than or equal to |
== | Equality |
!= | Inequality (difference) |
They are most commonly used in combination with relational operators to form complex logical expressions, which again return a result of 0 or 1.
Operator | Meaning |
---|---|
&& | Logical AND |
|| | Logical OR |
! | Negation (NOT) |
Example:
int a = 5 && 0; // a = 0;
= 2 && 5; // a = 1;
a = 0 || 5; // a = 1;
a = !0; // a = 1;
a = !5; // a = 0; a
=
.= (y = 10) * (z = 5);
x = y = z = 20; x
The value of the variable is increased before the expression in which it participates is calculated.
= ++b; a
The value of the variable is increased after the expression is calculated.
= b++; a
+=
operator+= 5; // a = a + 5;
a += b * c; // a = a + b * c; a
-=
operator-= 3; // a = a - 3; a
*=
operator*= 3; // a = a * 3; a
/=
operator/= 3; // a = a / 3; a
%=
operator%= 3; // a = a % 3; a
if (condition) {
;
statements_if_true}
else {
;
statements_if_false}
Example: What will it print?
#include <iostream>
using namespace std;
int main() {
int m = 5, n = 10;
if (m > n)
++m;
++n;
<< "m = " << m << ", n = " << n;
cout return 0;
}
Solution: m = 5, n = 11
Write a program that reads a character from the keyboard and prints 1 if it is a lowercase letter or 0 if it is an uppercase letter.
Solution:
#include <iostream>
using namespace std;
int main()
{
char ch;
int result;
<< "Enter a character: ";
cout >> ch;
cin = (ch >= 'a' && ch <= 'z');
result << result;
cout return 0;
}
Bonus solution:
= (ch >= '0' && ch <= '9'); result
The coordinates of a point on a plane are entered from the keyboard. Write a program that will print to which quadrant the entered point belongs.
Solution 1
#include <iostream>
using namespace std;
int main () {
float x, y;
<<"Enter coordinates:"<<endl;
cout>>x>>y;
cinif (x > 0 && y > 0)
<<"I quadrant."<<endl;
coutif (x > 0 && y < 0)
<<"IV quadrant."<<endl;
coutif (x < 0 && y > 0)
<<"II quadrant."<<endl;
coutif (x < 0 && y < 0)
<<"III quadrant."<<endl;
coutreturn 0;
}
Solution 2
#include <iostream>
using namespace std;
int main () {
float x, y;
<<"Enter coordinates:"<<endl;
cout>>x>>y;
cinif (x > 0)
if (y > 0)
<<"I quadrant."<<endl;
coutelse
<<"IV quadrant."<<endl;
coutelse if (y > 0)
<<"II quadrant."<<endl;
coutelse
<<"III quadrant."<<endl;
coutreturn 0;
}
Does this program cover all cases?
Will it print something for any entered point?
Solution v3
#include <iostream>
using namsepace std;
int main () {
float x, y;
<<"Enter coordinates:"<<endl;
cout>>x>>y;
cinif (x > 0)
if (y > 0)
<<"I quadrant."<<endl;
coutelse if (y < 0)
<<"IV quadrant."<endl;
coutelse
<<"Positive X."<endl;
coutelse if (x < 0)
if (y > 0)
<<"II quadrant."<endl;
coutelse if (y < 0)
<<"III quadrant."<endl;
coutelse
<<"Negative X."<endl;
coutelse
if (y > 0)
<<"Positive Y."<endl;
coutelse if (y < 0)
<<"Negative Y."<endl;
coutelse
<<"Coordinate start");
coutreturn 0;
}
Write a program that will generate an appropriate grade for an entered number of points from an exam according to the following table:
Решение
#include <iostream>
using namespace std;
int main () {
int points, grade = 0;
<<"Vnesi points: "<<endl;
cout>>points;
cinif (points >= 0 && points <= 50) grade = 5;
else if (points > 50 && points <= 60) grade = 6;
else if (points > 60 && points <= 70) grade = 7;
else if (points > 70 && points <= 80) grade = 8;
else if (points > 80 && points <= 90) grade = 9;
else if (points > 90 && points <= 100) grade = 10;
else cout<<"Nevalidna vrednost na points!"<<endl;
<<"grade: "<<grade<<endl;
coutreturn 0;
}
Решение в2
#include <iostream>
using namespace std;
int main () {
int points, grade = 0;
<<"Vnesi points: "<<endl;
cout>>points;
cinif (points < 0 || points > 100)
<<"Nevalidna vrednost za points!"<<endl;
coutelse {
if (points > 90) grade = 10;
else if (points > 80) grade = 9;
else if (points > 70) grade = 8;
else if (points > 60) grade = 7;
else if (points > 50) grade = 6;
else grade = 5;
<<"Grade: "<<grade<<endl;
cout}
return 0;
}
Modify the previous program so that in addition to the grades, it will also print the signs + and - depending on the value of the last digit of the points:
Example:
81 = 9- 94 = 10 68 = 7+
The + or - sign should not be added for the grade 5, and the + sign should not be added for the grade 10.
Solution
#include <iostream>
using namespace std;
int main () {
int points, grade = 0;
<<"Enter points: "<<endl;
cout>>points;
cinif (points < 0 || points > 100)
<<"Invalid points!"<<endl;
coutelse {
if (points > 90) grade = 10;
else if (points > 80) grade = 9;
else if (points > 70) grade = 8;
else if (points > 60) grade = 7;
else if (points > 50) grade = 6;
else grade = 5;
char znak = ' ';
int pc = points % 10;
if (grade != 5) {
if (pc >= 1 && pc <= 3) znak = '-';
else if (grade != 10 && (pc >= 8 || pc == 0))
= '+';
znak }
<<"Grade: "<<grade<<znak<<endl;
cout}
return 0;
}
The lengths of three line segments are entered from the keyboard in random order. Write a program that will check whether a triangle can be constructed from the line segments, and if it can, whether it is a right triangle and calculate its area. Otherwise, appropriate messages should be printed.
Solution
#include <iostream>
using namespace std;
int main() {
float a, b, c;
<<"Vnesi dolzini na strani: "<<endl;
cout>>a>>b>>c;
cinif ((a + b <= c) || (a + c <= b) || (b + c <= a))
<<"Ne moze da se konstruira triagolnik."<<endl;
coutelse {
if (a >= b) {
float tmp = a;
= b;
a = tmp;
b }
if (a >= c) {
float tmp = a;
= c;
a = tmp;
c }
if (b >= c) {
float tmp = b;
= c;
b = tmp;
c } // po ova najdolgata strana kje bide vo c
if (c * c == a * a + b * b) {
<<"Tragolnikot e pravoagolen."<<endl;
cout<<"Ploshtinata mu e: "<<a * b / 2;
cout} else {
<<"Tragolnikot NE e pravoagolen."<<endl;
cout}
}
return 0;
}
The lengths of three line segments are entered from the keyboard in random order. Check whether a triangle can be constructed from the given line segments. If it can, print whether the triangle is scalene, equilateral, or isosceles, and calculate its area.
Решение
#include <iostream>
#include <cmath>
using namespace std;
int main() {
float a, b, c;
<<"Vnesi dolzini na strani: "<<endl;
cout>>a>>b>>c;
cinif ((a + b <= c) || (a + c <= b) || (b + c <= a))
<<"A triangle cannot be constructed."<<endl;
coutelse {
if (a == b && b == c)
<<"The triangle is equilateral."<<endl; // equilateral
coutelse if (a == b || b == c || a == c)
<<"The triangle is isosceles."<<endl; // isosceles
coutelse
<<"The triangle is scalene."<<endl; // scalene
coutfloat p, s = (a + b + c) / 2;
= sqrt(s * (s - a) * (s - b) * (s - c));
p <<"Area :"<<p;
cout}
return 0;
}
Write a program to calculate the age of a dog in human years. The dog’s age is read from standard input.
If a negative number is entered for the age, print the following message: “Age must be a positive number”.
Note: For the first two years, one dog year is equal to 10.5 human years. After that, each dog year is equal to 4 human years.
#include <iostream>
using namespace std;
int main() {
int dogAge, humanAge;
<< "Enter dog age: ";
cout >> dogAge;
cin
if (dogAge < 0) {
<< "Age must be a positive number" << endl;
cout } else {
if (dogAge <= 2) {
= dogAge * 10.5;
humanAge } else {
= 2 * 10.5 + (dogAge - 2) * 4;
humanAge }
<< "Human age of the dog is: " << humanAge << endl;
cout }
return 0;
}
Write a program that will print the maximum of two numbers whose values are read from the keyboard.
Solution:
#include <iostream>
using namespace std;
int main() {
int a, b;
<< "Enter 2 numbers: " << endl;
cout >> a >> b;
cin if (a > b)
<< "Maximum: " << a;
cout else
<< "Maximum: " << b;
cout return 0;
}
Solution 2 (without if-else):
#include <iostream>
using namespace std;
int main() {
int a, b, max;
<< "Enter 2 numbers: " << endl;
cout >> a >> b;
cin = (a > b) ? a : b;
max << "Maximum: " << max;
cout return 0;
}
Write a program that checks if a given year, read from the keyboard, is a leap year or not and prints an appropriate message.
Examples of leap years: 1976, 2000, 2004, 2008, 2012…
Hint: A year is a leap year if it is divisible by 4 but not divisible by 100, or if it is divisible by 400.
Solution:
#include <iostream>
using namespace std;
int main() {
int year;
<< "Enter a year: " << endl;
cout >> year;
cin if ((year % 4 == 0 && year % 100 != 0) || year % 400 == 0)
<< year << " is a leap year." << endl;
cout else
<< year << " is not a leap year." << endl;
cout return 0;
}