170 likes | 191 Views
Simple Data Types and Statements. Namespaces. namespace MyNamespace { // …. { MyNamespace :: func1() using namespace OtherNamespace; Comments: // /* xxxx */. Basic Data Types. Type Range Literals/constants and variables Operators and expressions Assignment Input and output.
E N D
Namespaces namespaceMyNamespace { // …. { MyNamespace::func1() using namespace OtherNamespace; Comments: // /* xxxx */
Basic Data Types • Type • Range • Literals/constants and variables • Operators and expressions • Assignment • Input and output
Primitive Data Types in C++/CLI • Integer: • Int16 (short); UInt16 (unsinged short) • Int32 (int or long); UInt32(unsigned int or long) • Int64 (long long); UInt64(unsigned long long, _int64) int x, y(1), z = 123; (signed, 32 bits) int x = y = z = 200; Console::WriteLine(0x10); Console::WriteLine(-0x10); // negative hex 10 is base-10 -16 • Floating: • Single (float, 32bits) • Double (double, 64bits)
Primitive Data Types in C++/CLI • Boolean (bool, true or false) • Character (Char, 16bits, unsigned unicode) • Character and escape sequence chara = ‘A’; // character ‘A’ Charb = L‘A’; // unicode ‘A’ Char c = L‘\x0041’; // hex 41 is ASCII ‘A’ chard = ‘\t’; // tab escape Char e = L‘\\’; // unicode backslash escape
Arithmetic Operators • + addition • - subtraction • * multiplication • / division • % remainder or modulus
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
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 OR • ^, bitwise exclusive OR • <<, shift left • >>, shift right
Logical Operators • &&, AND • ||, OR • !, NOT
Precedence and Associativity • ++, --, unary minus, !, ~ • *, /, % • +, - • << , >> • <, <=, >, >= • ==, != • & • ^ • | • && • || • ?: • =, +=, …
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++, a%b • Bitwise 0101 & 0011 be 0001 0101 | 0011 be 0111 0101 ^ 0011 be 0110 ~0101 be 1010 00101100 >> 00001011 (twice /2) 00101100 <<10110000 (twice *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 += 1; • i++;
More Expressions • x>=0 • x+y > 3*z+5 • x>=0 && x<=100 • x<0 || x>100 • bFound != Found;
Ternary Operator • X?Y:Z • (grade>=60)?1:0
Examples • Quadratic Equation: ax2+bx+c=0 • Discriminant: ∆=b2-4ac • Two roots: x1= x2=