You are on page 1of 36

ALTERNATIVE

STRUCTURE
ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 1
INTRODUCTION
The control structures define the sequence in which the instructions are performed.
In this chapter, we will see how the known selection instructions work in C and we
will get to know a special pair of operators that allows us to choose between two
values inside an expression.

Let us already note that the most important feature of control instructions in C is that
the 'conditions' in C can be any expressions that provide a numerical result.
The value zero corresponds to the false logical value and any value other than zero is
considered true.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 2


< BLOCK OF INSTRUCTIONS >
a block of instructions is enclosed in braces containing 0 or more instructions
(terminated by ;)

Examples:
{} 
{ i=1; } 
{;} 
{ i=5; k=3 } 

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 3


IF - ELSE
if (<expression>)
<block of instructions 1>
else
<block of instructions 2>
If the <expression> provides a value other than zero, then <block of instructions 1> is
executed
If the <expression> provides the value zero, then < block of instructions 2 > is
executed
The <expression> part can designate:
 a variable of a numeric type,
 an expression providing a numerical result.

The < block of instructions > part can refer to:


 a block of instructions enclosed in braces,
 a single statement terminated by a semicolon.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 4


EXAMPLES

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 5


EXAMPLES

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 6


EXAMPLES

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 7


EXAMPLES

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 8


The else part is optional. We can use if as follows:
if (<expression>)
<block of instructions>

Since the else part is optional, expressions containing multiple if and if - else
structures can lead to confusion.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 9


In C an else part is always linked to the last if which has no else part.
In our example, C would use the first interpretation.

To avoid confusion and to force some interpretation of an expression, it is


recommended to use braces {}.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 10


EXAMPLE
2 different
codes !!

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 11


EXERCISE
Consider the following sequence of instructions:

Rewrite the sequence of instructions using tabulators to mark the if - else


blocks belonging together.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 12


EXERCISE
Consider the following sequence of instructions:

Determine the program responses for each of the following pairs of


numbers.
 A = 10 and B = 5
 A = 5 and B = 5
 A = 5 and B = 10
 A = 10 and B = 10
 A = 20 and B = 10
 A = 20 and B = 20 ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 13
IF - ELSE IF - ... - ELSE
By combining multiple if - else structures into an expression we get a structure that is very
common to make decisions between several alternatives:
if ( <expr1> )
<bloc1>
else if (<expr2>)
<bloc2>
else if (<expr3>)
<bloc3>
else if (<exprN>)
<blocN>
else <blocN+1>
Expressions <expr1> ... <exprN> are evaluated from top to bottom until one of them is
different from zero. The instruction block linked to it is then executed and the processing of
the command is completed.
ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 14
EXAMPLE

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 15


EXAMPLE
The last part else deals with the case where none of the conditions has been
fulfilled. It is optional, but it can be used very comfortably to detect errors.
...
printf("Continue (Y)es / (N)o ?");
getchar(C);
if (C == 'Y')
{
...
}
else if (C == 'N')
printf("Bye Bye ...\n");
else
printf("\aError !\n");
...

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 16


EXERCISE
Consider the following sequence of instructions:

Rewrite the sequence of instructions using tabulators to mark the if - else


blocks belonging together.
For which values of A and B do we obtain the results:
first choice, second choice, ... on the screen?
For which values of A and B do we not get an answer on the screen?
ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 17
CONDITIONAL OPERATORS
The C language has a somewhat exotic pair of operators that can be used as an
alternative to if - else and that has the advantage of being integrated into an
expression:
<expr1> ? <expr2> : <expr3>
If <expr1> provides a value other than zero, then the value of <expr2> is provided
as a result
If <expr1> provides the value zero, then the value of <expr3> is provided as a
result

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 18


EXAMPLE
if (A>B)
MAX = A;
MAX = (A > B) ? A : B;
else
MAX = B;

Employed in a rash manner, conditional operators can affect the readability of a program, but if
used with care, they provide very elegant solutions:

printf("You have %i book%c \n", N, (N == 1) ? ' ' : 's');

Type conversion rules also apply to conditional operators? : Thus, for an integer N of the type int and a
rational F of the float type, the expression
(N>0) ? N : F
will always provide a float result, no matter if N is larger or smaller than zero!

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 19


SWITCH

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 20


SWITCH
Give the output for n=1

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 21


SWITCH
Give the output for n=4

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 22


SWITCH
Give the output for n=25

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 23


SWITCH
switch ( <expr> )
{
case constant_1: [<bloc1>]
case constant_2: [<bloc2>]

case constant_n: [<blocN>]
[default: <blocN+1>]
}

Code between [] is optional

<expr> may also be of type char, since individual characters have equivalent integer values.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 24


ATTENTION
Cases should be integers constants (including characters) but not of rational types.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 25


ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 26
SWITCH
The switch statement causes a particular group of statements to be chosen from
several available groups.
The selection is based upon the current value of an expression which is included
within the switch statement.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 27


THE BREAK STATEMENT
The break statement is used to terminate loops or to exit from a switch. It can be
used within a for, while, do -while, or switch statement.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 28


EXERCISE
Write a program that reads an integer value x and displays whether x is an even or an
odd number.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 29


EXERCISE
Write a program that reads three integer values (A, B, and C) from the keyboard and
displays the larger of the three values, using:
a) if - else and a MAX help variable
b) if - else if - ... - else without help variable
c) conditional operators and a MAX help variable
d) conditional operators without any help variable

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 30


EXERCISE
Write a program that reads three integer values (A, B and C) on the keyboard. Sort
the values A, B and C by successive exchanges so as to obtain: val (A) <= val (B) <=
val (C).

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 31


EXERCISE
Write a program that reads two integer values (A and B) from the keyboard and
displays the product sign of A and B without performing the multiplication.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 32


EXERCISE
Write a program that reads two integer values (A and B) from the keyboard and
displays the sign of the sum of A and B without performing the addition.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 33


EXERCISE
Write a program that calculates the real solutions of a second degree equation
ax2+bx+c = 0 by discussing the formula:

Use a help variable D for the discriminant value b 2-4ac and decide using D if the
equation has one, two or no real solution. Use variables of type int for A, B and C.
Consider also the cases where the user enters null values for A; for A and B; for A,
B, and C. View the results and the necessary messages on the screen.

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 34


EXERCISE
Write a program that reads a real number X and displays its category:
 Category A if (0 < = X < 37)
 Category B otherwise

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 35


EXERCISE
A four-digit number ABCD is called Lucky if A+B=C+D.
Write a program that asks the user to enter a four-digit number n and prints whether
n is a Lucky number or not.
Example:
 The number 3719 is a Lucky number since 3 + 7 = 1 + 9
 The number 3521 is not a Lucky number since (3 + 5) != (2 + 1).

ALGORITHM & IMPERATIVE PROGRAMMING (I1101) 2019-2020 36

You might also like