week8week8 week8week8 selection making decisions 1 kulliyyah of ict info 2020 structured programming...

53
1 W E E K 8 Selection Making Decisions Kulliyyah of ICT INFO 2020 Structured Programming Language

Upload: noel-smith

Post on 02-Jan-2016

221 views

Category:

Documents


2 download

TRANSCRIPT

Page 1: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

1

WEEK

8

WEEK

8

Selection Making Decisions

Kulliyyah of ICT

INFO 2020Structured Programming

Language

Page 2: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

2

INFO2020 KICT

Selection Making Decision

• Logical Data and Operators• Two-way selection• Multiway selection• More standard library functions

Page 3: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

3

INFO2020 KICT

Logical data in C

C has no logical data type.• E.g. If a data item is zero, it is

considered false• If a data item is nonzero, it is

considered true

Page 4: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

4

INFO2020 KICT

True and false on the arithmetic scale

Page 5: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

5

INFO2020 KICT

Logical Operators

C has three logical operators;a) not operator ( ! ) is a unary

operatorb)and operator ( && ) is a binary

operatorc) or operator ( || ) is a binary

operator

Page 6: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

6

INFO2020 KICT

Logical Operators (cont …)

Operator Meaning Associativity Precedence

! not Right 15

&& Logical and Left 5

|| Logical or Left 4

Page 7: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

7

INFO2020 KICT

Logical operators truth table

Page 8: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

8

INFO2020 KICT

Evaluating Logical Expressions

Computer languages can use two methods to evaluate the binary logical relationships;First method, the expression must be completely evaluated, even when the first operand is false and it is known that the end result must be false.Second method, sets the resulting value as soon as it is known. It does not need to complete the evaluation. It operates a short-circuit methods.

In essence, C uses this short-circuit methodLogical expression is an expression that uses one or more of the logical operators && (and), || (or), !(not).

Page 9: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

9

INFO2020 KICT

Short Circuit methods for and/or

Page 10: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

10

INFO2020 KICT

Evaluating logical expression -Example 1

If a = 10, b = -1 and c = 0, what is the value of the following expression?

a)a && b b)a && c c)c && ad)a || c e)c || af)!a && !cg)!a && ch)a && !c

a) 1b) 0c) 0d) 1e) 1f) 0g) 0h) 1

Page 11: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

11

INFO2020 KICT

Evaluating logical expression - Example 2

If x = 3, y = 0 and z = -4, what is the value of the following expression?

a)x && y || zb)x || y && zc)( x && y ) || zd)( x || y ) && ze)( x && z ) || y

a) 1b) 1c) 1d) 1e) 1

Page 12: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

12

INFO2020 KICT

Relational Operators

• Six relational operators support logical relationships.

• They are all binary operators that accept two operands and compare them.

• Associativity start from left.

Page 13: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

13

INFO2020 KICT

Example of Relational Operators

Note: The first four operators will be evaluated first before the equaland not equals operators when they appear together in the same expression

Page 14: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

14

INFO2020 KICT

Logical Operators Compliments

• It is important to recognize that each operator is a complement of another operator in the group.

Page 15: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

15

INFO2020 KICT

Example of Logical Operators Compliments

The above figure shows each operator and its complement.It is may be unexpected compliments

Page 16: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

16

INFO2020 KICT

Clear and Good Coding Expression

Original Expression Simplified Expression

!(x < y) x >= y

!(x > y) x <= y

!(x != y) x == y

!(x <= y) x > y

!(x >= y ) x < y

!(x == y) x != y

Page 17: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

17

INFO2020 KICT

How to simplify the expression-Example 1

Simplify the following expression by removing the ! operator and parentheses.

a)!(x < y )b)!(x >= y)c)!(x == y)d)!(x != y )e)!(!(x > y))

a) x >= y b) x < yc) x != yd) x == ye) x > y

Page 18: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

18

INFO2020 KICT

Operator Precedence

Operator Precedence

Function calls highest

lowest

! + - &(unary operators)

* / %

+ -

< <= >= >

== !=

&&

||

=

Page 19: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

19

INFO2020 KICT

Evaluating relational expression-Example 1

Assume i = 1, j = 2, k = 3 and m = 2. What does the following statements print?

a)printf (“%d\n”, i == 1);b)printf (“%d\n”, j == 3);c)printf (“%d\n”, i >= 1 && j < 4);d)printf (“%d\n”, m <= 99 && k < m);e)printf (“%d\n”, j >= i || k == m);f)printf (“%d\n”, k + m < j || 3 – j >= k);

a) 1b) 0c) 1d) 0e) 1f) 0

Page 20: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

20

INFO2020 KICT

Evaluating relational expression - Example1 (cont…)

Assume i = 1, j = 2, k = 3 and m = 2. What does the following statements print?

a)printf (“%d\n”, j != m);b)printf (“%d\n”, !(j – m));c)printf (“%d\n”, !(k > m));d)printf (“%d\n”, !(j > k ));

a) 0b) 1c) 0d) 1

Page 21: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

21

INFO2020 KICT

Evaluating relational expression-Example 2

Assume x = -2, y = 5, z = 0 and t = -4. What is the value of each of the following expression?

a)x + y < z + tb)x – 2 * y + y < z * 2 / 3 c)3 * y / 4 % 5 && yd)t || z < (y + 5) && ye)! (4 + 5 * y >= z - 4) && (z – 2)

a) 0b) 1c) 1d) 1e) 0

Page 22: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

22

INFO2020 KICT

Evaluating relational expression -Example 3

Evaluate the following expression to true or false? Show how you get the answers.

a)!(3 + 3 >= 6);b)1 + 6 == 7 || 3 + 2 == 1c)1 > 5 || 6 < 50 && 2 < 5d)14 != 55 && !(13 < 29) || 31 >

52e)6 < 7 > 5

a) Falseb) Truec) Trued) Falsee) False

Page 23: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

23

INFO2020 KICT

Two-Way Selection

The basic decision statement in the computer is the two-way selection. It has two condition;

a)First condition is true where one or more action statements are executed.

b)Second condition is false where different action or set of actions is executed.

Then the process continues with the next statement after the selection.

Page 24: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

24

INFO2020 KICT

Two-Way Decision Logic

Page 25: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

25

INFO2020 KICT

if - else

• The expression must be enclosed in parentheses.

• The expression can has a side effect.• No semicolon after if … else• Must put semicolon after statement1 and

statement2.• Both true and false statements can be any

statement, another if…else statement or null statement.

• Statement1 and statement2 must be one statement. If it is more than one (multiple statements) can be combined into a compound statement and must use open and close braces.

• Can swap the position of statement1 and statement2 if we use the complement of the original expression.

Page 26: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

26

INFO2020 KICT

if-else logic flow

Page 27: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

27

INFO2020 KICT

Example if-else statement

Page 28: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

28

INFO2020 KICT

Compound statement in an if…else

The first example, compound statement for true condition. The second example the compound statements for both conditions. It begins with open brace and end with close brace

Page 29: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

29

INFO2020 KICT

Complemented if…else statements

Page 30: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

30

INFO2020 KICT

A null…else statement

If the condition is true, it will proceed with true statements but if it is false, it will do nothing. If it is null it can be omitted.

Page 31: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

31

INFO2020 KICT

A null if statement

We don’t use null in the true branch of an if…else statement. The solution is complement the expression and swap the two statements.

Page 32: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

32

INFO2020 KICT

Nested if statement

•Nested if statement happened when an if…else is included within an if….else.

•There is no limit to how many levels can be nested, but if more than three, they become difficult to read.

Page 33: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

33

INFO2020 KICT

Logic flow for nested if

Page 34: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

34

INFO2020 KICT

Dangling else problem

•Dangling else problem is created when there is no matching else for every if.

•C’s solution ;Always pair an else to the most recent unpaired if in the current block.

Page 35: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

35

INFO2020 KICT

Logic flow for dangling else

The programmer intended the else statement to be paired with the first if. However the compiler will pair it with the second if.

Page 36: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

36

INFO2020 KICT

Dangling else solution

The second if was enclosed in braces to be compound statement and the else is automatically paired with the correct if.

Page 37: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

37

INFO2020 KICT

Conditional Expressions

Another alternative to the if…else concept is the ternary conditional expression that was found at priority 3 in the precedence table.

The conditional expression has three operands and two operators.

expression ? expression1 : expression2 C first evaluates the leftmost

expression. If the expression is true, the value of the conditional expression is the value of expression1. If the expression is false, the value of the conditional expression is the value of expression2.

Page 38: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

38

INFO2020 KICT

Flow logic for conditional expression

When a equal to b, c– will be evaluated and one will subtract from c, c++will be ignored. Otherwise, a is not equal to b, c++ will be evaluated, c– will be ignored.

Page 39: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

39

INFO2020 KICT

Multi-way selection

•Multi-way selection chooses among several alternatives.

•C has two different way to implement multi-way selection;

a)Switch statementb)else if

Page 40: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

40

INFO2020 KICT

switch decision logic

Switch is a composite statement used to make a decision between manyalternatives. The selection condition must be one of the C integral types.

Page 41: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

41

INFO2020 KICT

switch statement

switch(grade){ case ‘A’: case ‘a’: ++aCount; break; case ‘B’: case ‘b’: ++bCount; break; case ‘C’: case ‘c’: ++cCount; break; default: printf(“…….”); break;}

Page 42: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

42

INFO2020 KICT

while ((grade = getchar( ))!=EOF)

The parenthesized assignment (grade =getchar( )) is executed first.

The getchar function (from the standard input/output library) reads one character from the keyboard and stores that character in integer variable grade .

The value of the assignment grade=getchar ( ) is compared with the value of EOF (a symbol whose acronym stands for “end of file”)

Page 43: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

43

INFO2020 KICT

EOF

EOF which normally has the value –1 as the sentinel value.

EOF is a symbolic integer constant defined in the <stdio.h> header file.

If the value assigned to grade is equal to EOF, the program terminates.

On UNIX system: EOF indicator is entered by typing the sequence <return><ctrl-d>

Microsoft MS-DOS: EOF indicator can be entered by typing <ctrl-z>

NOTE: The character ‘a’ has the value 97a)The integer 97 is the character’s numerical

representation in the computer.b)Computer use ASCII character set in which 97

represents the lower case letter ‘a’

Page 44: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

44

INFO2020 KICT

switch(grade)

Keyword switch is followed by the variable name grade in parentheses. This called the “controlling expression”

The value of this expression is compared with each of the case labels.

Example, user enter c or C is automatically compared to each case in the switch. When match occurs (case ‘C’: case ‘c’) the statement for that case are executed. Then cCount is incremented by 1.

The switch structured is exited immediately with break statement.

Page 45: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

45

INFO2020 KICT

case ‘A’: case ‘a’:

Each case expression is associated with a constant and the keyword case together with its constant are known as a case-labeled statement.

Each case can have zero, one or more actions/statements.

Braces are not required around multiple actions/statements in a case of a switch.

case ‘A’: case ‘a’: is means the same set of actions is to occur for either of these cases.

switch structure can only be used for testing a constant integeral expression.

i.e. any combination of character constants and integer constants that evaluates to a constant integer value.

A character constant is represented as the specific character in single quotes such as ‘A’. It must enclosed within single quotes to be recognized as character constant.

Page 46: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

46

INFO2020 KICT

default

Default is a special form of the labeled statement. It is executed whenever none the other case values matches the value in the switch expression. Example if the user enter invalid grade such as H, so the default case will mention that it is invalid grade and ask the user to reenter again the valid grade.

case 1 : we also can use integer constant for each cases.

case 2 :

Page 47: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

47

INFO2020 KICT

The switch multiple–selection structure with break

case a

case b

default action(s)

case b action(s)

case a action(s) break

break

……….

True

True

False

FalseBreak each statement at the end of a case causes control to immediately exit the switch structure

Page 48: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

48

INFO2020 KICT

switch flow

Page 49: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

49

INFO2020 KICT

break;

The break statement causes program to continue with the first statement after switch structure.

If we don’t put break statement, the cases in a switch statement for all the remaining cases will be executed.

See the example flow on the next slide.

Page 50: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

50

INFO2020 KICT

switch results

If printfFlag is a 1, then all three print statements are executed.If printFlag is a 2, then the first print statement is skipped and the last two are executed.If printFlag is neither 1 nor 2, then only default is executed.The break allow us to leave the body of the switch as soon as we havecompleted the case.

Page 51: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

51

INFO2020 KICT

Logic flow for switch-break

Page 52: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

52

INFO2020 KICT

else-if

• If the value is in the condition is not integral, we can use else-if.

• Once the correct range is located, none of the following conditions will be tested.

• Example, if a score of 85 is entered, the test against 90% is false, so we execute the else-if test for a score greater than 80%. The condition is true, then we set grade to ‘’ and ship all the remaining sets.

Page 53: WEEK8WEEK8 WEEK8WEEK8 Selection Making Decisions 1 Kulliyyah of ICT INFO 2020 Structured Programming Language

53

INFO2020 KICT

The else-if logic design for program 5-9