CIS 162 AD C Decision Statements 04decisions ppt
- Slides: 35
CIS 162 AD - C# Decision Statements 04_decisions. ppt
Overview of Topic ¨ Pseudocode ¨ Flow Control Structures ¨ Flowcharting ¨ If, If-Else ¨ Nested If’s ¨ Select Case – Switch statement CIS 162 AD 2
Pseudocode ¨ Pseudocode is a mixture of C# code and English like statements. ¨ Used when designing algorithms. . CIS 162 AD 3
Flow Control Structures ¨ The order in which statements are executed. ¨ There are four structures. 1. Sequence Control Structure 2. Selection Control Structure • Also referred to as branching (if and if-else) 3. Case Control Structure (switch) 4. Repetition Control Structure (loops) CIS 162 AD 4
Flowcharting ¨ A flowchart is a pictorial representation of an algorithm or logical steps. ¨ Each step is represented by a symbol and the arrows indicate the flow and order of the steps. ¨ The shape of the symbol indicates the type of operation that is to occur. ¨ Flowcharts may help the more visual students learn and understand logic. CIS 162 AD 5
Flowchart Symbols Begin or End Processing Input or Output Decision Branch or Direction of Flow CIS 162 AD 6
1. Sequence Control Structure ¨ The order statements are placed (sequenced) //input int. Qty = int. Parse(txt. Quantity. Text); dec. Price = decimal. Parse(txt. Price. Text); //process dec. Subtotal = int. Qty * dec. Price; //output txt. Subtotal. Text = dec. Subtotal. To. String(“N”); ¨ The only way to display subtotal, statements must be in this order. CIS 162 AD 7
Flowchart – Sequence Control Begin Input price, qty subtotal = price * qty Output subtotal End CIS 162 AD 8
2. Selection Control ( If ) ¨ Use if statements to determine if a set of statements should be executed. dec. Subtotal = int. Qty * dec. Price if (chk. Sales. Tax. Checked = = true) dec. Sales. Tax = dec. Subtotal * cdec. TAX_RATE; lbl. Sales. Tax. Text = dec. Sales. Tax. To. String(“C”); CIS 162 AD 9
Flowchart – If Statement Subtotal = qty * price True If taxable Sales tax = subtotal * Tax Rate False Output sales tax CIS 162 AD 10
Selection Control (If-Else) ¨ Use if else statements when there are several options to choose from. if (rad. Next. Day. Checked == true) dec. Shipping = cdec. NEXT_DAY_SHIPPING_RATE; else dec. Shipping = cdec. THREE_DAY_SHIPPING_RATE; ¨ After if else statement, dec. Shipping will have a value. CIS 162 AD 11
Flowchart – If-Else Statement User selects Shipping True If Next Day Shipping = Next Day False Shipping = Three Day Output Shipping CIS 162 AD 12
Condition is an Expression ¨ Conditions evaluate to true or false. if (condition) true – 1 or more statements else false – 1 or more statements CIS 162 AD 13
Relational Operators = = ! = < > < = > = Equal to Not Equal to Less than Greater than Less than or equal to Greater than or equal to Relational Operators are used to form conditions, and conditions can involve constants, variables, numeric operators, and functions. CIS 162 AD 14
Block of code – Compound Statements ¨ Use braces to create a block of statements. ¨ Single statements do NOT require braces. if (int. Hours > 40) { dec. Regular. Pay = 40 * dec. Pay. Rate; dec. Overtime. Pay = (int. Hours – 40) * (dec. Pay. Rate * cdec_OVERTIME_RATE); } else { dec. Regular. Pay = int. Hours * dec. Pay. Rate; dec. Overtime. Pay = 0; } dec. Gross. Pay = dec. Regular. Pay + dec. Overtime. Pay; CIS 162 AD 15
Incorrect if-else if (int. Hours > 40) { dec. Regular. Pay = 40 * dec. Pay. Rate; dec. Overtime. Pay = (int. Hours – 40) * (dec. Pay. Rate * cdec_OVERTIME_RATE); } else dec. Regular. Pay = int. Hours * dec. Pay. Rate; dec. Overtime. Pay = 0; dec. Gross. Pay = dec. Regular. Pay + dec. Overtime. Pay; ¨ dec. Overtime. Pay would always be set to zero, because there is a single statement for else since braces are not included. CIS 162 AD 16
Nested If Statements ¨ First if has to be true in order to continue. if (int. Quantity >= 1) if (dec. Price >= 5. 00 M) dec. Subtotal = int. Qty * dec. Price; else { } Message. Box. Show(“Invalid price entered”); txt. Price. Focus(); txt. Price. Select. All(); else { Message. Box. Show(“Invalid quantity entered”); txt. Quantity. Focus(); txt. Quantity. Select. All(); } CIS 162 AD 17
Matching an Else to If ¨ How are else statements matched with an if? ¨ Compiler works it’s way back. When an else is encounter, it looks back to find an if that has not been matched to an else. ¨ Why do we indent each level? ¨ We indent to make programs easier to read. Indenting has no effect on how compiler matches an else to an if. CIS 162 AD 18
Assignment (=) vs Comparison (==) ¨ if (x = 12) // may generate syntax error. ¨ if (x == 12) // comparison ¨ The error message generated will have to do with an implicit data conversion to bool. ¨ If you get this error, check that you used 2 equal signs. CIS 162 AD 19
Logical Operators ¨ Compound or Complex Conditions can be form using Logical Operators. • && – And, both conditions must be true • | | – Or, either condition must be true • ! – Not, reverses the resulting condition CIS 162 AD 20
&& - And Logical Operator ¨ Both conditions must be true in order for the entire condition to be true. if (int. Qty > 0 && int. Qty < 51) dec. Subtotal = int. Qty * dec. Price; Else Message. Box. Show (“Enter a value between 1 and 50”); ¨ What happens if qty = 25? ¨ What happens if qty = 60? CIS 162 AD 21
Use Parentheses ¨ Use parentheses to clarify or group conditions. ¨ All conditions must be enclosed with outer parentheses. if ((int. Qty > 0) && (int. Qty < 51)) dec. Subtotal = int. Qty * dec. Price; else Message. Box. Show (“Enter a value between 1 and 50”); CIS 162 AD 22
| | - Or Logical Operator ¨ Either condition must be true for the entire condition to be true. ¨ The pipe character is located above the Enter key; must shift to select it, and two of them are entered next to each other. if ((int. Qty < 1) || (int. Qty > 50)) Message. Box. Show “Enter a value between 1 and 50”); else dec. Subtotal = int. Qty * dec. Price; ¨ What happens if qty = 25? ¨ What happens if qty = 60? CIS 162 AD 23
! – Not Logical Operator ¨ Not operator makes the condition the opposite of what it evaluated to… ¨ If condition is true, Not makes it false. ¨ If condition is false, Not makes it true. if ! (int. Qty = = 50) true else false int. Qty < > 50 int. Qty = 50 ¨ Confusing, try not to use Not. CIS 162 AD 24
Practice Exercises count =0 limit = 10 min=5 if ((count = = 0) && (limit < 20)) if (count = = 0 && limit < 20) if (limit > 20 | | count < 5 ) if ! (count = = 12) if ((count = = 10) | | (limit > 5 && min < 10)) CIS 162 AD True True 25
Comparing Boolean’s ¨ Booleans are variables that are equal to either True or False. ¨ Some properties are also True or False. ¨ The following condition will return True if the radio button for Next Day was checked: if (rad. Next. Day. Checked = = true) ¨ The shortcut to the above condition is to leave = = true off as follows: if (rad. Next. Day. Checked) ¨ We should usually express the desired condition (= = true) to avoid bugs and confusion. CIS 162 AD 26
Comparing Strings ¨ String variables, properties, or literals can also be compared. ¨ Only (==) and (!=) can be used with strings. ¨ Can use Compare. To method – more on this later. ¨ Strings are compared from left to right. ¨ The characters’ binary value is used to determined which is greater or less than. ¨ This means that capitalization is considered. ¨ ASCII Code Table lists the binary values. CIS 162 AD 27
ASCII Table ¨ ASC II table shows the binary value of all 256 characters. ¨ American Standard Code for Information Interchange ¨ The Dec column shows the decimal value of the binary value. ¨ Binary values are usually converted to hexadecimal (Hex) because the value can represented with two digits instead of 8. ¨ Look up CR, HT, Space, A, a ¨ Shows that a capital A is not the same as a lower case a. ¨ Shows how values will be sorted (collating sequence). ¨ Unicode – 2 bytes, 16 bits, 65, 536 unique characters CIS 162 AD 28
To. Upper and To. Lower Methods ¨ Use To. Upper and To. Lower methods of the String class to convert strings for comparisons. if txt. State. Text. To. Upper( ) = = “CA” dec. Sales. Tax = dec. Subtotal * cdec. TAX_RATE; ¨ When we convert strings we only need to test for one condition, instead of every possible condition – ca, CA, Ca, c. A. CIS 162 AD 29
Input Validation ¨ We can test for missing data by using the empty string literal. if (txt. Name. Text = = “”) ¨ If you find an input error – Display a specific error message (Message. Box. Show). – Use Focus method to place the user in the textbox that had the bad data. – Use Select. All to select the bad data so that users can immediately type in the correct value. ¨ Textboxes should be validated in the order matching the form’s Tab. Order. CIS 162 AD 30
3. Case Control Structure (switch) ¨ Switch statement is an efficient decision-making structure that simplifies choosing among several actions. ¨ Works good for a list of options (menus) ¨ It avoids complex nested If constructs. ¨ Just about all switch statements can be stated using an if block. ¨ Be sure to handle each possible case. ¨ Use default: to catch errors. ¨ A break statement is required at the end of each case. CIS 162 AD 31
Switch Example switch (str. Disc. Type. To. Upper) { case “E”: dec. Discount. Rate =. 05; break; case “S”: dec. Discount. Rate =. 10; break; case “C”: dec. Discount. Rate =. 15; break; default: dec. Discount. Rate =. 00; break; } CIS 162 AD //Employee //Student //Senior Citizen //No discount 32
Switch Expression ¨ The matching case is determined by the value of the expression. ¨ The expression can be a variable, property, operator expression, or method. switch (expression) { case 1: … case 2: … } CIS 162 AD 33
4. Repetition Control (loops) ¨ Loops are covered later in the term. CIS 162 AD 34
Summary ¨ Flow Control Structures ¨ Flowcharts ¨ If, If-Else ¨ Nested if’s ¨ Switch statement CIS 162 AD 35
- No decision snap decision responsible decision
- Slidetodoc.com
- Decision making statements in c with examples ppt
- Decision making statements in c
- Decision table and decision tree examples
- Prime factorization of 144 using exponents
- Speed and velocity quiz
- Tutti i numeri composti
- Hcf lcm prime factorisation
- Mcd de 90 y 126
- Apsc 162
- Factorization of 162
- Komulator
- 18/36 simplified
- 6-1 work together p. 162 answers
- 6-1 work together p. 162 answers
- 1. 6-1 work together, p. 162
- 7 sınıf en'âm suresi 162. ayet
- Rle 162
- 6-4 work together p. 170
- Geometric sequence 6 18 54
- 162 ad
- Pb 162
- Cs 162
- C++ language
- Cs 162
- Cs 162
- Fourth root of 162
- Decision making tools in management ppt
- Cit 590 upenn
- Cis 2168
- Cis decalin newman projection
- What is an unsaturated hydrocarbon
- Genera tu certificado ctc
- Cis548
- Nada ces enharmonis dengan