Beruflich Dokumente
Kultur Dokumente
If Statements
04_decisions.ppt
Overview of Topic
Pseudocode
Flow Control Structures
Flowcharting
If, If-Else
Nested If’s
Select Case – Switch statement
CIS162AD 2
Pseudocode
Pseudocode is a mixture of C# code and English like
statements.
Used when designing algorithms.
When designing, we don’t necessarily want to be
concerned about method names (ToString). We want
to concentrate on the design.
I’ll use pseudocode throughout the course, so don’t
feel compelled to correct my syntax.
However, if at anytime you are not sure about a
command, please be sure to ask for clarification.
CIS162AD 3
Flow Control Structures
CIS162AD 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 move visual students
learn and understand logic.
CIS162AD 5
Flowchart Symbols
Processing Decision
Input
price, qty
subtotal =
price * qty
Output
subtotal
End
CIS162AD 8
2. Selection Control ( If )
Use if statements to determine if a set of statements should be
executed.
if (chkSalesTax.Checked = = true)
decSalesTax = decSubtotal * cdecTAX_RATE;
lblSalesTax.Text = decSalesTax.ToString(“C”);
CIS162AD 9
Flowchart – If Statement
Subtotal =
qty * price
True
Sales tax =
If taxable subtotal * Tax Rate
False
CIS162AD 10
Selection Control (If-Else)
Use if else statements when there are several options to
choose from.
if (radNextDay.Checked == true)
decShipping = cdecNEXT_DAY_SHIPPING_RATE;
else
decShipping = cdecTHREE_DAY_SHIPPING_RATE;
CIS162AD 11
Flowchart – If-Else Statement
User selects Shipping
True
If Next Day Shipping = Next Day
False
Output Shipping
CIS162AD 12
Condition is an Expression
Conditions evaluate to true or false.
if (condition)
true – 1 or more statements
else
false – 1 or more statements
CIS162AD 13
Block of code – Compound Statements
Use braces to create a block of statements.
Single statements do NOT require braces.
if (intQuantity >= 1)
if (decPrice >= 5.00M)
decSubtotal = intQty * decPrice;
else
{
MessageBox.Show(“Invalid price entered”);
txtPrice.Focus();
txtPrice.SelectAll();
}
else
{
MessageBox.Show(“Invalid quantity entered”);
txtQuantity.Focus();
txtQuantity.SelectAll();
}
CIS162AD 16
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.
CIS162AD 17
Selection Control (else if)
Multiway Branching
Compare each condition from top to bottom, block of code
for 1st true condition is executed, and then skip to the
statement following the if-else statements.
if (radNextDay.Checked == true)
decShipping = cdecNEXT_DAY_SHIPPING_RATE;
else if (radThreeDay.Checked == true)
decShipping = cdecTHREE_DAY_SHIPPING_RATE;
else
decShipping = cdecGROUND_SHIPPING_RATE;
CIS162AD 18
Relational Operators
== Equal to
!= Not Equal to
< Less than
> Greater than
<= Less than or equal to
>= Greater than or equal to
CIS162AD 19
Assignment (=) vs Comparison (==)
if (x = 12) // may generate syntax error.
if (x == 12) // comparison
CIS162AD 20
Logical Operators
Compound or Complex Conditions can be form
using Logical Operators.
CIS162AD 21
&& - And Logical Operator
Both conditions must be true in order for the entire
condition to be true.
CIS162AD 23
| | - 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.
CIS162AD 24
Pipe Character - |
Where is the pipe character on the keyboard?
On most keyboards
– It is right above the Enter key
– Shares the key with the back slash - \
– Must hold the shift key go get it
– Instead of a solid line, it is shown as a broken line
For the Or operator,
2 pipe characters must be entered - | |.
For the And operator,
2 ampersands characters must be entered - &&.
CIS162AD 25
Short-Circuit Comparisons
In a compound comparison, if the result (True or False)
can be determined before evaluating the entire Boolean
Expression. This improves performance.
And – The 2nd condition is not evaluated if the 1st
evaluates to False – the result of an entire evaluation of an
And would be False anyway.
Or - The 2nd condition is not evaluated if the 1st evaluates
to True – the result of an entire evaluation of an Or would
be True anyway.
Using single operators (& or | ) forces the second
condition to be tested. This is only needed if the condition
includes a calculation. If we needed intCount incremented
each time, this would be the correct syntax so that the 2nd
condition is processed.
(intAmount > 0 & intCount++ < intLimit)
CIS162AD 26
! – 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 ! (intQty = = 50)
true intQty < > 50
else
false intQty = 50
CIS162AD 27
Practice Exercises
count =0 limit = 10 min=5
CIS162AD 28
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 (radNextDay.Checked = = true)
CIS162AD 29
Comparing Strings
String variables, properties, or literals can also
be compared.
Only (==) and (!=) can be used with strings.
Can use CompareTo 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.
CIS162AD 30
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
CIS162AD 31
ToUpper and ToLower Methods
Use ToUpper and ToLower methods of the
String class to convert strings for comparisons.
if txtState.Text.ToUpper( ) = = “CA”
decSalesTax = decSubtotal * cdecTAX_RATE;
CIS162AD 35
Switch Expression
switch (expression)
{
case 1: …
case 2: …
}
CIS162AD 36
4. Repetition Control (loops)
Loops are covered later in the term.
CIS162AD 37
Summary
Flow Control Structures
Flowcharts
If, If-Else
Nested if’s
Switch statement
CIS162AD 38