1 / 53

Kulliyyah of ICT

INFO 2020 Structured Programming Language. Kulliyyah of ICT. Selection Making Decision. Logical Data and Operators Two-way selection Multiway selection More standard library functions. Logical data in C. C has no logical data type. E.g. If a data item is zero , it is considered false

nanda
Télécharger la présentation

Kulliyyah of ICT

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. INFO 2020 Structured Programming Language Kulliyyah of ICT

  2. Selection Making Decision • Logical Data and Operators • Two-way selection • Multiway selection • More standard library functions

  3. 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

  4. True and false on the arithmetic scale

  5. Logical Operators C has three logical operators; • not operator ( ! ) is a unary operator • and operator ( && ) is a binary operator • or operator ( || ) is a binary operator

  6. Logical Operators (cont …)

  7. Logical operators truth table

  8. 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 method • Logical expression is an expression that uses one or more of the logical operators && (and), || (or), !(not).

  9. Short Circuit methods for and/or

  10. Evaluating logical expression -Example 1 If a = 10, b = -1 and c = 0, what is the value of the following expression? • a && b • a && c • c && a • a || c • c || a • !a && !c • !a && c • a && !c • 1 • 0 • 0 • 1 • 1 • 0 • 0 • 1

  11. Evaluating logical expression - Example 2 Ifx = 3, y = 0 and z = -4, what is the value of the following expression? • x && y || z • x || y && z • ( x && y ) || z • ( x || y ) && z • ( x && z ) || y • 1 • 1 • 1 • 1 • 1

  12. Relational Operators • Six relational operators support logical relationships. • They are all binary operators that accept two operands and compare them. • Associativity start from left.

  13. Example of Relational Operators Note: The first four operators will be evaluated first before the equal and not equals operators when they appear together in the same expression

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

  15. Example of Logical Operators Compliments The above figure shows each operator and its complement. It is may be unexpected compliments

  16. Clear and Good Coding Expression

  17. How to simplify the expression-Example 1 Simplify the following expression by removing the ! operator and parentheses. • !(x < y ) • !(x >= y) • !(x == y) • !(x != y ) • !(!(x > y)) • x >= y • x < y • x != y • x == y • x > y

  18. Operator Precedence

  19. Evaluating relational expression-Example 1 Assume i = 1, j = 2, k = 3 and m = 2. What does the following statements print? • printf (“%d\n”, i == 1); • printf (“%d\n”, j == 3); • printf (“%d\n”, i >= 1 && j < 4); • printf (“%d\n”, m <= 99 && k < m); • printf (“%d\n”, j >= i || k == m); • printf (“%d\n”, k + m < j || 3 – j >= k); • 1 • 0 • 1 • 0 • 1 • 0

  20. Evaluating relational expression - Example1 (cont…) Assume i = 1, j = 2, k = 3 and m = 2. What does the following statements print? • printf (“%d\n”, j != m); • printf (“%d\n”, !(j – m)); • printf (“%d\n”, !(k > m)); • printf (“%d\n”, !(j > k )); • 0 • 1 • 0 • 1

  21. 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? • x + y < z + t • x – 2 * y + y < z * 2 / 3 • 3 * y / 4 % 5 && y • t || z < (y + 5) && y • ! (4 + 5 * y >= z - 4) && (z – 2) • 0 • 1 • 1 • 1 • 0

  22. Evaluating relational expression -Example 3 Evaluate the following expression to true or false? Show how you get the answers. • !(3 + 3 >= 6); • 1 + 6 == 7 || 3 + 2 == 1 • 1 > 5 || 6 < 50 && 2 < 5 • 14 != 55 && !(13 < 29) || 31 > 52 • 6 < 7 > 5 • False • True • True • False • False

  23. Two-Way Selection The basic decision statement in the computer is the two-way selection. It has two condition; • First condition is true where one or more action statements are executed. • Second condition is false where different action or set of actions is executed. Then the process continues with the next statement after the selection.

  24. Two-Way Decision Logic

  25. 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.

  26. if-else logic flow

  27. Example if-else statement

  28. 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

  29. Complemented if…else statements

  30. 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.

  31. 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.

  32. 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.

  33. Logic flow for nested if

  34. Dangling else problem • Dangling elseproblem is createdwhen there isno matching elseforevery if. • C’s solution ; Always pair an else to the most recent unpaired if in the current block.

  35. 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.

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

  37. 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.

  38. 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.

  39. Multi-way selection • Multi-way selection chooses among several alternatives. • C has two different way to implement multi-way selection; • Switch statement • else if

  40. switch decision logic Switch is a composite statement used to make a decision between many alternatives. The selection condition must be one of the C integral types.

  41. 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; }

  42. 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”)

  43. 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 97 • The integer 97 is the character’s numerical representation in the computer. • Computer use ASCII character set in which 97 represents the lower case letter ‘a’

  44. 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.

  45. 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.

  46. 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 :

  47. The switch multiple–selection structure with break True case a case a action(s) break False case b True case b action(s) break False Break each statement at the end of a case causes control to immediately exit the switch structure ………. default action(s)

  48. switch flow

  49. 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.

  50. 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 have completed the case.

More Related