Sunteți pe pagina 1din 49

C++ Booleans

•In programming, you will need a data


type that can only have one of two
values, like:
•YES / NO
•ON / OFF
•TRUE / FALSE
Boolean Values
• A boolean variable is declared with the bool
keyword and can only take the values true or false:
bool isCodingFun = true;
bool isFishTasty = false;
cout << isCodingFun << "\n"; // Outputs 1 (true)
cout << isFishTasty; // Outputs 0 (false)
Boolean Expression
• A Boolean expression is a C++ expression that returns a
boolean value: 1 (true) or 0 (false).
• You can use a comparison operator, such as the greater than
(>) operator to find out if an expression (or a variable) is
true:
int x = 10;
int y = 9;
cout << (x > y); // returns 1 (true), because 10 is higher than 9
Or even easier:
cout << (10 > 9); // returns 1 (true), because 10 is
higher than 9
•In the examples below, we use the equal
to (==) operator to evaluate an
expression:
int x = 10;
cout << (x == 10); // returns 1 (true),
because the value of x is equal to 10
•cout << (10 == 15); // returns 0
(false), because 10 is not equal to
15
Characters
•The char data type is used to store a single
character. The character must be
surrounded by single quotes, like 'A' or 'c':
char myGrade = 'B';
cout << myGrade;
•ASCII was the first character set (encoding
standard) used between computers on the
Internet.
•ASCII stands for the "American Standard Code
for Information Interchange".
•It was designed in the early 60's, as a standard
character set for computers and electronic
devices.
•Alternatively, you can use ASCII values to display
certain characters:
•Example
•char a = 65, b = 66, c = 67;
cout << a;
cout << b;
cout << c;

•Tip: A list of all ASCII values can be found in


our ASCII Table Reference.
• Although the + operator is often used to add together two values, like in
the example above, it can also be used to add together a variable and a
value, or a variable and another variable:
•int sum1 = 100 + 50;
int sum2 = sum1 + 250;
int sum3 = sum2 + sum2;
C++ Operators
• Operators are used to perform operations on variables and values.

• In the example below, we use the + operator to add together two


values:
int x = 100 + 50;
cout << x;
•C++ divides the operators into the following
groups:
•Arithmetic operators
•Assignment operators
•Comparison operators
•Logical operators
Arithmetic Operators
• Arithmetic operators are used to perform common mathematical
operations.
Int a= 5; int b= 2; int c=10;
++b, cout << a+b* c;
int x = 5; int y = 2; int z;
++ y; z = x + y; cout << z ;
int x = 45; int y = 5; int z;
++ x; z = x - y; cout << z ;
int x = 45; int y = 5; int z;
++ x; ++ x; ++ y; z = x - y; cout << z ;
C++ Assignment Operators
•Assignment operators are used to assign values to
variables.
•In the example below, we use the assignment
operator (=) to assign the value 10 to a variable
called x:
•int x = 10;
•The addition assignment operator (+=)
adds a value to a variable:

int x = 10;
x += 5;
cout x;
int x = 10;
Int y = 90;
x += 5;
cout x;
int x=10;
int y=23;
x +=50;
cout << x-y;
int x = 14; int y = 5; int z; x += 5;
z = x - y; cout << z ;
Subtraction Assignment Operator -=

int x=10;
int y=23;
x -=5;
cout << x+y;
Multiplication Assignment Operator *=

int x=10;
int y=23;
x *=5;
cout << x+y;
Division Assignment Operator /=

int x=10;
int y=23;
x /=5;
cout << x+y;
•Exercises in Arithmetic and
Assignment Operators
1.

int a=82; int b=48; int c=8;


cout << a +b-c;
2.

int a= 78; int b=52; ++a; b-=5;


cout << a-b;
3.

int x =72; int y=48; int z; y-


=14; z=x*y; cout << z;
C++ Comparison Operators
• Comparison operators are used to compare two values.
• Tip: The return value is either true (1) or false (0). You will learn more
about them and how to use them in a later chapter.
== Equal to

int x = 5;
int y = 3;
cout << (x == y);
Not equal !=
• int x = 5;
• int y = 3;
• cout << (x != y);
Greater than >
• int x = 5;
• int y = 3;
• cout << (x > y);
• int x = 5;
• int y = 3;
• cout << (x < y);

less than <


Greater than or equal to >=
• int x = 5;
• int y = 3;
• cout << (x >= y); // returns 1 (true) because five is greater than, or
equal, to 3
Less than or equal to <=
• int x = 5;
• int y = 3;
• cout << (x <= y); // returns 0 (false) because 5 is neither less than or
equal to 3
1.

int x=71; int y=52; y-=19;


cout << (x>=y);
2.

int a=57; int b=25; --a; b/=5;


cout << (a!=b);
3.

int a=49; int b=74; ++a; b-=16;


cout << (a <=b);
4.

int x=10; int y=7; --x; b+=10;


cout << (a==b);
5.

int a=56; a-=65; cout << (a>=65);


C++ Logical Operators
Logical and &&
•int x = 5;
• int y = 3;
• cout << (x > 3 && x < 10);
Logical or ||
• int x = 5;
• int y = 3;
• cout << (x > 3 || x < 4);
Logical not !
• int x = 5;
• int y = 3;
• cout << (!(x > 3 && x < 10));
Exercises: C++ operation
1. Multiply 10 with 5, and print the result.
cout << 10 _____ 5;
2. Divide 10 by 5, and print the result.
cout << 10 _____ 5;
3. Use the correct operator to increase the value of the variable x by 1.

S-ar putea să vă placă și