|
| 1 | +Tutorial |
| 2 | +-------- |
| 3 | + |
| 4 | +### 决策 |
| 5 | + |
| 6 | +生活中我们必须做出决定。要根据自己的需要来做出决定,程序也是如此。 |
| 7 | + |
| 8 | +下面是C语言中决策结构的一般形式。 |
| 9 | + |
| 10 | + |
| 11 | + int target = 10; |
| 12 | + if (target == 10) { |
| 13 | + printf("Target is equal to 10"); |
| 14 | + } |
| 15 | + |
| 16 | + |
| 17 | +### `if`语句 |
| 18 | + |
| 19 | +`if`语句检查一个表达式是`真`还是`假`,并根据结果执行不同的代码。 |
| 20 | + |
| 21 | +使用了`==`运算符判断两个变量是否相等,就像上面的例子一样。 |
| 22 | + |
| 23 | +也可以用不等式运算符来判断。例如—— |
| 24 | + |
| 25 | + int foo = 1; |
| 26 | + int bar = 2; |
| 27 | + |
| 28 | + if (foo < bar) { |
| 29 | + printf("foo is smaller than bar."); |
| 30 | + } |
| 31 | + |
| 32 | + if (foo > bar) { |
| 33 | + printf("foo is greater than bar."); |
| 34 | + } |
| 35 | + |
| 36 | +使用`else`关键字在表达式为`false`时退出代码。 |
| 37 | + |
| 38 | + int foo = 1; |
| 39 | + int bar = 2; |
| 40 | + |
| 41 | + if (foo < bar) { |
| 42 | + printf("foo is smaller than bar."); |
| 43 | + } else { |
| 44 | + printf("foo is greater than bar."); |
| 45 | + } |
| 46 | + |
| 47 | +有两个以上的结果可供选择的情况下,可以将多个`if` `else`语句`连锁`。 |
| 48 | + |
| 49 | + int foo = 1; |
| 50 | + int bar = 2; |
| 51 | + |
| 52 | + if (foo < bar) { |
| 53 | + printf("foo is smaller than bar."); |
| 54 | + } else if (foo == bar) { |
| 55 | + printf("foo is equal to bar."); |
| 56 | + } else { |
| 57 | + printf("foo is greater than bar."); |
| 58 | + } |
| 59 | + |
| 60 | +也可以嵌套`if` `else`语句。 |
| 61 | + |
| 62 | + int peanuts_eaten = 22; |
| 63 | + int peanuts_in_jar = 100; |
| 64 | + int max_peanut_limit = 50; |
| 65 | + |
| 66 | + if (peanuts_in_jar > 80) { |
| 67 | + if (peanuts_eaten < max_peanut_limit) { |
| 68 | + printf("Take as many peanuts as you want!\n"); |
| 69 | + } |
| 70 | + } else { |
| 71 | + if (peanuts_eaten > peanuts_in_jar) { |
| 72 | + printf("You can't have anymore peanuts!\n"); |
| 73 | + } |
| 74 | + else { |
| 75 | + printf("Alright, just one more peanut.\n"); |
| 76 | + } |
| 77 | + } |
| 78 | + |
| 79 | + |
| 80 | +两个或多个表达式可以使用逻辑运算符检查是否都为`真`,或是否其中一个为`真`。使用AND运算符`&&`检查两个表达式是否都为`真`。使用OR运算符`||`检查是否至少有一个表达式为`真`。 |
| 81 | + |
| 82 | + int foo = 1; |
| 83 | + int bar = 2; |
| 84 | + int moo = 3; |
| 85 | + |
| 86 | + if (foo < bar && moo > bar) { |
| 87 | + printf("foo is smaller than bar AND moo is larger than bar."); |
| 88 | + } |
| 89 | + |
| 90 | + if (foo < bar || moo > bar) { |
| 91 | + printf("foo is smaller than bar OR moo is larger than bar."); |
| 92 | + } |
| 93 | + |
| 94 | +NOT运算符`!`类似: |
| 95 | + |
| 96 | + int target = 9; |
| 97 | + if (target != 10) { |
| 98 | + printf("Target is not equal to 10"); |
| 99 | + } |
| 100 | + |
| 101 | + |
| 102 | +Exercise |
| 103 | +-------- |
| 104 | + |
| 105 | +练习,在`guessNumber`函数中构建一个`if`语句,检查数字`guess`是否等于555。如果是,在该函数内用`printf`打印出 "正确。你猜对了!"。如果`guess`小于555,用`printf`打印出 "你猜的低了"。如果`guess`大于555,用`printf`打印出 "你猜的高了"。 |
| 106 | + |
| 107 | +* **重要**:不要忘记在要打印的字符串末尾添加一个换行符`\n`。 |
| 108 | + |
| 109 | +Tutorial Code |
| 110 | +------------- |
| 111 | + |
| 112 | + #include <stdio.h> |
| 113 | + |
| 114 | + void guessNumber(int guess) { |
| 115 | + // TODO:在这里写代码 |
| 116 | + } |
| 117 | + |
| 118 | + int main() { |
| 119 | + guessNumber(500); |
| 120 | + guessNumber(600); |
| 121 | + guessNumber(555); |
| 122 | + } |
| 123 | + |
| 124 | +Expected Output |
| 125 | +--------------- |
| 126 | + |
| 127 | +Your guess is too low. |
| 128 | +Your guess is too high. |
| 129 | +Correct. You guessed it! |
| 130 | + |
| 131 | +Solution |
| 132 | +-------- |
| 133 | + |
| 134 | + #include <stdio.h> |
| 135 | + |
| 136 | + void guessNumber(int guess) { |
| 137 | + // TODO:在这里写代码 |
| 138 | + if (guess < 555) { |
| 139 | + printf("Your guess is too low.\n"); |
| 140 | + } else if (guess > 555) { |
| 141 | + printf("Your guess is too high.\n"); |
| 142 | + } else { |
| 143 | + printf("Correct. You guessed it!\n"); |
| 144 | + } |
| 145 | + } |
| 146 | + |
| 147 | + int main() { |
| 148 | + guessNumber(500); |
| 149 | + guessNumber(600); |
| 150 | + guessNumber(555); |
| 151 | + } |
0 commit comments