1. Programming Methodology &
Abstractions
1
Lecture 8
Decision and Control Statements:
if
CS106
Programming Methodology
& Abstractions
FALL 2005
Balochistan University
of I.T & M.S
Faculty of System Sciences
Sadique Ahmed Bugti
2. Programming Methodology &
Abstractions
2
Conditional Expressions
As we have seen, the following logical,
relational and equality operators exist in C:
These operators are used in conjunction with
decision and control statements.
3. Programming Methodology &
Abstractions
3
Conditional Expressions
For example:
heart_rate > 75
performs the necessary comparison and
evaluates to 1 (true) when heart_rate
is over 75; and evaluates to 0 (false)
when heart_rate is not greater than
75.
4. Programming Methodology &
Abstractions
4
Conditional Expressions
For example:
marks is in the range 40 to 100, (40 marks
b) inclusive
(marks >= 40) && (marks <= 100)
performs the necessary comparison and
evaluates to 1 (true) when marks is greater than
or equal to 40 AND marks is less than or equal to
100; and evaluates to 0 (false) when the either
expression is false.
5. Programming Methodology &
Abstractions
5
Decision and Control Statements
Decision or control-flow statements specify the
order in which computations are performed.
• they are branching statements
There are two principle control statements:
•if-else
•switch
6. Programming Methodology &
Abstractions
6
if-else statement
• The if-else statement is used to carry out
a logical test and then take one of two
possible actions, depending on whether the
outcome of the test is true or false.
• The else portion of the statement is
optional.
8. Programming Methodology &
Abstractions
8
if statement
The simplest possible if-else statement takes the
form:
if(expression)
statement;
The expression is evaluated:
• If expression has a nonzero value (i.e., if
expression if true), statement is executed.
• If expression has a value of zero (i.e., if
expression is false) then the statement will be
ignored.
The expression must be placed in
parenthesis, as shown.
11. Programming Methodology &
Abstractions
11
if statement
Where appropriate, compound statements can
be used to group a series of statements under
the control of a single if expression.
• For example:
if (j < k) if (j < k) {
min = j; min = j;
if (j < k) max =
k;
max = k; }
12. Programming Methodology &
Abstractions
12
Compound if statement
Computes the growth rate of a population from
time t1 to time t2.
if (pop_t2 > pop_t1)
{
growth = pop_t2 – pop_t1;
growth_pct = (growth/pop_t1) *
100;
printf(“The growth % is %.2fn”,
growth_pct);
}
13. Programming Methodology &
Abstractions
13
if-else statement
The if-else statement is an extension of if used in
situations where there are two alternatives:
if(expression)
statement1;
else
statement2;
The expression is evaluated:
• If expression has a nonzero value (i.e., if expression
is true), statement1 is executed.
• If the expression has a value of zero (i.e., if
expression is false) and there is an else part,
statement2 is executed.
15. Programming Methodology &
Abstractions
15
if-else statement
For example:
if (marks >=40)
printf(“Pass);
else
printf(“Fail”);
If marks >= y is true, then “Pass” will be
printed; if it is false, “Fail” will be printed.
16. Programming Methodology &
Abstractions
16
if-else statement
For example:
if (ch >= ‘a’ && ch <= ‘z’)
++lower_char;
else
++other_char;
If ch >= ‘a’ && ch <= ‘z’ (c is a
lowercase character) is true, the variable
lower_char is incremented; if it is false,
other_char is incremented.
17. Programming Methodology &
Abstractions
17
Example: Finding the Minimum Value
/* Find the minimum of two values */
#include <stdio.h>
void main(void)
{
int x, y, min;
printf(“Input two integers: “);
scanf(“%d%d”, &x, &y);
if (x < y)
min = x;
else
min = y;
printf(“The minimum value is %dn”, min);
}
18. Programming Methodology &
Abstractions
18
if-else statement
An example of a syntax error:
if (a != b){
a = a + 1;
b = b + 1;
};
else
c = a + b;
The syntax error occurs because the semicolon
following the right brace creates an empty
statement, and consequently the else has
nowhere to attach.
19. Programming Methodology &
Abstractions
19
The “dangling-else” Problem
There is an ambiguity when an else is
omitted from a nested if statement:
“dangling else” problem
if (marks>= 40)
if (marks > 90)
printf(“Excellent”);
else
printf(“Failed”);
This is resolved by associating the else with
the closest previous else-less if.
20. Programming Methodology &
Abstractions
20
The “dangling-else” Problem
If that’s not what you want, use braces to
force the proper association.
if (marks>=40{
if (marks >90)
printf(“Excellent”);
}
else
printf(“Fail”);
Use braces where there are nested ifs.
21. Programming Methodology &
Abstractions
21
Nested if statement
Sometimes the if-else statement is
used for a multi-way decision.
The expressions are evaluated in order;
if any expression is true, the statement
associated with it is executed, and this
terminates the whole chain.
The last else can be used to handle the
default case where none of the other
conditions is satisfied.
23. Programming Methodology &
Abstractions
23
Nested if statement
For example
if (marks >= 80)
printf(“Grade A”);
else if (marks >=70)
printf(“Grade B”);
else if(marks>=50)
printf(“Grade C”)
else /* marks less than 50 */
printf(“Fail”);
26. Programming Methodology &
Abstractions
26
Example: Quadratic Equation
The program quadratic.c, listed
previously, is incapable of dealing
correctly with cases where the
roots are complex (i.e., b2< 4ac),
or cases where a = 0.
28. Programming Methodology &
Abstractions
28
Example: Quadratic Equation
/* Test for complex roots */
e = b * b - 4. * a * c;
if (e < 0.)
{
printf("nError: roots are complexn");
exit(1);
}
/* Test for a = 0. */
if (a == 0.)
{
printf("nError: a = 0.n");
exit(1);
}
/* Perform calculation */
d = sqrt(e);
x1 = (-b + d) / (2. * a);
x2 = (-b - d) / (2. * a);
/* Display output */
printf("nx1 = %12.3e x2 = %12.3en", x1, x2);
}
29. Programming Methodology &
Abstractions
29
Example: Quadratic Equation
The standard library function call exit(1)
(header file: stdlib.h) causes the program
to abort with an error status.
Execution of the above program for the case
of complex roots yields the following output:
a = 4
b = 2
c = 6
Error: roots are complex %
30. Programming Methodology &
Abstractions
30
Example: Quadratic Equation
/* Perform calculation */
e = b * b - 4. * a * c;
if (e > 0.) // Test for real roots
{
/* Case of real roots */
d = sqrt(e);
x1 = (-b + d) / (2. * a);
x2 = (-b - d) / (2. * a);
printf("nx1 = %12.3e x2 = %12.3en", x1, x2);
}
else
{
/* Case of complex roots */
d = sqrt(-e);
x1 = -b / (2 * a);
x2 = d / (2 * a);
printf("nx1 = (%12.3e, %12.3e) nx2 = (%12.3e, %12.3e)n",
x1, x2, x1, -x2);
}
31. Programming Methodology &
Abstractions
31
Example: Quadratic Equation
The output from the above program for
the case of complex roots looks like:
a = 9
b = 2
c = 2
x1 = ( -1.111e-01, 4.581e-01)
x2 = ( -1.111e-01, -4.581e-01)