You are on page 1of 24

SME1013

Programming for
Engineers

Control Statements
by:
Assoc. Prof. Dr. Mohd Shafiek Yaacob
Branching and Looping
 Branching
- One of several possible actions will be carried
out, depending on the outcome of the logical test.
- Sometimes a group of statements is selected
from several available groups.
 Looping
- A group of instruction is executed repeatedly,
until some logical condition has been satisfied.
- Sometimes the number of repetitions is known.
Relational and Logical Operators
 The relational operators are
< <= > >=

 The equality operators are


== !=

 The logical operators are


&& ||
Example 6.3 (conditional operator ?:)
status = (balance==0)? ‘C’:‘0’

yes no
balance==0?

status=‘C’ status=‘0’
The if-else Statement
 The if-else statement is used to carry out a
logical test and take one of two possible
actions.
?
IF (expression) statement
or
IF (expression) statement1 else statement2
Example 6.5 (if statement)

if (x < 0) printf(“%f”, x);

if (pastdue > 0)
credit = 0;

if (x <= 3.0) {
y = 3 * pow(x, 2);
printf("%f\n", y);
}
Example 6.5 (if-else statement)
if(status == ‘S’)
tax = 0.20*pay;
else
tax = 0.14 * pay;

if (pastdue > 0) {
printf(“acc no %d is overdue”, accno);
credit = 0;
}
else
credit = 1000.0;
Nested Branching Operation
 A nested branch is used when the computer
needs to choose between more than two
alternatives.
if (expression) ?
statement1
else ?
if (expression)
statement2
else
statement3
Example 6.7
Construct a nested branching operation to test
if n is negative, positive, or zero.
int n;
scanf(“%d”, &n);
if (n>0)
printf(“n is positive”);
else
if (n==0)
printf(“n is zero”);
else
printf(“n is negative”);
Looping: The while Statement
 A group of statements is executed repeatedly, until
some condition has been satisfied.
while (expression) statement

yes no
Finished ?

body of
loop
Example 6.8
#include <stdio.h>

main( ) /* display integers 0 through 9 */


{
int digit=0;

while (digit <= 9) {


printf(“%d\n”,digit);
++digit;
}
}
Looping: The do-while
Statement
 The test for continuation is carried out at the end
of each pass.
do statement while (expression)

body of no
loop
yes
Finished ?
Example 6.11
#include <stdio.h>

main( ) /* display integers 0 through 9


*/
{
int digit=0;

do
printf(“%d\n”, digit++);
while (digit <= 9);
}
Looping: The for Statement
 Most widely used in
computer programming
loop
 No of iterations or finished initialize back
computations is terminate
increment
predetermine
index
for (expression1;
expression2; body of
expression3;) loop

statement
Example 6.11
#include <stdio.h>

main( ) /* display integers 0 through 9


*/
{
int digit=0;

for (digit=0; digit<=9; ++digit)


printf(“%d\n”, digit);
}
Nested Control Structures
 Loops, like if-else statements, can be
nested, one within another.
 One loop must be completely embedded
within the other
 Nested control structure can involve both
loops and if-else statements.
start
Sample 6-1
n=1
n<=10
Construct a program ++n

to create a 10 by 10 n
m=1
multiplication table m<=10
Stop ++m
such as 1 x 1 = 1,
m
1 x 2 = 2, and so Product =
forth nxm

Print
n, m,
Product
Sample 6.1
#include <stdio.h>
main( ) /* 10x10 multiplication table */
{
int n, m, p;
for (n=1; n<=10; ++n){
for (m=1; m<=10; ++m){
p=n*m;
printf(“%d x %d = %d\n”, n,m,p);
}
}
}
The switch, break, and continue
Statements
 The switch statement causes a particular
group of statements to be chosen from several
available groups.
switch (expression) statement
 The break statement is used to terminate
loops or to exit from a switch.
break;
 The continue statement is used to bypass the
remainder of the current pass through a loop.
continue;
Example 6.23
switch (choice = getchar()) {
case ‘r’:
printf (“RED”);
break;
case 'w':
Case ‘W’:
printf (“WHITE”);
break;
case 'b’;
printf (“BLUE”);
}
Example 6.28
scanf (“%f”, &x);
while (x <= 100) {
if (x < 0) {
printf(“ERR0R - NEGATIVE VALUE FOR X”);
break;
}
/* process the nonnegative value of x */
. . . .
scanf (“%f”, &x);
}
Example 6.30
do {
scanf(“%f”, &x);
if(x < 0) {
printf(“ERR0R - NEGATIVE VALUE FOR X”);
continue;
}
/*process the nonnegative value of x */
. . . . .
} while (x <= 100);
The goto Statement
 The goto statement is used to alter the normal
sequence of program execution by transfering
control to some other part of the program.
goto label;
 The label is an identifier that is used to label
the target statement to which control will be
transferred
label: statement
Example 6.33
scanf(“%f”, &x);
while (x <= 100) {
. . . . .
if (x < 0) goto errorcheck;
. . . . .
scanf (“f”, &x);
}
/* error detection routine */
. . . . .
errorcheck: {
printf (“ERROR - NEGATIVE VALUE FOR X”);
}

You might also like