Control Structures: Conditions, Conditionals, and Loops

(制御文: 条件、枝分れ、繰返し)

Computing Practice I

3rd lecture, April 27, 2017

http://www.sw.it.aoyama.ac.jp/2017/CP1/lecture3.html

Martin J. Dürst

AGU

© 2005-17 Martin J. Dürst 青山学院大学

Today's Schedule

 

ミニテスト

 

The Origin of the C Programming Language

 

システムについて

 

Exercise Results

02B1 02C1 02C2
100 points 99 84 63
60 points - 15 35
not submitted - - 1

 

About Last Week's Exercises

 

Operator Priority and How to Check Odd Numbers

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))

 

How to Select Identifiers

 

Syntax (Grammar) and Semantics

Syntax of if statement:

if ( condition )
statement

Grammar consists of fixed parts (if(、and ) above; terminals) and replacable parts (nonterminals)

Semantics of if statement:

Evaluate the condition. If the result is true, then execute the statement. Otherwise, do not execute the statement.

 

Syntax and Semantics: Example

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.

 

Different Kinds of Statements

 

Preferably Use Curly Braces

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");
}

 

Combining if and else

if (score >= 60) {
printf("You got more than 60 points.\n");
} else {
printf("See you again next year.\n");
}

 

How to Write if - else if - else

if (...) {
...
}
else if (...) {
...
}
else {
...
}

 

A Bad Way to Write if - else if - else

if (...) {
...
}
else if (...) {
...
}
else if (...) {
...
}
else {
...
}

 

Replacement for if: Ternary Operator

(also called conditional operator)

Frequent example:

if (x > y)
max = x;
else
max = y;

Replacement:

max = (x > y) ? x : y;

 

Replacement for if: Conditional Expressions

Using && 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!)

 

Conditional Expressions in Cygwin

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.

 

Program Conversion

 

Syntax of the switch Statement

switch ( 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

 

Usages of break

 

ifswitch の使い分け

主な目安:

 

Syntax of the for Statement

for ( 初期化; 継続条件; 次の一歩 ) {
繰り返す処理;
}

 

Syntax of the while Statement

while ( 継続条件 ) {
繰り返す処理;
}

 

forwhile の使い分け

主な目安:

 

The do ... while Statement

文法:

do {
繰り返す処理;
} while ( 継続条件 );

 

Loop Invariant

  

Example of Loop Invariant

(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

 

Loop Invariant: Inductive Step

What we have to prove: The invariant holds at all i (0 ≦ if)

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

 

Proving Loop Termination

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.

 

今週の演習問題

 

Partial Points

Example display:

...
Your program only passed 17 out of 20 tests.
You received 72.0 out of 100 points.

 

次回の準備

 

Glossary

higher level (programming) language
高級プログラム言語
portability
移植性
operating system
オペレーティングシステム
grammar
文法
syntax
構文
semantics
意味(論)
syntactical
構文的
semantical
意味的
terminals (terminal symbols)
終端記号
nonterminals (nonterminal symbols)
非終端記号
expression statement
式文
expression
(curly) brace
波括弧 (中括弧)
ternary operator
三項演算子
conditional operator
条件演算子
(loop) invariant
(繰返しの) 不変条件
identifier
識別子、名前
romanized
ローマ字化、ローマ字での
intent
意図
guaranteed
保証されている
statement
expression statement
式文
compound statement
複文
loop invariant
繰り返しの不変条件