0% found this document useful (0 votes)
14 views10 pages

Fe1008 06

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
14 views10 pages

Fe1008 06

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 10

Three Basic Program Structures

1. Sequence
FE1008 Computing Statements are executed line by line in
sequential order.
2. Selection
Chapter 6 Sequence of program execution may be altered
based on some conditions.
Decision Making 3. Repetition
A group of statements is repeated a certain
number of times.

6-2

Relational Operators Relational Operators in C

In programming, we frequently need to check


expressions to see whether they are true or not. > Greater than
If true, the program flow follows one path, and if
>= Greater than or equal to
false, it follows another path.
< Less than
For example:
<= Less than or equal to
Is exam_mark greater than 80?
== Equal to
Is getchar() == '\n' ?
!= Not equal to
Does the integer n lie in the interval
[0,10]?
6-3 6-4
Example: Example (Continued):
Output:
An expression containing a relational operator
has value 1 (TRUE) or 0 (FALSE). The expression x*x >= 0 has value 1
For real x; x*x >= 0 is always true. ------------------------------------

The C expression x*x >= 0 has a value! On the other hand,


printf("The expression x*x < 0 has
x = -5; value %d", x*x<0);
printf("The expression x*x >= 0 has Output:
value %d", x*x>=0);
The expression x*x < 0 has value 0
6-5 6-6

Note: Note:
Do not confuse == with = (assignment) In C, any non-zero numeric value can also
a==b Checks whether values of a and b are represent TRUE.
equal.
Example:
a=b Value of b is assigned to a. There is change
of data. a = 15;
Suppose a = 1, b = 5 if (a) printf("%d", a);
printf("The value of a==b is %d", a==b); As a is not zero, printf() will be executed.
printf("The value of a=b is %d", a=b);
If a is 0 (which means FALSE), printf() will
not be executed.
6-7 6-8
Note: Remark on the example about isdigit()
Original Form:
Suppose we write the statement as
if (a = 15) printf("%d", a); char ch='1';
if (isdigit(ch) != 0)
Interpretation: printf("%c is a digit.", ch);
else
Value of expression a = 15 is 15. printf("%c is not a digit.", ch);

Condition is TRUE. Simpler Form:


How about if (isdigit(ch))
if (a = 0) printf("%d", a); printf("%c is a digit.", ch);
else
if (a == 0) printf("%d", a); ? printf("%c is not a digit.", ch);
6-9 6-10

Comparing Characters Example

Expression Value
Characters can be compared using relational
operators.
'9' >= '0' 1 (TRUE)
Is 'B' > 'A'? (57) (48)
Is '1' > 'A'? 'B' <= 'A' 0 (FALSE)
Basis for comparison?
'A' <= 'a' 1 (TRUE)
ASCII values
(65) (97)
'a' <= '$' 0 (FALSE)
6-11 6-12
Logical Operators (&&, ||, !) Examples
AND, OR, NOT
temperature > 90.0 && humidity > 0.9
Symbols && || !
x >= 1 || x < 0
These are needed to form complex conditions.
They increase the decision-making capabilities x >= 1 && x < 0 (possible?)
of our C programs. !(x >= 1 && x < 0) (True or false?)
&& and || are binary operators because they 'A' <= ch && ch <= 'Z'
act on 2 expressions.

! is unary.
6-13 6-14

Truth Values and Truth Table Logical Assignment


The truth value (True or False) of a logical We may even assign logical expressions to
expression is determined by a Truth Table. In variables which will then evaluate to 0 or 1.
the table below, P and Q are relational
expressions. even = (n%2 == 0);
in_range = (n > -10 && n < 5);
P Q P && Q P || Q !P is_letter = ('A' <= ch && ch <= 'Z') ||
F F F F T ('a' <= ch && ch <= 'z');
F T F T T
T F F T F
T T T T F 6-15 6-16
More about the if statement Program 6.1 (quadratic equation)
if (expression) a*x*x + b*x + c = 0
{ Program 2.4 fails if the user inputs 0 for the
statement_1; coefficient a.
....... We should test for a non-zero value of a given by
statement_n; the user.
} if (a != 0) //can be written as if (a)
{ root1 = (-b + sqrt(…))/(2*a);
As we have seen in Chap 2, if expression is root2 = . . .;
true, the statement block is executed. Otherwise,
}
the block is skipped.
6-17 6-18

The else clause Program 6.2 (quadratic equation)


The if structure is very frequently used with
if (a == 0)
the optional else:
{
printf("You input a = 0.\n");
if (expression) printf("Only one root: %8.3f", -c/b);
{ . . . . . . . }
} else
else { root1 = (-b + sqrt(…))/(2*a);
{ . . . . . . . . . .
} }

6-19 6-20
Exercises: Nested if statements

These are if statements that contain other if


How do you handle the following situations?
statements.
• a = 0 and b = 0, c not 0 In the quadratic equation problem, what if
• a = b = c = 0 b*b - 4*a*c < 0 ?
A good program must take into account these We’ll use a nested if to deal with this.
possibilities.

6-21 6-22

Program 6.3 Exercise

if (a == 0) { . . .} Modify Program 6.3 so that it appears to be


capable of handling complex roots. How?
else
{ if (b*b-4*a*c < 0) A little cheating!
printf("Complex roots.\n") Suppose the complex roots are
else
α + iβ, α - iβ
{
Compute α and β separately, and in the output
root1 = (-b + sqrt() . . .);
insert the + i and - i.
. . .
}
}
6-23 6-24
if..else if..else if.. An Equivalent Form
Program Fragment 6.4: if (quiz_mark >= 80)
if (quiz_mark >= 80) grade = 'A';
grade = 'A';
else if (quiz_mark >= 70)
else
if (quiz_mark >= 70) grade = 'B';
grade = 'B'; else if (quiz_mark >= 60)
else
grade = 'C';
if (quiz_mark >= 60)
grade = 'C'; else if
else . . . . . .
6-25 6-26

Exercise Exercise
• Rewrite the above so that the program tests Suppose we rewrite this program fragment in
for 'F' grade first, then 'D', etc. the form:

• Suppose we rewrite Program Fragment 6.4 if (quiz_mark < 50) grade = 'F';
using separate if statements: if (quiz_mark >= 50) grade = 'D';
if (quiz_mark >= 80) grade = 'A'; if (quiz_mark >= 60) grade = 'C';
. . .
if (quiz_mark >= 70) grade = 'B';
if (quiz_mark >= 60) grade = 'C'; What's the difference?
Is it equivalent to the original if-else-if...
. . . . . .
form?
Are the 2 ways of writing equivalent?
6-27 6-28
Program 6.5 Program 6.5 (Continued)

Purpose: To construct a menu of choices which


looks like the following: char selection;
printf("Select . . . Ohm’s Law.\n");

Select the form of Ohm’s Law: printf("[A] Voltage [B] …\n");

[A] Voltage [B] Current [C] Resistance printf("Your selection => ")
scanf("%c", &selection);
Your Selection (A, B, C) =>

6-29 6-30

Program 6.5 (Continued) The switch statement


Too many if-else statements can be confusing.
if (selection == 'A')
In this case, we may use the switch statement.
printf("V = I*R");
switch (expression)
else if (selection == 'B')
{ case value_1: statement(s)
printf("I = V/R");
case value_2: statement(s)
else if (selection == 'C')
. . . . . .
printf("R = V/I");
case value_n: statement(s)
else
default: statement(s)
printf("Wrong selection");
6-31
} 6-32
The switch statement (Continued) The break statement
The expression has an integer (including char)
value. If C finds a match, it will still execute the
The computer compares this value against the remaining cases, without checking the case
value following each case label. values unless there is a break statement.
• If there is a match, the statement(s) following Thebreak statement is used to exit from the
the case label will be executed. switch structure.
• If no match at all, the default label (optional)
will be executed.
• If no match and no default label, then exit.

6-33 6-34

Program 6.6 Program 6.6a (without a ‘break’)


switch (selection)
switch (selection)
{case 'A': printf("V = I*R");
{case 'A': printf("V = I*R");
break;
break;
case 'B': printf("I = V/R");
case 'B': printf("I = V/R");
case 'C': printf("R = V/I");
break;
break;
case 'C': printf("R = V/I");
break; default: printf("Wrong selection.");
default: printf("Wrong selection. "); }
} If user picks 'B', output is:
I = V/RR = V/I
6-35 6-36
The Conditional Operator (?:)

This is a ternary operator acting on 3 operands. It


is related to if-else.

expr1 ? expr2 : expr3


which is equivalent to
if (expr1) expr2
else expr3

status = (mark >= 50) ? 'P' : 'F';

expr1 expr2 expr3


6-37

You might also like