1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
| switch (chChar) { case '+' : DoAddition(x, y); break ; case '-' : DoSubtraction(x, y); break ; case '*' : DoMultiplication(x, y); break ; case '/' : DoDivision(x, y); break ; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
| #include <cstdio> // for getchar() #include <iostream> using namespace std; int main() { // count how many spaces the user has entered int nSpaceCount = 0; // loop 80 times for ( int nCount=0; nCount < 80; nCount++) { char chChar = getchar (); // read a char from user // exit loop if user hits enter if (chChar == '\n' ) break ; // increment count if user entered a space if (chChar == ' ' ) nSpaceCount++; } cout << "You typed " << nSpaceCount << " spaces" << endl; return 0; } |
1
2
3
4
5
6
| while (1) { char chChar = getchar (); if (chChar == '\n' ) break ; } |
1
2
3
4
5
6
7
8
| for ( int iii=0; iii < 20; iii++) { // if the number is divisible by 4, skip this iteration if ((iii % 4) == 0) continue ; cout << iii << endl; } |
1
2
3
4
5
6
7
8
| int iii=0; while (iii < 10) { if (iii==5) continue ; cout << iii << " " ; iii++; } |
1
2
3
4
5
6
7
8
9
10
11
12
13
| int nPrinted = 0; for ( int iii=0; iii < 100; iii++) { // messy! if ((iii % 3) && (iii % 4)) { cout << iii << endl; nPrinted++; } } cout << nPrinted << " numbers were found" << endl; |
1
2
3
4
5
6
7
8
9
10
11
12
13
| int nPrinted = 0; for ( int iii=0; iii < 100; iii++) { // if the number is divisible by 3 or 4, skip this iteration if ((iii % 3)==0 || (iii % 4)==0) continue ; cout << iii << endl; nPrinted++; } cout << nPrinted << " numbers were found" << endl; |
0 comments: