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

Slides:



Advertisements
Similar presentations
1 Chapter Five Selection and Repetition. 2 Objectives How to make decisions using the if statement How to make decisions using the if-else statement How.
Advertisements

BBS514 Structured Programming (Yapısal Programlama)1 Selective Structures.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4 Control Structures I. Objectives ► Examine relational and logical operators ► Explore how to form and evaluate logical (Boolean) expressions.
© Copyright 1992–2004 by Deitel & Associates, Inc. and Pearson Education Inc. All Rights Reserved. 1 Chapter 4 – C Program Control Outline 4.1Introduction.
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
Logical Operators Java provides two binary logical operators (&& and ||) that are used to combine boolean expressions. Java also provides one unary (!)
true (any other value but zero) false (zero) expression Statement 2
 2007 Pearson Education, Inc. All rights reserved C Program Control.
 2000 Prentice Hall, Inc. All rights reserved. Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
C++ for Engineers and Scientists Third Edition
 2008 Pearson Education, Inc. All rights reserved JavaScript: Control Statements II.
C How to Program, 6/e Summary © by Pearson Education, Inc. All Rights Reserved.
CONTROL STATEMENTS IF-ELSE, SWITCH- CASE Introduction to Computer Science I - COMP 1005, 1405 Instructor : Behnam Hajian
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ Programming: From Problem Analysis to Program Design, Fourth Edition Chapter 4: Control Structures I (Selection)
C++ Programming: From Problem Analysis to Program Design, Third Edition Chapter 4: Control Structures I (Selection)
1 Chapter 4: Selection Structures. In this chapter, you will learn about: – Selection criteria – The if-else statement – Nested if statements – The switch.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational operators – Discover.
Lecture 4 C Program Control Acknowledgment The notes are adapted from those provided by Deitel & Associates, Inc. and Pearson Education Inc.
Liang, Introduction to Programming with C++, Second Edition, (c) 2010 Pearson Education, Inc. All rights reserved Chapter 3 Selections.
Dr. Yang, QingXiong (with slides borrowed from Dr. Yuen, Joe) LT3: Conditional Statements CS2311 Computer Programming.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
1 Lecture 5: Selection Structures. Outline 2  Control Structures  Conditions  Relational Operators  Logical Operators  if statements  Two-Alternatives.
Pseudocode When designing an ALGORITHM to solve a problem, Pseudocode, can be used. –Artificial, informal language used to develop algorithms –Similar.
A First Book of ANSI C Fourth Edition Chapter 4 Selection.
1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical operators: and, or, and not ❏ To understand how a C program.
Control Statements in C 1.Decision making statements 2.Looping statements 3.Branching statements
Chapter 7 Selection Dept of Computer Engineering Khon Kaen University.
Liang, Introduction to C++ Programming, (c) 2007 Pearson Education, Inc. All rights reserved X1 Chapter 3 Control Statements.
Computer Science: A Structured Programming Approach Using C1 5-2 Two-Way Selection The decision is described to the computer as a conditional statement.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
 2000 Prentice Hall, Inc. All rights reserved. 1 Chapter 4 - Program Control Outline 4.1Introduction 4.2The Essentials of Repetition 4.3Counter-Controlled.
Selection-making Decisions Selection allows you to choose between two or more possible program flow --- it lets you make decisions in your program. Examples.
Chapter 5: Control Structures I (Selection). Objectives In this chapter you will: Learn about control structures Examine relational and logical operators.
Sections © Copyright by Pearson Education, Inc. All Rights Reserved.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Chapter 4: Control Structures I (Selection). Objectives In this chapter, you will: – Learn about control structures – Examine relational and logical operators.
CPS120: Introduction to Computer Science Decision Making in Programs.
Decision Statements, Short- Circuit Evaluation, Errors.
Switch Selection Structure (L14) * General Form of the switch Statement * Details of switch Statement * Flowchart of switch Statement * cin.get * Character.
CPS120: Introduction to Computer Science Decision Making in Programs.
Programming Language C++ Lecture 3. Control Structures  C++ provides control structures that serve to specify what has to be done to perform our program.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Lecture 3.1 Operators and Expressions Structured Programming Instructor: Prof. K. T. Tsang 1.
C Program Control September 15, OBJECTIVES The essentials of counter-controlled repetition. To use the for and do...while repetition statements.
Computer Science: A Structured Programming Approach Using C1 Objectives ❏ To understand how decisions are made in a computer ❏ To understand the logical.
Internet & World Wide Web How to Program, 5/e © by Pearson Education, Inc. All Rights Reserved.
C++ for Engineers and Scientists Second Edition Chapter 4 Selection Structures.
Chapter 4 – C Program Control
Chapter 3 Control Statements
Chapter 4 C Program Control Part I
Selections Java.
Control Structures Combine individual statements into a single logical unit with one entry point and one exit point. Used to regulate the flow of execution.
Selection—Making Decisions
Topic 3, Selection Statements
Chapter 3 Control Statements Lecturer: Mrs Rohani Hassan
Chapter 4: Making Decisions.
JavaScript: Control Statements.
JavaScript: Control Statements I
Chapter 8 JavaScript: Control Statements, Part 2
3 Control Statements:.
Topics discussed in this section:
Chapter 4 - Program Control
Presentation transcript:

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

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

INFO2020 KICT 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

INFO2020 KICT 4 True and false on the arithmetic scale

INFO2020 KICT 5 Logical Operators C has three logical operators; a)not operator ( ! ) is a unary operator b)and operator ( && ) is a binary operator c)or operator ( || ) is a binary operator

INFO2020 KICT 6 Logical Operators (cont …) OperatorMeaningAssociativityPrecedence !notRight15 &&Logical andLeft5 ||Logical orLeft4

INFO2020 KICT 7 Logical operators truth table

INFO2020 KICT 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).

INFO2020 KICT 9 Short Circuit methods for and/or

INFO2020 KICT 10 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 && a d)a || c e)c || a f)!a && !c g)!a && c h)a && !c a)1 b)0 c)0 d)1 e)1 f)0 g)0 h)1

INFO2020 KICT 11 Evaluating logical expression - Example 2 If x = 3, y = 0 and z = -4, what is the value of the following expression? a)x && y || z b)x || y && z c)( x && y ) || z d)( x || y ) && z e)( x && z ) || y a)1 b)1 c)1 d)1 e)1

INFO2020 KICT 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.

INFO2020 KICT 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

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

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

INFO2020 KICT 16 Clear and Good Coding Expression Original ExpressionSimplified 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

INFO2020 KICT 17 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 < y c)x != y d)x == y e)x > y

INFO2020 KICT 18 Operator Precedence OperatorPrecedence Function calls highest lowest ! + - &(unary operators) * / % + - = > == != && || =

INFO2020 KICT 19 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 = k); a)1 b)0 c)1 d)0 e)1 f)0

INFO2020 KICT 20 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)0 b)1 c)0 d)1

INFO2020 KICT 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? a)x + y < z + t b)x – 2 * y + y < z * 2 / 3 c)3 * y / 4 % 5 && y d)t || z < (y + 5) && y e)! (4 + 5 * y >= z - 4) && (z – 2) a)0 b)1 c)1 d)1 e)0

INFO2020 KICT 22 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 || == 1 c)1 > 5 || 6 < 50 && 2 < 5 d)14 != 55 && !(13 52 e)6 5 a)False b)True c)True d)False e)False

INFO2020 KICT 23 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.

INFO2020 KICT 24 Two-Way Decision Logic

INFO2020 KICT 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.

INFO2020 KICT 26 if-else logic flow

INFO2020 KICT 27 Example if-else statement

INFO2020 KICT 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

INFO2020 KICT 29 Complemented if…else statements

INFO2020 KICT 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.

INFO2020 KICT 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.

INFO2020 KICT 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.

INFO2020 KICT 33 Logic flow for nested if

INFO2020 KICT 34 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.

INFO2020 KICT 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.

INFO2020 KICT 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.

INFO2020 KICT 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.

INFO2020 KICT 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.

INFO2020 KICT 39 Multi-way selection Multi-way selection chooses among several alternatives. C has two different way to implement multi-way selection; a)Switch statement b)else if

INFO2020 KICT 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.

INFO2020 KICT 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; }

INFO2020 KICT 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”)

INFO2020 KICT 43 EOF  EOF which normally has the value –1 as the sentinel value.  EOF is a symbolic integer constant defined in the 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  Microsoft MS-DOS: EOF indicator can be entered by typing  NOTE: The character ‘a’ has the value 97 a)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’

INFO2020 KICT 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.

INFO2020 KICT 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.

INFO2020 KICT 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 :

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

INFO2020 KICT 48 switch flow

INFO2020 KICT 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.

INFO2020 KICT 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.

INFO2020 KICT 51 Logic flow for switch-break

INFO2020 KICT 52 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.

INFO2020 KICT 53 The else-if logic design for program 5-9