2621008 - c++ 2
DESCRIPTION
TRANSCRIPT
![Page 1: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/1.jpg)
Slide 1
![Page 2: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/2.jpg)
Slide 2
Expressions
In C++, there are many special characters with particular meanings. Examples include the arithmetic operators:
Relational, Equality, and Logical Operators
Just as with other operators, the relational, equality, and logical operators have rules of precedence and associativity that determine precisely how expressions involving them are evaluated. C++ systems use the bool values truetrue and falsefalse to direct the flow of control in the various statement types.
![Page 3: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/3.jpg)
Slide 3
ExpressionsRelational, Equality, and Logical Operators
the relational, equality, and logical operators have rules of precedence and associativity that determine precisely how expressions involving them are evaluated. C++ systems use the bool values truetrue and falsefalse to direct the flow of control in the various statement types.
![Page 4: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/4.jpg)
Slide 4
ExpressionsLogical AND (&&)
p q p && q
T T
T F
F T
F F
![Page 5: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/5.jpg)
Slide 5
ExpressionsLogical AND (&&)
p q p && q
T T T
T F F
F T F
F F F
![Page 6: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/6.jpg)
Slide 6
ExpressionsLogical OR
p q p || q
T T
T F
F T
F F
![Page 7: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/7.jpg)
Slide 7
ExpressionsLogical OR
p q p || q
T T T
T F T
F T T
F F F
![Page 8: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/8.jpg)
Slide 8
ExpressionsLogical negation
p !p
T
F
![Page 9: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/9.jpg)
Slide 9
ExpressionsLogical negation
p !p
T F
F T
![Page 10: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/10.jpg)
Bitwise Operators
Slide 10
Bitwise operation means convert the number into binary and the carry out the operation on each bit individually. For example let us take the operation complement represented by symbol (~). Let me take a numbershort A = 42;A short number is stored in two byte or 16 bits. Therefore,
A when expressed in binary = 00000000 00101010Complement of A is ~A = 11111111 11010101
Contd...
![Page 11: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/11.jpg)
Slide 11
Bitwise Operators
![Page 12: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/12.jpg)
Slide 12
#include<iostream>using namespace std;void main(){ short A =42;short B = 12;int C = A|B;int D = A<<1;cout << "C = "<<C <<endl;cout<< "D = "<<D <<endl;}
Bitwise Operators
The expected output is as under.C = 46D = 84
![Page 13: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/13.jpg)
Slide 13
#include<iostream>using namespace std;void main(){short A =42;short B = 12;short C = 24;short D = A^B; // XOR operatorC <<= 1;A <<=2; // Shift to left by 2 places and assignB >>=2 ; // shift right by 2 places and assigncout<< "A = "<<A<< " \tB = "<< B <<endl;cout << "C = "<<C <<endl;cout << "D = "<< D <<endl;}
The expected output is given below.A = 168 B = 3C = 48D = 38
![Page 14: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/14.jpg)
Operators common to C++
Slide 14
![Page 15: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/15.jpg)
Slide 15
Operator Precedence
In C++, you use operators and expressions such as the following:
int MyNumber = 10 * 30 + 20 – 5 * 5 << 2;
The question is, what value would MyNumber contain? The order in which the various operators are invoked is very strictly specified by the C++ standard. This order is what is meant by operator precedence.
![Page 16: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/16.jpg)
Slide 16
![Page 17: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/17.jpg)
Slide 17
Assignment and Expressions
C++ provides assignment operators that combine an assignment operator and some other operator.
C++ also provides increment (++) and decrement (--) operators in both prefix and postfix form.
![Page 18: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/18.jpg)
Slide 18
Assignment and Expressions
The postfix form behaves differently from the prefix form:
![Page 19: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/19.jpg)
Slide 19
PROGRAMMING WITH VISUAL C++
C++ Relational, Equality, and Logical
![Page 20: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/20.jpg)
Slide 20
PROGRAMMING WITH VISUAL C++
C++ Relational, Equality, and Logical
![Page 21: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/21.jpg)
Slide 21
PROGRAMMING WITH VISUAL C++
C++ Relational, Equality, and Logical
![Page 22: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/22.jpg)
Slide 22
Statements
C++ has a large variety of statement types, including an expression statement. For example, the assignment statement in C++ is syntactically an assignment expression followed by a semicolon.
![Page 23: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/23.jpg)
Slide 23
The Compound Statement
The Compound Statement
• A compound statement in C++ is a series of statements surrounded by braces { and }.
• The body of a C++ function, for example, is always a compound statement.
![Page 24: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/24.jpg)
Conditional ternary operator ( ? )
Slide 24
The conditional operator evaluates an expression, returning first value if that expression evaluates to true, and a different one if the expression evaluates as false. Its syntax is:
condition ? result1 : result2
7==5 ? 4 : 3 // evaluates to 3, since 7 is not equal to 5.7==5+2 ? 4 : 3 // evaluates to 4, since 7 is equal to 5+2.5>3 ? a : b // evaluates to the value of a, since 5 is greater than 3.a>b ? a : b // evaluates to whichever is greater, a or b.
![Page 25: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/25.jpg)
Slide 25
For example:// conditional operator#include <iostream>using namespace std;
int main (){ int a,b,c;
a=2; b=7; c = (a>b) ? a : b;
cout << c << '\n';}
In this example, a was 2, and b was 7, so the expression being evaluated (a>b) was not true, thus the first value specified after the question mark was discarded in favor of the second value (the one after the colon) which was b (with a value of 7).
![Page 26: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/26.jpg)
Slide 26
The if if and if-elseif-else Statements
if and if-else
The general form of an if statement is:
Here is an example of an if statement:
Or another example:
![Page 27: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/27.jpg)
Slide 27
Closely related to the if statement is the if-else statement, which has the general form
if and if-else
![Page 28: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/28.jpg)
Slide 28
If condition is true, then statement1 is executed and statement2 is skipped; if condition is false, then statement1 is skipped and statement2 is executed. After the if-else statement has been executed, control passes to the next statement. Consider the next code:
if and if-else
![Page 29: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/29.jpg)
Slide 29
If x < y is true, then min is assigned the value of x; if x < y is false, min is assigned the value of y. After the if-else statement is executed, min is printed.
if and if-else
![Page 30: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/30.jpg)
Slide 30
The switch StatementThe switch statement is a multiway conditional statement generalizing the if-else statement. The general form of the switch statement is given by
![Page 31: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/31.jpg)
Slide 31
The switch Statementswitch (expression){ case constant1: group-of-statements-1; break; case constant2: group-of-statements-2; break; . . .
default: default-group-of-statements}
![Page 32: 2621008 - C++ 2](https://reader035.vdocuments.us/reader035/viewer/2022081518/54981afdb4795918638b469d/html5/thumbnails/32.jpg)
Slide 32
The switch Statement
Ex: