C++课件(精华版)概要1.ppt

C课件(精华版)概要1

Switches switch (expression) { case i: //do this break; case j: //do that break; default: //do other thing } Switch example #include iostream using namespace std; int main() { int x = 6; switch(x) { case 1: cout x is 1\n;break; case 2: case 3: cout x is 2 or 3; break; default: cout x is not 1, 2, or 3; } return 0; } //weekday void weekday(int d) { switch (d) { case 7: cout Sunday; break; case 1: cout Monday; break; case 2: cout Tuesday; break; case 3: cout Wednesday; break; case 4: cout Thursday; break; case 5: cout Friday; break; case 6: cout Saturday; break; } return; } For Loops for (initializations; condition; updates) { //do this again and again } for(int i = 0; i 10; i++) { cout i; } * * * * * * * * Think about: int a,b,c=2; int x,y,z,10; int m=2; int n=3; long int sum=0,add; long hello; char a=’m’; char b,c,d; char m=65,n=a+1; float a,b,ccc=3.1415; float sum=0.0; double f1, f2=1.414e12 * Assignment and increment int a = 7; a = 9; a = a+a; a += 2; ++a; * 7 9 18 20 21 a: Think about int a=10, b; b=a; float x; int k=300; x=k; float x=3.14; int n; n=x+6; float x=3.14; int n; n=3; cout x+n; 3.0/9 or (float)3/9 //2.3.cpp A program to illustrate integer overflow #include iostream using namespace std; /** * A program to illustrate what happens when large integers * are multiplied together. */ int main() { int million = 1000000; int trillion = million * million; cout According to this computer, million squared is trillion . endl; } * A type-safety violation (“implicit narrowing”) int main() { int a = 20000; char c = a; int b = c; if (a != b) cout oops!: a != b \n; else cout Wow! We have large characters\n; } * 20000 a ?

文档评论(0)

1亿VIP精品文档

相关文档