0% found this document useful (0 votes)
21 views

CHAPTER 2 C Programing

The document discusses different types of control structures in C including if-else statements, conditional expressions, switch statements, while loops, do-while loops, for loops, and break and continue statements. It provides examples and explanations of how each structure works.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

CHAPTER 2 C Programing

The document discusses different types of control structures in C including if-else statements, conditional expressions, switch statements, while loops, do-while loops, for loops, and break and continue statements. It provides examples and explanations of how each structure works.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 4

CHAPTER 2 : BRANCHING AND CONTROL

LOOPS
The C language provides three typ es of decision-making c onstructs: if-else, the conditional
expression ?: , and the switch statement. It also provides three looping constructs: while, do-
while,
and for. And it has the infamous goto, which is capable of both non-conditional branching and
looping.
I- I f-Else
The basic if statement tests a conditional expression and, if it is non-zero (i.e.,
TRUE), executes
the subsequent statement. For example, in this code segment
if (a < b)
b = a;
the assignment b = a will only occur if a is less-than b. The else statement deals
with the alternative
case where the conditional expression is 0 (i.e., FALSE).
if (a < b)
b = a;
else
b += 7;
The if-else statement can also command multiple statements by wrapping them in
braces. Statements so grouped are called a compound statement, or block, and they
are syntactically equivalent
to a single statement.
if (a < b) {
b = a;
a *= 2;
}
else {
b += 7;
--a;
}
It is possible to chain if-else statements if the following form
if (expression)
statement;
else if (expression)
statement;
else if (expression)
statement;
else
statement;

LEVEL I COURSE OF STRUCTURED PROGRAMING ISLAPE BY TAGA


II- CONDITIONAL EXPRESSION

The c onditional expression is a ternary op erator; that is, it takes three


op e rands. I t h as the following
form
(expression 1) ? (expression 2) : (expression 3)
If the fi rst e xpression is TRUE (i.e., non-zero), the s econd expression
is evaluated, otherwise the
third is e valuated. Thus, the result of the ternary expression will b e
the r esult of e ither the s econd
or third e xpressions, resp ectively. For e xample, to c alculate the
maximum of two values,
c = (a > b) ? a : b; /* c = max(a,b) */
As a b ranching construct, the ?: operator appears far less frequently
than the if-else and switch
constructs.
III- SWITCH
The switch statement is a multi-way decision that tests whether an expression
matches
one of a number of constant integer values, and branches accordingly [KR88,
page 58].
The general form of the switch statement is as follows.
switch (expression) {
case const-int-expr: statements
case const-int-expr: statements
default: statements
}

It is worth mentioning here that all the c ontrol structures—if-else, ?: , while, do-while, and
for—can be nested, which means that they can exist within other control statements. The
switchstatement is no exception, and the statements following a case label may include a
switch or other
control structure. For example, the following code-structure is legitimate.
if (expression)
while (expression)
switch(integer expression) {
case A1:
switch(integer expression) {
case B1: statements
case B2: statements
case B3: statements
}
case A2: statements

LEVEL I COURSE OF STRUCTURED PROGRAMING ISLAPE BY TAGA


default: statements
}
The following example converts the value of a double variable angle to normalised radians
(i.e.,
−π ≤ angle ≤ π). The original angle representation is either degrees or radians, as indicated by
the integer angletype, and DEG, RAD, and PI are symbolic constants.
switch (angletype)
{
case DEG:
angle *= PI / 180.0; /* convert to radians */
/* fall through */
case RAD:
while (angle > PI) /* normalise radians */
angle -= 2.0*PI;
while (angle < -PI)
angle += 2.0*PI;
break;
default:
printf("Error: Invalid type\n");
break;
}
IV- While LOOPS

The while loop has the general form


while (expression)
statement;
If the conditional expression is TRUE, then the while will execute the following
statement, after
which it will reevaluate the conditional. It continues this iteration while ever the
conditional remains
TRUE. As with the if-else statement, the while loop can execute multiple
statements as a block
by enclosing them in braces.
V- Do-While Loops

The do-while loop has the general form


do
statement;
while (expression);
VI- For Loops
The for loop has the general form
for (expr1; expr2; expr3)
statement;
Its behaviour is equivalent to a while loop with the following arrangement of
expressions.
expr1;

LEVEL I COURSE OF STRUCTURED PROGRAMING ISLAPE BY TAGA


while (expr2) {
statement;
expr3;
}

VII- Break and Continue


As seen previously, break can be used to branch out of a switch-statement. It may also be used
to
branch out of any of the iterative constructs. Thus, a break may be used to terminate the
execution
of a switch, while, do-while, or for. It is important to realise that a break will only branch out
of
an inner-most enclosing block, and transfers program-flow to the first statement following the
block.
For example, consider a nested while-loop,
while (expression) {
while (expression) {
if (expression)
break;
statements
}
statements
}
Here the break will terminate the inner while-loop and proceed to execute the statements of
the
outer while-loop.

LEVEL I COURSE OF STRUCTURED PROGRAMING ISLAPE BY TAGA

You might also like