(制御文: 条件、枝分れ、繰返し)
http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture3.html
© 2005-17 Martin J. Dürst 青山学院大学
02B1 | 02C1 | 02C2 | |
100 points | 99 | 84 | 63 |
60 points | - | 15 | 35 |
not submitted | - | - | 1 |
Examples of how to check for odd numbers:
a-((a>>1)<<1)==0
(a>>1)<<1 == 1
One more example: a & 1 == 1
Intent: (a&1) == 1
Actual interpretation: a & (1==1)
(see p.
388)
Accidental luck: The result of 1==1
(true) by chance was 1
(this is not guaranteed)
Conclusion 1: Please be careful with the priority of shift and bitwise operations
Conclusion 2: a & 1
is enough
(for even numbers, the result is 0 (false), for odd numbers, the result is 1
(true))
int a, b, c, d, e;
buffer
is better than
buf
)leap_year
is better than uruudoshi
)if
statement:if ( condition )
statement
Grammar consists of fixed
parts (if
、(
、and )
above;
terminals) and replacable parts
(nonterminals)
if
statement:Evaluate the condition
. If the result is true, then
execute the statement
. Otherwise, do not execute the
statement
.
a = 20; if (a < 10) a = 15;
The if
statement is syntactically correct, but semantically
wrong.
Syntactic errors are caught by the compiler. Most semantic errors are not caught by compilers.
式;
(expression statement)if
statement,switch
statement, for
statement, while
statementreturn
statement, break
statement{}
)This will lead to errors:
if (score >= 60)
printf("You got 60 or more points.\n");
printf("You passed the exam.\n");
This is much clearer:
if (score >= 60) {
printf("You got 60 or more points.\n");
printf("You passed the exam.\n");
}
if
and else
if (score >= 60) {
printf("You got more than 60 points.\n");
} else {
printf("See you again next year.\n");
}
if
- else
if
-
else
elseif
or elsif
or
elif
if
- else if
- else
if
- ... - else
as a single structureelse if
/else
are indented at the
same level as the original if
:if (...) {
...
}
else if (...) {
...
}
else {
...
}
if
-
else
if
- else
if (...) {
...
}
else if (...) {
...
}
else if (...) {
...
}
else {
...
}
if
: Ternary Operator(also called conditional operator)
Frequent example:
if (x > y)
max = x;
else
max = y;
Replacement:
max = (x > y) ? x : y;
if
: Conditional ExpressionsUsing &&
or ||
, it is possible to create
replacements for if
statements:
if (score >= 60) {
grade = calculate(score);
}
Replacement:
score>=60 && (grade=calculate(score));
(Caution: not recommended in C!)
Example 1: gcc 03B1.c && ./a
Example 2: gcc -o 03B1 03B1.c && ./03B1
How it works:
It is also possible to connect two different executions in this way.
The result of execution is the value returned from main
(0 if
successful).
Outside a program (i.e. in the (cygwin) shell), 0
is
true and other values are false.
Inside a C program, 0
is false and other
values are true.
gcc 02B1.c && ./a
を推奨)switch
Statementswitch ( expression ) {
case literal1 :
処理1 ;
break;
case literal2:
case literal3:
処理2 ;
... break; default : 処理3 ;
break;
}
switch
文の注意点Without a break
statement, execution continues past case
labels.
int a = 5, b = 1;
switch (a) {
case 4:
b = 3;
break;
case 5:
b = 7;
case 6:
b *= 3;
break;
}
After execution, b
is 21
If break
is purposely omitted, use a comment
break
case
part of a switch
statementif
と switch
の使い分け主な目安:
if
switch
を検討switch
を検討for
Statementfor ( 初期化; 継続条件; 次の一歩 ) {
繰り返す処理;
}
while
Statementwhile ( 継続条件 ) {
繰り返す処理;
}
for
と while
の使い分け主な目安:
for
for
while
do
... while
Statement文法:
do {
繰り返す処理;
} while ( 継続条件 );
(Russian peasant multiplication)
a0, b0: original multiplier and multiplicand (input)
sf: Final total (output)
Goal: sf = a0 · b0
Loop invariant: ai · bi + si = a0 · b0
Induction base:
For i = 0, through initialization, s0 = 0, and
therefore
a0 · b0 + s0
= a0 · b0 + 0 =
a0 · b0
What we have to prove: The invariant holds at all i (0 ≦ i ≦ f)
Inductive step:
Assume the invariant holds at the end of loop pass k 、(and therefore at the start of loop pass k+1)
Assumption: ak · bk + sk = a0 · b0
Ifak is even: a0 · b0 = ak · bk + sk = ak/2 · bk·2 + sk = ak+1 · bk+1 + sk+1 = a0 · b0 (/ is integer division)
If ak is odd: a0 · b0 = ak · bk + sk = ak/2 · bk·2 + bk + sk = ak+1 · bk+1 + sk+1 = a0 · b0
Termination condition: ai = 0
Because ai+1 = ai / 2, ai is reduced to half in each loop pass, and will reach 0 eventually.
Result check:
When ai = 0, a0 · b0 = ai · bi + si = 0 · bi + si = 0 + si = si
Therefore, when ai = 0, si is the correct result.
switch
を使用)Example display: