Professional Documents
Culture Documents
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.
Examples:
{}
{ i=1; }
{;}
{ i=5; k=3 }
Since the else part is optional, expressions containing multiple if and if - else
structures can lead to confusion.
Employed in a rash manner, conditional operators can affect the readability of a program, but if
used with care, they provide very elegant solutions:
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!
<expr> may also be of type char, since individual characters have equivalent integer values.
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.