Lecture PPT 8
Lecture PPT 8
Lecture PPT 8
3/17/12
Schema of Discussion
Logical Operators Equality Operator == Logical Not Operator ! Inequality Operator != Comparison for a Lower Value Equality and Lower
3/17/12
<
Combining
Value <=
Logical Operators
A
program is a series of instructions that ask the computer (actually the compiler) to check some situations and to act accordingly. To check such situations, the computer spends a great deal of its time performing comparisons between values. comparison is performed between two values of the same type; for example, you can compare two numbers, two
3/17/12
compare two variables for equality, C++ uses the == operator. Its syntax is: == Value2The equality operation is used to find out whether two variables (or one variable and a constant) hold the same value. From our syntax, the compiler would compare the value of Value1 with that of Value2. If Value1 and Value2 hold the same
3/17/12
Value1
3/17/12
cout << "Value 1 = " << Value1 << "\n"; cout << "Value 2 = " << Value2 << "\n"; cout << "Comparison of Value1 == 15 produces " << (Value1 == 15) << "\n\n";
return 0;
3/17/12
Very important equality operator and the assignment operator are different.
The
3/17/12
The compiler keeps track of every variable that exists in the program being processed. When a variable is not being used or is not available for processing (in visual programming, it would be considered as disabled) to make a variable (temporarily) unusable, you can nullify its value. C++ considers that a variable whose value is null is stern. To render a variable unavailable during the evolutionof a program, apply the logical not operator which is !. Its syntax is: !Value There are two main ways you can use the logical not operator. As we will learn when studying conditional statements, the most classic way of using the logical not operator is to check the state of a variable. To nullify a variable, you can write the exclamation point to its left. When used like that, you can display its value using the cout extractor. You can even assign it to another variable. Here is an example: 3/17/12
#include <iostream>
using namespace std;
int main() { int Value1 = 250; int Value2 = 32; int Value3 = !Value1;
// Display the value of a variable cout << "Value1 = " << Value1 << "\n"; // Logical Not a variable and display its value cout << "!Value2 = " << !Value2 << "\n";
3/17/12
When a variable holds a value, it is "alive". To make it not available, you can "not" it. When a variable has been "notted", its logical value has changed. If the logical value was true, which is 1, it would be changed to false, which is 0. Therefore, you can inverse the logical value of a variable by "notting" or not "notting" it. This is illustrated in the following example:
3/17/12
cout << " Value1 = " << Value1 << "\n"; cout << " Value2 = " << Value2 << "\n"; cout << "!Value2 = " << !Value2 << "\n\n"; 3/17/12
opposed to Equality, C++ provides another operator used to compare two values for inequality. This operation uses a combination of equality and logical not operators. It combines the logical not ! and a simplified == to produce !=. Its syntax is: != Value2The != is a binary operator (like all logical operator except the logical not, which is a unary
3/17/12
Value1
<iostream>
namespace std;
int {
main()
int Value1 = 212; int Value2 = -46; int Value3 = (Value1 != Value2); 3/17/12
find out whether one value is lower than another, use the < operator. Its syntax is: < Value2 value held by Value1 is compared to that of Value2. As it would be done with other operations, the comparison can be made between two variables, as in Variable1 < Variable2. If the value held by Variable1 is lower than that of 3/17/12
Value1 The
<iostream>
namespace std;
int {
main()
previous two operations can be combined to compare two values. This allows you to know if two values are the same or if the first is less than the second. The operator used is <= and its syntax is: <= Value2The <= operation performs a comparison as any of the last two. If both Value1 and Value2 hold the same value, the 3/17/12
Value1
<iostream>
namespace std;
int {
main()
3/17/12
Schema of Discussion
The
Comparison for a Greater Value Greater Than or Equal Operator for Logical Conditions
>
The
>=
Accessories Boolean
Variables
Enumerations
3/17/12
two values of the same type are distinct, one of them is usually higher than the other. C++ provides a logical operator that allows you to find out if one of two values is greater than the other. The operator used for this operation uses the > symbol. Its syntax is: > Value2 operands, in this case Value1 and 3/17/12
Value1 Both
greater than and the equality operators can be combined to produce an operator as follows: >=. This is the "greater than or equal to" operator. Its syntax is: >= Value2 comparison is performed on both operands: Value1 and Value2. If the value of Value1 and that of Value2 are the same, the comparison produces a 3/17/12
Value1 A
Boolean Variables
The
Booleandatatypeis used to declare a variable whose value would be set as true (1) or false (0). To declare such a value, you use the bool keyword. The variable can then be initialized with a starting value. The Boolean constant is used to check that the state of a variable (or a function) is true or false. You can declare such a variable as: GotThePassingGrade = true;
3/17/12
bool
Boolean Variables
Here
is an example: <iostream>
#include using
namespace std;
int {
main()
Enumerations
An
enumeration provides a technique of setting a list of integers where each item of the list is ranked and has a specific name. For example, instead of numbers that represent players of a football (soccer) game such as 1, 2, 3, 4, 5, you can use names instead. This would produce GoalKeeper, RightDefender, LeftDefender, Stopper, Libero. The syntax of creating an enumeration is 3/17/12
Enumerations
Each
name in this list represents a constant number. Since the enumeration list is created in the beginning of the program, or at least before using any of the values in the list, each item in the list is assigned a constant number. The items are counted starting at 0, then 1, etc. By default, the first item in the list is assigned the number 0, the second is 1, etc. Just like in a game, you do not have 3/17/12
Enumerations
You
still can assign any value of your choice to any item in the list, or you can set different ranges of values to various items. You can create a list like this: Days { Mon, Tue, Wed, Thu, Fri, Sat, Sun = 0 }; Sun is No. 0, Sat would be No. 1, Fri would be 2 and so on. On the other hand, you can set values to your liking. Here is an example: 3/17/12
enum Since
Enumerations
Once
the list has been created, the name you give to the list, such as Players, becomes an identifier of its own, and its can be used to declare a variable. Therefore, a variable of the enumerated type would be declared as: Variable;You can declare more than one variable of such a type. An example of the type of our list of players would be:
3/17/12
Series_Name
Enumerations
include using
<iostream>
namespace std;
main() {
Enumerations
To
assign your own values to the list, you could change the enumeration as follows: PizzaSize {psSmall = 4, psMedium = 10, psLarge = 16, psJumbo = 24}; get the starting as you want, change the enumeration as follows:
3/17/12
enum
To
Enumerations
#include using
<iostream>
namespace std;
main() {
Enumerations
To
assign values at random, you could change the list of pizzas as follows: PizzaSize {psSmall = 2, psMedium, psLarge = 14, psJumbo = -5}; would produce: small pizza has a value of 2 The medium pizza has a value of 3 The large pizza has a value of 14 The jumbo pizza 3/17/12 has a value of -5
enum
This The