460 likes | 570 Views
MIS 3200 – Unit 3. What can computers do Follow a sequence of instructions Follow different paths of instructions based on decisions Do things over and over again – loops This unit focuses mainly on decisions dealing with conditions if statement nesting if statements and / or
E N D
MIS 3200 – Unit 3 • What can computers do • Follow a sequence of instructions • Follow different paths of instructions based on decisions • Do things over and over again – loops • This unit focuses mainly on decisions • dealing with conditions • if statement • nesting if statements • and / or • shortcut operators
Basic program structures • Computers are good at doing a series of operations one after the other • One way to view this is shown here • The sequence has a starting point • There are one or more steps • Any number of steps is possible • There is a stopping point • For example • The click event methods you have been working with have a sequence of steps that start with the open { and end with the closing } • The instructions within a method are always executed top to bottom (or start to stop like you see above)
REALLY IMPORTANT! • Common Errors • Using variables before they are declared • Using the value of a variable before it has been assigned • Outputting final results before they are calculated • Normal processing starts at the top and moves towards the bottom – ALWAYS - the only “exception” is when you are in a loop!!
Program structures 2 • Computers need to make decisions • They do that by testing conditions • Is this person over 18? • Is this sale taking place in Ohio? • Is this package going to shipping zones 5 or 6? • Did the Bobcats beat the Miami? • Is this employee eligible for overtime pay and if they are did they work enough hours to earn it? • Is this student and MIS major or a Finance major or a Marketing major?
Decisions 2 • Some decisions are one-sided – if the condition is true do something (B), if not just skip to the next thing (C) • One way to show this is • If the condition is true • Do the things at B • Once B is complete move to C • If the condition is false • Go directly to C TRUE or FALSE: regardless of the condition, will you always end up at C?
Decisions 3 • Some decisions are two-sided • Do one thing if the condition is true (B) • Do something different (C) if the condition is false • TRUE or FALSE: • Regardless of which branch you take, will you always end up at (D)?
Program structures 3 • Computers need to do some things over and over in a loop(the technical term is iteration) • There are several ways to create loops but the most common one looks like this • test a condition • if the condition is true, do the things at B • once finished with B go back and test the condition again • if the condition is true go to B, if not, go to C What do you think will happen if the condition is false from the beginning?
Decision making in C# • The most common structure in C# of making decisions is the if statement • One sided decision if • Two sided decision if else
Conditions • Conditions, or logical expressions, have only two possible value: true and false • Can be as simple as a Boolean variable (true/false) • Can be a relational expression !!
Relational operators • There are six types of relations that can be tested
Relational expressions • Only have two possible values: true or false
Two-sided if statement • Sometimes you want to do one thing if a condition is true and something different if it is false
Nested if • Sometimes we need to consider two or more conditions before we can act • One approach • Put one if statement inside another so you only look at it if the first one is true • The if statement inside begins before the oen outside • This is called nesting and the second if statement can be on either the true or false side of the first if statement
Nested if - example You only get to this part if the employee is an Hourly employee If the employee is not Hourly, you immediately skip to here
Another approach • Nested if statements are often used when we need to test two or more conditions at the same time. Sometimes we need • All conditions to be true before we do something • Any one of several conditions to be true before we do something • C# provides a way to test more than one condition in a single if statement
Multiple conditions, all true • && which is called an AND operation • Do something ONLY if all conditions are true • Don’t do it if ANY condition is false • For example, and employee is an hourly employee AND has worked more than 40 hours • In C# the AND operation is represented by && Both sides of the && operator must be logical conditions – they must have or produce a value of either true or false
Multiple conditions, at least one of which is true • || which is called an OR operation • Do something if one or more of the conditions is true • Don’t do it if ALL conditions are false • For example, give a discount to a young person or an older person • In C# the OR operation is indicated by ||(note, this symbol is normally found over the \ key on most keyboards) Both sides of the || operator must be logical conditions – they must have or produce a value of either true or false
Web Controls for Decisions LIST CONTROLS • CheckBoxList • DropDownList • ListBox • RadioButtonList Some are lists, while others are not! NON- LIST CONTROLS • CheckBox • RadioButton
Handling different controls LIST • Use SelectedIndexChanged for method name • Use .SelectedValuein the if statement • Used with CBL & RBL • Values stored in Items NON-LIST • Use CheckChangedfor method name • Use .Checked in the if statement • Used with checkboxes • Values stored in .Text
RadioButtonList: Adding • Similar to RadioButtons but easier to use when you have multiple choices • Choices are represented by a series of ListItems • ListItems are created using the EditItems… option of the smart menu New RadioButtonList Smart menu button RadioButtonList with expanded smart menu – click on Edit Items…
RadioButtonList: Editing Completed RadioButtonList with its RepeatDirection property set to Vertical New ListItem with Text and Value set Click to add a new ListItem The Text property is what the user sees Completed RadioButtonList with its RepeatDirection property set to Horizontal The Value property is what the program sees and and the value used in decisions
RadioButtonList: Processing • There is only one event, a SelectedIndexChanged event, for the entire RadioButtonList • Once the event fires there are several ways to determine which button is clicked
Shortcut operators Shortcut operator are of two types • Accumulation • A value is added to the value & stored in another variable • And the result is stored back in the same variable • Moving by one value • When the new value only changes by ONE (+1 or -1)
Accumulation It works for string concatenation (i.e. accumulation) too!
Adding or Subtracting by ONE • This is a simplification of Accumulation intCnt +=1; intCnt++; intCnt-=1; intCnt--;
Try it – L1 • This exercise will give you a chance to use if statements and test different conditions • Be sure ASPPub is on your desktop, open VWD and open ASPPub as your website • Select Unit3 under MIS3200 and add a new web page called yourLastNameU3L1.aspx • Click in the MainContent area and press Enter several times • In the top paragraph type UNIT 3 – L1 – USING IF STATEMENTS If you have problems with any of this, please refer back to L1 instructions in previous units.
L1 #2 • Convert the text to an h2 heading and make it have white letters on a dark green background • Add a table to the second paragraph. The table should have 8 rows and 3 columns • In the first row • first column, type Enter a state abbreviation • Drop a TextBox immediately after the text • Change the (ID) to txtState • Drop a button in the second column • Name the (ID) to btnState • Change the Text to Test State
L1 #3 • Drop a Label in the third column • Change the (ID) to lblState • Clear the Text • Double-click the Test State button • Add appropriate method level comments • Add the following code within the button’s method {} • Run the page and test with different values in the text box
L1 #4 • In the second row, first column • Type Enter a negative whole number • Drop a TextBox after the words • Change its (ID) to txtNegative • Drop a RequiredFieldValidator after the TextBox • Change the (ID) to rfvNegativeNumber • Change the ControlToValidate to txtNegative • Change the ErrorMessage to The negative number is required • Change the Text to * • Change the ForeColor to Red • Change the ValidationGroup to group1 Note: Validation controls are normally in a single group. If you want to have a few controls work together you put them in a ValidationGroup much like can place radio buttons in a group.
L1 #5 • Drop a CompareValidator after the RequiredFieldValidator • Set the (ID) to cvNegative • Set the ControlToValidate to txtNegative • Set ErrorMessage to The negative number must be a whole number • Set the Text to * • Set ForeColor to Red • Set Operator to DataTypeCheck • Set Type on Integer • Set ValidationGroup to group1 • Drop a ValidationSummary after the CompareValidator • Set the (ID) to vsNegative • Set ForeColor to Red • Set ShowMessageBox to true • Set ShowSummary to false • Set ValidationGroup to group1
L1 #6 • In the second column of the second row • Drop a Button • Set the (ID) to btnNegative • Set Text to Test Number • Set ValidationGroup to group1 • In the third column of the second row • Drop a Label • Set the (ID) to lblNegative • Clear the text • Double-click the Test Number button • Convert the contents of txtNegative to an integer • Test to see if the value is negative and display a message (see next slide) RelatedValidators, ValidationSummaries and Buttons all have to be in the same ValidationGroup
L1 #7 • Add appropriate method level comments • Run the page and test to be sure everything is working
L1 #8 • In the first column of the third row • Type Enter a number that is less than 18 or greater than 64 • Drop a TextBox after the message • Set the (ID) to txtOrTest • In the middle column of the third row • Drop a Button • Set the (ID) to btnOrTest • Set the Text to Test Number • In the third column • Drop a Label • Set the (ID) to lblOrTest • Clear the Text • (NOTE: you would normally use Validators here to assure good data but we are omitting them for now to save time. Just remember that L3 assignments always require validation!)
L1 #9 • Double-click the Test Number button in this row • Enter appropriate method level comments • Convert the context of txtOrTest to an integer • Test it to see if it meets the criteria and display a message • In this example we have two disconnected values that are acceptable, either less than 18 OR greater than 64, so the test we need to use is the OR operator • Verify - run the page and test to be sure you get the correct results
L1 #10 • In the first column of the fourth row • Type Enter a number that is between 18 and 64 inclusive • Drop a TextBox after the message • Set the (ID) to txtAndTest • In the middle column of the fourth row • Drop a Button • Set the (ID) to btnAndTest • Set the Text to Test Number • In the third column • Drop a Label • Set the (ID) to lblAndTest • Clear the Text
L1 #11 • Double-click the Test Number button in this row • Enter appropriate method level comments • Convert the context of txtAndTest to an integer • Test it to see if it meets the criteria and display a message • In this example we have a continuous range of acceptable values so the test we need to use is the AND operator • Run the page and test to be sure you get the correct results
L1 #12 • Double check to be sure everything works • Open your MIS3200 home page and and a link to this L1 assignment • Copy ASPPub back to your ASPNET account and submit your MIS Portfolio public URL to the Unit 3.1 L1 drop box.
L2 • Open ASPPub • Copy your xU3L1.aspx file, paste it back in the Unit3 directory and rename it yourLastNameU3L2.aspx • In the first column of row 5 of the new file • Drop a CheckBox • Set the (ID) to ckbAge • Set AutoPostBack to true • Set the Text to 18 and over • Skip the the right-most column • Drop a Label • Change (ID) to lblAge • Clear the Text We will talk about this more during the next class, but the AutoPostBack property needs to be set to true if we want to process the CheckBox as soon as its value changes
L2 #2 • Double-click the CheckBox • This method needs to see if the CheckBox is checked and output an appropriate message
L2 #3 • In the first column of row 6 • Type Gender with Radio Buttons • Drop a RadioButton after the text • Set (ID) to rbMale • Set Text to Male • Set AutoPostBack to true and set the GroupName to gender • Drop another RadioButton after the first one • Set (ID) to rbFemale • Set Text to Female • Set AutoPostBack to true and set the GroupName to gender • In the right-most column of row 6 • Drop a Label • Set (ID) to lblGender1 • Clear the Text
L2 #4 • Double-click rbMale • Enter code to determine if the button is checked and display an appropriate message it if is • Double-click rbFemale • Enter code to determine if the button is checked and display an appropriate message it if is • Run the page and test to be sure the new row works correctly (Notice any weird behavior? How could we address it? This is typically why we will use RadioButtonLists instead of RadioButtons)
L2 #5 • In the left-most column of row seven • Type Gender with a Radio Button List • Drop a RadioButtonList after the text • Set (ID) to rblGender • Set AutoPostBack to True • Click the smart menu (see slide 20)and select Edit Items… • Add an item with Text = Male and Value = Male and • Add a 2nd item with Text = Female and Value = Female • In the right-most column of row seven • Drop a Label • Set (ID) to lblGender2 • Clear the Text
L2 #6 • Double-click the RadioButtonList • Write code to see which button is selected and output an appropriate message • Run the page and test to see that the new code works properly (did we really need to nest the 2nd if statement? In this case, no,
L2 #7 • In the first cell of row eight • Drop a Button • Set (ID) to btnClearEverything • Set Text to Clear Everything • Double-click the button • Write code that clears the Text of all the Labels and TextBoxes • Write code to set the Checked property of all CheckBoxes and RadioButtons to false (this unselects everything) • Write code to set the SelectedIndex property of the RadioButtonList to -1(-1 indicates that no value is selected in the list)
L2 #8 • Test everything again • Link this page to your MIS3200 homepage • Copy ASPPub back to your ASPNet account and submit your MIS Portfolio public URL to the Unit 3.1 L2 drop box
Think About It! • What does an if statement allow us to do? • What is a nested if statement? • What function is performed by these shortcut operators? • += -= *= /= • What about relational operators? • == != > >= < <= • How are increment/decrement operators used? • ++ --