C h a p t e r 03
C++ 3 1 9 4 3 break continue 2 110
if if else if else switch 1 if if if 3 1 1 if 2 2 3 if if 1 2 111
01 #include<iostream> 02 using namespace std; 03 void main( ) 04 { 05 int x; 06 07 cout <<? => ; 08 cin >> x; 09 10 if(x < 0) // 11 x = -x; // 12 13 cout << => << x << n ; 14 } 112
2 if if else if esle if if if else 3 2 1 if 2 3 4 113
if~else 01 #include<iostream> 02 using namespace std; 03 void main( ) 04 { 05 int x; 06 07 cout << => ; 08 cin >> x; 09 10 if(x % 2 == 1) // 2 1 11 cout << n ; 12 else 13 cout << n ; 14 } 114
C++ 00 x 2 1 if(x % 2) cout << n ; else cout << n ; if x 2 0 if(x % 2 == 0) x 2 0 NOT 1 x x 2 0 if(! (x % 2) ) NOT 3 if else if if else if else if else 115
if 1 1 if 2 2 2 3 else if 90 A 80 B 70 C 60 D 59 F 116
01 #include<iostream> 02 using namespace std; 03 void main( ) 04 { 05 int score; // 06 char grade; // 07 cout<< : ; 08 cin>>score; 09 // 10 if(score>=90) // score 90? 11 grade= A ; // grade= A 12 else if (score>=80) // score 80 13 grade= B ; // grade= B 14 else if (score>=70) // score 70 15 grade= C ; // grade= C 16 else if (score>=60) // score 60 17 grade= D ; // grade= D 18 else // 19 grade= F ; // grade= F 20 cout<< <<score<< : <<grade<< n ; 21 } 117
a b a b int a=5, b=3; bool istrue = a = b; // bool istrue = a == b; a b istrue 1 0 a b 1 a b b 3 a a 3istrue C++ 0 0 1 100 a 3 bool istrue 3 1 a b 0 false 1 true if 01 #include<iostream> 02 using namespace std; 03 void main( ) 04 { 05 int i=200; 06 cout<< i 300? ; 118
07 if(i==300) 08 cout<< true n ; 09 else 10 cout<< false n ; 11 12 cout<< i 300? ; 13 if(i=300) 14 cout<< true n ; 15 else 16 cout<< false n ; 17 } 4 switch if else if else switch if else switch 119
switch case case case case switch default switch if else 01 #include<iostream> 02 using namespace std; 120
03 void main( ) 04 { 05 int score; // 06 char grade; // 07 cout<< : ; 08 cin>>score; 09 10 switch(score/10) { // 11 case 10 : grade= A ;break; 12 case 9 : grade= A ;break; 13 case 8 : grade= B ;break; 14 case 7 : grade= C ;break; 15 case 6 : grade= D ;break; 16 default : grade= F ; 17 } 18 cout<< <<score<< : <<grade<< n ; 19 } switch break 75 C C F if switch switch case break 121
01 #include<iostream> 02 using namespace std; 03 void main( ) 04 { 05 int score; // 06 char grade; // 07 cout<< : ; 08 cin>>score; 09 10 switch(score/10) { // 11 case 10 : cout<< A <<endl; 12 case 9 : cout<< A <<endl; 13 case 8 : cout<< B <<endl; 14 case 7 : cout<< C <<endl; 15 case 6 : cout<< D <<endl; 16 default : cout<< F <<endl; 17 } 18 } 122
for while do while 1 for for control variable for for 123
1 1 for 1 for 1 10 sum 1 2 3 4 5 6 7 8 9 10 1 10 1 1000 for for(int i=5;i>=1;i--) 124
1 5 for for 1 5 for 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int total=0; // 06 for(int i=1;i<=5;i++) { 07 cout<< i = <<i<<endl; 08 total+=i; // total=total+i; 09 cout<< total = <<total<<endl; 10 } 11 cout<< 1 << i-1 << << total << <<endl; 12 } 125
12 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int total=0; 06 for(int i=1;i<=5;i+=2) { 07 cout<< i = <<i<<endl; 08 total=total+i; 09 cout<< total = <<total<<endl; 10 } 11 cout<< 1 5 << total << <<endl; 12 } for(int i=2;i<=5;i+=2) { 126
1 9 for 1 9 1 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int dan; 06 cout<< : ; 07 cin>>dan; // 08 for(int j=1;j<10;j++) 09 cout<< dan << * <<j<< = << dan*j << n ; 10 } 127
2 for for for 2 3 for loop 128
for 2 9 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 for(int dan=2;dan<=9;dan++) // for 06 for(int j=1;j<10;j++) // for 07 cout << dan << * << j << = << dan*j << n ; 08 } or ( ; ; ){ ; } 129
3 while for while while while { // } // while 130
while for while 1 10 while for 1 10 for int total=0; for(int i=1;i<=10;i++) total+=i; cout<< 1-10 <<total<< n ; for for while for fo for 1 10 while 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int total=0; 06 int i=1; // for 07 while(i<=10) { // for 08 total+=i; 09 i++; // for 10 } 11 cout<< 1-10 <<total<< n ; 12 } 131
for for 2 int i=1; // for for( ;i<=10; ) { // total+=i; i++; // } C++ for while for while 0 132
01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int num; 06 cout<< (0 ) : ; 07 cin>>num; 08 while(num!=0) { 09 cout<< num << n ; 10 cout<< (0 ) : ; 11 cin>>num; 12 } 13 cout<< num << n ; 14 } 4 do while while while do while while do while 133
do while do while while while 3_13.cpp while do while 3_13.cpp 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int num; 06 do{ 07 cout<< (0 ) : ; 08 cin>>num; 09 cout<< num << n ; 134
10 }while(num!=0); 11 cout<< num << n ; 12 } cout<< (0 ) : ; cin>>num; while(num!=0) { cout<< num << n ; } cout<< (0 ) : ; cin>>num; 135
break continue 1 break switch for while do while switch break switch break switch switch for while do while break for while do while while break while 2 while 3 136
1 10 if 2 i 1 10 for 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int total=0; 06 for(int i=1;i<=10;i++) { 07 if(i%2==0) // i 2 08 break; // for 09 total+=i; 10 } 11 cout<< i << i << for n ; 137
12 cout<< total : <<total<< n ; 13 } for while switch break break break { } { break; : } : 2 continue break continue break 138
continue for while do while break switch continue continue break 139
break 3 14 break continue 01 #include <iostream> 02 using namespace std; 03 void main() 04 { 05 int total=0; 06 for(int i=1;i<=10;i++) { 07 if(i%2==0) // i 2 08 continue; // for 09 // 10 total+=i; 11 } 12 cout<< i << i << for n ; 13 cout<< total : <<total<< n ; 14 } 140
1 2 3 if 4 if else 5 switch case break 6 for 7 for while 8 do while 9 break for while do while 10 continue for while do while 141
1 a x 100 200 b ch a A c y 10 20 z 30 2 if else if 0 3 if else 4 for if 1 100 5 for A Z 142
6 for 123410 7 #include<iostream> using namespace std; void main() { int a=0, b=0; do { ++a; b += a; }while( a < 50 ); cout<< a = <<a<< b= <<b<<endl; } 8 #include<iostream> using namespace std; void main() { int n; for(n=100; n>0; n--) if(n%13==0) break; cout<< n===> <<n<<endl ; } 143
9 continue 5 if continue #include<iostream> using namespace std; void main() { int n; for(n=0; n<20; n++){ cout<<n<< t ; } cout<< n ; } 144