Simple Data Types and Statements Namespaces namespace My

  • Slides: 17
Download presentation
Simple Data Types and Statements

Simple Data Types and Statements

Namespaces namespace My. Namespace { // …. { My. Namespace: : func 1() using

Namespaces namespace My. Namespace { // …. { My. Namespace: : func 1() using namespace Other. Namespace; Comments: // /* xxxx */

Basic Data Types • • • Type Range Literals/constants and variables Operators and expressions

Basic Data Types • • • Type Range Literals/constants and variables Operators and expressions Assignment Input and output

Primitive Data Types in C++/CLI • Integer: – Int 16 (short); UInt 16 (unsinged

Primitive Data Types in C++/CLI • Integer: – Int 16 (short); UInt 16 (unsinged short) – Int 32 (int or long); UInt 32(unsigned int or long) – Int 64 (long); UInt 64(unsigned long, _int 64) int x, y(1), z = 123; //(signed, 32 bits) Console: : Write. Line(0 x 10); Console: : Write. Line(-0 x 10); // negative hex 10 is base-10 -16 • Floating: – Single (float, 32 bits) – Double (double, 64 bits)

Primitive Data Types in C++/CLI • Boolean (bool, true or false) • Character (Char,

Primitive Data Types in C++/CLI • Boolean (bool, true or false) • Character (Char, 16 bits, unsigned unicode) – Character and escape sequence char a = ‘A’; // character ‘A’ Char b = L‘A’; // unicode ‘A’ Char c = L‘x 0041’; // hex 41 is ASCII ‘A’ char d = ‘t’; // tab escape Char e = L‘\’; // unicode backslash escape

Arithmetic Operators • • • + addition - subtraction * multiplication / division %

Arithmetic Operators • • • + addition - subtraction * multiplication / division % remainder or modulus

Arithmetic Expression(informal and Inexhaustible) • A constant is an expression. • A variable is

Arithmetic Expression(informal and Inexhaustible) • A constant is an expression. • A variable is an expression. • If P and Q are expressions, and ☺ is a binary operator, then P☺Q is an expression. An expression has a type too.

Unary Operators • • +, plus -, minus ++, increment --, decrement

Unary Operators • • +, plus -, minus ++, increment --, decrement

Relational Operators • • • == equal to != not equal to > greater

Relational Operators • • • == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to

Bitwise and Shift Operators • • • ~, unary bitwise complement &, bitwise AND

Bitwise and Shift Operators • • • ~, unary bitwise complement &, bitwise AND |, bitwise OR ^, bitwise exclusive OR <<, shift left >>, shift right

Logical Operators • &&, AND • ||, OR • !, NOT

Logical Operators • &&, AND • ||, OR • !, NOT

More examples: • ++d, d++: (++d –e) be ++d then - e (d++ -

More examples: • ++d, d++: (++d –e) be ++d then - e (d++ - e) be: d-b then d++ int a = 2; int b = 3; int c = (b++, a = b++ * a++, a%b); Be: b++, a*b, b++, a%b • Bitwise 0101 & 0011 be 0001 0101 | 0011 be 0111 0101 ^ 0011 be 0110 ~0101 be 1010 00101100 >> 2 be 00001011 (two positions, /4) 00101100 << 1 be 01011000 (one, *2) • a = a+5; b = b*2; be a += 5; b *= 2;

Expressions and Assignments • • • y=a*x*x+b*x+c; y=(a*x+b)*x+c; n=7/3; n=7%3; z=x+y/2; z=(x+y)/2; i=i+1; i

Expressions and Assignments • • • y=a*x*x+b*x+c; y=(a*x+b)*x+c; n=7/3; n=7%3; z=x+y/2; z=(x+y)/2; i=i+1; i += 1; i++;

More Expressions • • • x>=0 x+y > 3*z+5 x>=0 && x<=100 x<0 ||

More Expressions • • • x>=0 x+y > 3*z+5 x>=0 && x<=100 x<0 || x>100 b. Found != Found;

Ternary Operator • X? Y: Z • (grade>=60)? 1: 0

Ternary Operator • X? Y: Z • (grade>=60)? 1: 0

Examples • Quadratic Equation: ax 2+bx+c=0 • Discriminant: ∆=b 2 -4 ac • Two

Examples • Quadratic Equation: ax 2+bx+c=0 • Discriminant: ∆=b 2 -4 ac • Two roots: x 1= x 2= Math class: System: : Math: : Sqrt(discriminant)