0% found this document useful (0 votes)
16 views46 pages

04 - Unit 4. Control Statements

Uploaded by

antogf1986
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
16 views46 pages

04 - Unit 4. Control Statements

Uploaded by

antogf1986
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 46

UNIT 4 – CONTROL STATEMENTS

ARTURO S. GARCÍA
UNIVERSITY OF CASTILLA-LA MANCHA PROGRAMMING FUNDAMENTALS 1

DEGREE IN COMPUTER SCIENCE AND ENGINEERING.


2022-2023 ESII ALBACETE
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 IF STATEMENT
 Syntax:
 if (<expression>) <statement>
 Semantics:
 The execution of the statement depends on
the value of a controlling expression. #include <stdio.h>

 Executed if expression compares unequal to 0. void main() {


int number;
 Restrictions:
scanf("%d", &number);
 The controlling expression of an if statement
shall have scalar type. if (number % 2 == 0) {
printf("%d: even.\n", number);
 The value zero (0) means false. }
}
 None-zero means true.
3
THE IF…ELSE STATEMENT

 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 //

int number; If user enters 0. Output?


scanf("%d", &number); > 0: zero.

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

 Prompt the user for the marks of theory and laboratory.


 Tip 1: variables and type
 Tip 2: printf & scanf for obtaining the user input
 Return the arithmetic average only if it’s greater or equal than 5.
Otherwise, identify the failed part.
 Tip 3: The if...else statement
 Tip 4: printf for showing the result
 5 minutes!

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

(theory < 5) ? printf("Only theory is failed.") : printf("Only laboratory is failed.");9


THE ? STATEMENT

 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 //

int i; If user enters 1. Output?


printf("Enter a number between 1 and 3: "); > One
scanf("%d", &i); > Two
> Tree
switch (i) {
case 1: If user enters 2. Output?
printf("One"); > Two
> Tree
case 2:
printf("Two"); If user enters 3. Output?
> Tree
case 3:
printf("Three"); If user enters 7. Output?
} Nothing

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

int i; If user enters 1. Output?


printf("Enter a number between 1 and 3: "); > One
scanf("%d", &i);
If user enters 2. Output?
switch (i) { > Two
case 1:
printf("One\n"); If user enters 3. Output?
break; > Tree
case 2: If user enters 7. Output?
printf("Two\n");
Nothing
break;

case 3:
printf("Three\n");
}
THE DEFAULT LABEL

 Syntax: switch (<expression>) {


case value1 : <statement1>
case value2 : <statement2>

case valueN : <statementN>
default: <statement>
}
 Semantics:
 If no case constant expression matches, control jumps to this label.

20
THE DEFAULT LABEL
// CODE A // // After compiling and running //

int i; If user enters 1. Output?


printf("Enter a number between 1 and 3: "); > One
scanf("%d", &i);
If user enters 2. Output?
switch (i) { > Two
case 1:
printf("One"); If user enters 3. Output?
break; > Tree
case 2:
printf("Two"); If user enters 7. Output?
break;
> default!
case 3:
printf("Three");
break;

default:
printf("default!");
}
EXERCISE

 Make a question that can be answered with yes or no.


 Capture the answer.
 Print 'yes' if the user presses y/Y and 'no' if the user presses n/N.
 Do not be case sensitive! (y/Y or n/N -> just one character!)
 5 minutes!

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

 Prompt the user to enter an integer number.


 Print the sequence between that number and ten.

// CODE A // // After compiling and running //

int i; If user enters 1. Output?


> 1 2 3 4 5 6 7 8 9 10
scanf("%d", &i);
while (i <= 10) { If user enters -3. Output?
printf("%d ", i); > -3 -2 -1 0 1 2 3 4 5 6 7 8 9 10
i = i + 1;
} If user enters 11. Output?
>

25
EXAMPLE 2

 Prompt the user to enter an integer number.


 Determine if this number is a prime number.

26
EXAMPLE 2

// CODE A // // After compiling and running //

int div = 2; If user enters 5. Output?


int num; > It’s a prime number.
scanf("%d", &num);
If user enters 6. Output?
while ((div < num) && ((num % div) != 0)) { > It’s not a prime number. (2)
div++;
}

if (div == num) {
printf("It\’s a prime number.");
}
else {
printf("It\’s not a prime number. (%d)", div);
}

See this one in the compiler and execute it step by step 27


EXAMPLE 2A

 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 //

int div = 2; int div = 1;


int num; int num;
scanf("%d", &num); scanf("%d", &num);

while ((div < num) && ((num % div) != 0)) { while (div < num) {
div++; div++;
} if (num % div == 0)
break;
}

if (div == num) { if (div == num){


printf("It is prime number."); printf("It is a prime number.");
} }
else { else {
printf("It is not a primer number. (%d)", div); printf("It is not a prime number. (%d)", div);
} }

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

// CODE A // // After compiling and running //

int i = 0; If user enters 7. Output?


int num; > 1 2 4 5 7

scanf("%d", &num); If user enters 10. Output?


> 1 2 4 5 7 8 10
while (i < num){
i = i + 1;
if ((i % 3) == 0)
continue;

printf("%d ", i);


}

31
EXAMPLE

// CODE A // // After compiling and running //

int i = 0; If user enters 7. Output?


int num; > 1 2

scanf("%d", &num);

while (i < num){


if ((i % 3) == 0)
continue;

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

char c; If user enters w ?


Loop runs again
do {
printf("Connect to server (Yes/No)? "); If user enters y ?
scanf(" %c", &c); Loop runs again
} while (c!='Y' && c!='N');
If user enters N ?
Loop finishes

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

// CODE // // After compiling and running //

for (int i=0; i<=10; i++) > 0 1 2 3 4 5 6 7 8 9 10


printf("%d ", i);

for (int i=0; i<=10; i++) { > 0 1 2 3 5 6 7 8 9


if (i==4)
continue;

printf("%d ", i);

if (i==9) break;
}

41
THE FOR STATEMENT

 Example

// CODE // // After compiling and running //

int i=0; > 0 1 2 3 4 5 6 7 8 9 10


for (; i<=10; i++)
printf("%d ", i);
> 0 1 2 3 5 6 7 8 9
printf("\n");

for (int i=0; i<=10; i++) {


if (i==4)
continue;

printf("%d ", i);

if (i==9) break;
}
42
THE FOR STATEMENT

 Example

// CODE // // After compiling and running //

int i=0; > 0 1 2 3 4 5 6 7 8 9 10


for (; i<=10;)
{
printf("%d ", i); > 0 1 2 3 5 6 7 8 9
i++;
}
printf("\n");

for (int i=0; i<=10; i++) {


if (i==4)
continue;

printf("%d ", i);

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

for (int i=0; i<=10; i++) {


if (i==4)
continue;

printf("%d ", i);

if (i==9) break; 44
}
THE FOR STATEMENT

 The for statement is semantically equivalent to the while statement.

// CODE // // CODE //

for (<clause-1>;<expression-2>;<expression-3>) { <clause-1>


<statement> while (<expression-2>) {
} <statement>
Equivalent! <expression-3>
}

45
END.

You might also like