04 - Unit 4. Control Statements
04 - Unit 4. Control Statements
ARTURO S. GARCÍA
UNIVERSITY OF CASTILLA-LA MANCHA PROGRAMMING FUNDAMENTALS 1
SELECTION STATEMENTS
• A. The if statement
• B. The switch statement
ITERATION STATEMENTS
• A. The while statement
• B. The do statement
• C. The for statement
THE IF STATEMENT
Syntax:
if (<expression>) <statement>
Semantics:
The execution of the statement depends on
the value of a controlling expression. #include <stdio.h>
Syntax:
if (<expression>) <statement1>
else <statement2>
#include <stdio.h>
Semantics: void main() {
int number;
statement2 executed if the expression
scanf("%d", &number);
compares equal to 0 (i.e., false).
if (number % 2 == 0)
An else is associated with the lexically printf("%d: even.\n", number);
else
nearest preceding if that is allowed by the }
printf("%d: odd.\n", number);
syntax.
4
THE IF...ELSE IF SUBSTATEMENT
// CODE A // // CODE B //
if (<expression1>) { if (<expression1>) {
<statement1> <statement1>
} }
else { else if (<expression2>) {
if (<expression2>) { <statement2>
<statement2> }
} else if (<expression3>) {
else { <statement3>
if (<expression3>) { Equivalent! }
<statement3> else {
} <statement4>
else { }
<statement4>
}
}
}
5
THE IF...ELSE IF SUBSTATEMENT
// CODE A // // CODE D //
if (<expression1>) { if (<expression1>) {
<statement1> <statement1>
} }
else { else if (<expression2>) {
if (<expression2>) { <statement2>
<statement2> }
} else if (<expression3>) {
else { <statement3>
if (<expression3>) { Not Equivalent! }
<statement3> else {
} <statement4>
else { }
<statement4>
}
}
}
6
THE IF...ELSE IF SUBSTATEMENT
// CODE E // // After compiling and running //
if (number == 0) {
printf("%d: zero.", number); If user enters 12. Output?
} > 12: even.
else {
if (number % 2 == 0) {
printf("%d: even.", number); Result If user enters 13. Output?
} > 13: odd.
else {
printf("%d: odd.", number);
}
}
7
EXERCISE
8
THE ? STATEMENT
Syntax:
<expression> ? <statement1> : <statement2>
Semantics:
if expression is true, then statement1 is executed.
Otherwise, the statement2 is executed.
Example:
if (theory < 5)
printf("Only theory is failed.");
else
printf("Only laboratory is failed.");
Syntax:
<expression> ? <statement1> : <statement2>
Semantics:
if expression is true, then statement1 is executed.
Otherwise, the statement2 is executed.
Exercise:
Given two integer numbers, identify the lowest one.
5 minutes!
10
CONTENTS
SELECTION STATEMENTS
• A. The if statement
• B.The switch statement
ITERATION STATEMENTS
• A. The while statement
• B. The do statement
• C. The for statement
THE SWITCH STATEMENT
Syntax:
switch (<expression>) {
case value1 : <statement1>
case value2 : <statement2>
…
case valueN : <statementN>
}
Semantics:
Control jumps into the case statement, whose value is logically equal to
the controlling expression (let’s consider “expression == value” for each
case ).
12
THE SWITCH STATEMENT
Restrictions:
Values are constant expressions and not duplicated in the enclosing switch
statement.
Constant expressions of char type can be represented either with the
corresponding ASCII code or character. For example,
case 65: …
case 'A': …
Variables are not allowed as case values.
Cases can be grouped when their corresponding case bodies share the
same statements.
13
THE SWITCH STATEMENT
// CODE A // // CODE B //
int A; int A;
switch (A) { if (A == 1) {
case 1: <statement1> <statement1>
case 2: <statement2> }
case 3: <statement3> else if (A == 2) {
case 4: <statement4> <statement2>
} }
Not Equivalent! else if (A == 3) {
<statement3>
}
else if (A == 4) {
<statement4>
}
14
THE SWITCH STATEMENT
// CODE A // // CODE C //
int A; int A;
switch (A) { if (A == 1) {
<statement1>
case 1: <statement1> <statement2>
case 2: <statement2> <statement3>
case 3: <statement3> <statement4>
case 4: <statement4> }
} else if (A == 2) {
Equivalent! <statement2>
<statement3>
<statement4>
}
else if (A == 3) {
<statement3>
<statement4>
}
else if (A == 4) {
<statement4>
}
15
THE SWITCH STATEMENT
// CODE A // // CODE D //
int A; int A;
switch (A) { if (A == 1) {
<statement1>
case 1: <statement1> <statement2>
case 2: <statement2> <statement3>
case 3: <statement3> <statement4>
case 4: <statement4> }
} if (A == 2) {
Not Equivalent! <statement2>
<statement3>
<statement4>
}
if (A == 3) {
<statement3>
<statement4>
}
if (A == 4) {
<statement4>
}
16
THE SWITCH STATEMENT
// CODE A // // After compiling and running //
17
THE BREAK STATEMENT
Syntax:
break
Semantics:
Control directly jumps to the end of the switch body.
It belongs to the category of jump statements (i.e., goto, continue,
break, and return).
18
THE BREAK STATEMENT
// CODE A // // After compiling and running //
case 3:
printf("Three\n");
}
THE DEFAULT LABEL
20
THE DEFAULT LABEL
// CODE A // // After compiling and running //
default:
printf("default!");
}
EXERCISE
22
CONTENTS
SELECTION STATEMENTS
• A. The if statement
• B. The switch statement
ITERATION STATEMENTS
• A.The while statement
• B. The do statement
• C. The for statement
THE WHILE STATEMENT
Syntax:
while (<expression>) {
<statement>
}
Semantics:
The statement (aka, loop body) is executed repeatedly until the controlling
expression compares to zero (i.e., false).
The evaluation of the controlling expression takes place before each
execution of the loop body.
24
EXAMPLE 1
25
EXAMPLE 2
26
EXAMPLE 2
if (div == num) {
printf("It\’s a prime number.");
}
else {
printf("It\’s not a prime number. (%d)", div);
}
Take Example 2
Use the break statement.
Break can be used in a while statement as well.
28
EXAMPLE 2A - SOLUTION
// CODE A // // CODE B //
while ((div < num) && ((num % div) != 0)) { while (div < num) {
div++; div++;
} if (num % div == 0)
break;
}
29
THE CONTINUE STATEMENT
Syntax:
continue
Semantics:
It causes a jump to the loop-continuation portion of the smallest enclosing
iteration statement; that is, to the end of the loop body.
Example:
Print the sequence of numbers between one and a given number.
Omit the numbers that are multiple of three.
30
EXAMPLE
31
EXAMPLE
scanf("%d", &num);
i = i + 1;
printf("%d ", i);
}
32
CONTENTS
SELECTION STATEMENTS
• A. The if statement
• B. The switch statement
ITERATION STATEMENTS
• A. The while statement
• B.The do statement
• C. The for statement
THE DO-WHILE STATEMENT
Syntaxis:
do {
<statement>
} while (<expression>);
Semantics:
The evaluation of the controlling expression takes place after each
execution of the loop body.
34
EXAMPLE
Prompt the user for entering characters until the character is either
'Y' or 'N’.
Be case sensitive (only upper case).
35
EXAMPLE
// CODE // // After compiling and running //
If user enters Y ?
Loop finishes
36
EXERCISE
Prompt the user for entering characters until the character is 'Y' ,'y'
or 'N', 'n'.
Do not be case sensitive!
5 minutes!
37
CONTENTS
SELECTION STATEMENTS
• A. The if statement
• B. The switch statement
ITERATION STATEMENTS
• A. The while statement
• B. The do statement
• C.The for statement
THE FOR STATEMENT
Syntax:
for (<clause-1> ; <expression-2> ; <expression-3>)
<statement>
Semantics:
clause-1: If it’s a declaration, the scope of any identifiers it declares is the
remainder of the declaration and the entire loop, including the other two
expressions; it is reached in the order of execution before the first
evaluation of the controlling expression.
If clause-1 is an expression, it is evaluated as a void expression before the
first evaluation of the controlling expression.
39
THE FOR STATEMENT
Semantics+
expression-2: controlling expression that is evaluated before each
execution of the loop body.
expression-3: evaluated as a void expression after each execution of the
loop body.
clause-1, expression-2 and expression-3 can be omitted.
If expression-2 is false, the statement is not executed.
The break and continue statements can be used.
40
THE FOR STATEMENT
Example
if (i==9) break;
}
41
THE FOR STATEMENT
Example
if (i==9) break;
}
42
THE FOR STATEMENT
Example
if (i==9) break;
43
}
THE FOR STATEMENT
Example
// CODE //
// After compiling and running //
int i=0;
for (;;)
> 0 1 2 3 4 5 6 7 8 9 10
{
printf("%d ", i);
i++;
> 0 1 2 3 5 6 7 8 9
if (i>10)
break;
}
printf("\n");
if (i==9) break; 44
}
THE FOR STATEMENT
// CODE // // CODE //
45
END.