Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT.

Slides:



Advertisements
Similar presentations
Programming with App Inventor Computing Institute for K-12 Teachers Summer 2012 Workshop.
Advertisements

5.04 Apply Decision Making Structures
1.
Objectives Understand the software development lifecycle Perform calculations Use decision structures Perform data validation Use logical operators Use.
Microsoft Visual Basic: Reloaded Chapter Five More on the Selection Structure.
Chapter 8 and 9 Review: Logical Functions and Control Structures Introduction to MATLAB 7 Engineering 161.
Conditional Statements If these were easy then everyone would them!
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CSC110 Fall Chapter 5: Decision Visual Basic.NET.
Chapter 4 Making Decisions
IS 1181 IS 118 Introduction to Development Tools VB Chapter 03.
Slide 1 Chapter 2 Visual Basic Interface. Slide 2 Chapter 2 Windows GUI  A GUI is a graphical user interface.  The interface is what appears on the.
Visual Basic Chapter 1 Mr. Wangler.
Microsoft Visual Basic 2005 CHAPTER 5 Mobile Applications Using Decision Structures.
Microsoft Visual Basic 2008: Reloaded Fourth Edition
Chapter 4: The Selection Structure
University of Toronto at Scarborough © Andria Hunter, Kersti Wain-Bantin CSCA01 VBA-3 1 Lecture Outline Variable Scope Calling another subprogram Programming.
© 2006 Lawrenceville Press Slide 1 Chapter 3 Visual Basic Interface.
Chapter 4: The Selection Process in Visual Basic.
Programming Fundamentals. Today’s lecture Decisions If else …… Switch Conditional Operators Logical Operators.
Lecture Set 5 Control Structures Part A - Decisions Structures.
Copyright © 2001 by Wiley. All rights reserved. Chapter 4: The Selection Process in Visual Basic Selection Process Two Alternative Structure If..Then..ElseIf.
Copyright © 2012 Pearson Education, Inc. Chapter 4: Making Decisions.
CMPS 1371 Introduction to Computing for Engineers CONDITIONAL STATEMENTS.
Input Textboxes Input Boxes Different than textboxes Good for small amount of input (form full of textboxes is not nice) X = Inputbox(“prompt message”,
1 Boolean Expressions to Make Comparisons Boolean expression –Represents only one of two states –Expression evaluates to either true or false Expressions.
CHAPTER FIVE Specifying Alternate Courses of Action: Selection Statements.
1 COMS 261 Computer Science I Title: C++ Fundamentals Date: September 21, 2005 Lecture Number: 10.
Copyright 2003 Scott/Jones Publishing Making Decisions.
22/11/ Selection If selection construct.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
Practical Programming COMP153-08S Week 5 Lecture 1: Screen Design Subroutines and Functions.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions.
JavaScript, Fourth Edition
Lesson Two: Everything You Need to Know
Chapter 3 Control Structures. The If…Then Statement The If…Then statement is a Decision statement = that executes a set of statements when a condition.
Visual Basic 2010 How to Program © by Pearson Education, Inc. All Rights Reserved.1.
31/01/ Selection If selection construct.
Chapter 3 Decisions Three control structures Algorithms Pseudocode Flowcharts If…then …else Nested if statements Code blocks { } multi statement blocks.
Copyright © Don Kussee 1120-Ch2 #531 CNS 1120 Chapter 2 Project Structure and Tools 1120-Ch2.PPT.
Decisions with Select Case and Strings Chapter 4 Part 2.
A First Book of C++ Chapter 4 Selection. Objectives In this chapter, you will learn about: –Relational Expressions –The if-else Statement –Nested if Statements.
Controlling Program Flow with Decision Structures.
Visual Basic Review LBS 126. VB programming Project Form 1Form 2Form 3 Text boxButton Picture box Objects Text box Button Objects.
Microsoft Visual Basic 2012 CHAPTER FIVE Decision Structures.
Copyright © 2015, 2012, 2009 Pearson Education, Inc., Publishing as Addison-Wesley All rights reserved. Chapter 4: Making Decisions 1.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Visual Basic.NET Comprehensive Concepts and Techniques Chapter 5 Decision Making.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
4 - Conditional Control Structures CHAPTER 4. Introduction A Program is usually not limited to a linear sequence of instructions. In real life, a programme.
Chapter 4.  Variables – named memory location that stores a value.  Variables allows the use of meaningful names which makes the code easier to read.
 By the end of this section you should be able to: ◦ Differentiate between sequence, selection, and repetition structure. ◦ Differentiae between single,
Computer Science Up Down Controls, Decisions and Random Numbers.
A First Book of C++ Chapter 4 Selection.
Copyright © 2007 Pearson Education, Inc. Publishing as Pearson Addison-Wesley Extended Prelude to Programming Concepts & Design, 3/e by Stewart Venit and.
Chapter 4: Decisions and Conditions
Project Structure and Tools 1120-Ch2.PPT
Chapter 4: Making Decisions.
CHAPTER FIVE Decision Structures.
Chapter 4: Making Decisions.
3rd prep. – 2nd Term MOE Book Questions.
Microsoft Visual Basic 2005 BASICS
More Selections BIS1523 – Lecture 9.
Items, Group Boxes, Check Boxes & Radio Buttons
Chapter 3: Selection Structures: Making Decisions
Boolean Expressions to Make Comparisons
Chapter 3: Selection Structures: Making Decisions
CHAPTER FOUR VARIABLES AND CONSTANTS
Introduction to Computer Programming IT-104
Presentation transcript:

Copyright © Don Kussee 1410-Ch5 #1031 CNS 1120 Chapter 5 Selection statements 1120-Ch5.PPT

Copyright © Don Kussee 1410-Ch5 #1032 Programming concepts There are 5 programming concepts –Input InputBox( ), TextBox –Output MsgBox( ), Label –Computing * ^ / \ MOD –Selection If - Then, Select Case –Loop

Copyright © Don Kussee 1410-Ch5 #1033 Selection Code Allows for the use of different code -- depending on some condition. Most programs include different paths for changeable conditions Two programming methods are used –If (test condition) Then –Select Case

Copyright © Don Kussee 1410-Ch5 #1034 If (test) Then code Else code End If True False VB code If( ) VB code Code before If section Code after If section

Copyright © Don Kussee 1410-Ch5 #1035 If two code choices are required Based on one test condition, if test is true then one section of code, if test is false then use a different code section If ( age < 18) Then MsgBox(“Child”) ‘ If test is true Else MsgBox(“Adult”)‘ If test is false End If

Copyright © Don Kussee 1410-Ch5 #1036 If (test) Then...Else… End If If (age > 6) Then Beep MsgBox(“Go to school”) ‘TEST TRUE other VB code Else MsgBox( “No school yet”) ‘TEST FALSE other VB code End If

Copyright © Don Kussee 1410-Ch5 #1037 Scope of If Then...Else… End If Note that the code is indented to assist the human to read the code (Computer ignores) If (test) Then VB Code ‘ Four space indent more VB code End If ‘ End of the If - Outdent VB Code ‘ Code runs if test is True or False

Copyright © Don Kussee 1410-Ch5 #1038 If one code choice is required One test condition, if test true then one section of code, if false then no additional code is run. If ( hours > 40 ) Then ‘ If test is true MsgBox(“overtime = ”, hours - 40) End If VB Code ‘ jumps to here if test is false ‘ Also runs here if true

Copyright © Don Kussee 1410-Ch5 #1039 If more than two code choices are required Multiple test conditions, for each test a true runs a section of code, a false leads to the next test condition - Order critical If ( Test1) Then ElseIf ( Test2 ) Then ElseIf ( Test3 ) Then End If

Copyright © Don Kussee 1410-Ch5 #10310 If (t) Then…ElseIf(t)…Else... If (age = 6) Then MsgBox( “1 st grade”) ‘ Note indenting ElseIf (age = 7) Then ‘ElseIf one word MsgBox( “2 nd grade”) ElseIf (age = 8) Then MsgBox( “3 rd grade”) Else ‘Only if all tests are false MsgBox( “Not in grades 1, 2 or 3”) End If

Copyright © Don Kussee 1410-Ch5 #10311 If(t)Then…ElseIf(t)…End If VB code If() VB code true false Without else

Copyright © Don Kussee 1410-Ch5 #10312 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false Without else

Copyright © Don Kussee 1410-Ch5 #10313 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false Without else

Copyright © Don Kussee 1410-Ch5 #10314 If(t)Then…ElseIf(t)…End If VB code If() VB code true false (Else)

Copyright © Don Kussee 1410-Ch5 #10315 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false (Else)

Copyright © Don Kussee 1410-Ch5 #10316 If(t)Then…ElseIf(t)…End If VB code If() ElseIf() VB code true false (Else)

Copyright © Don Kussee 1410-Ch5 #10317 Unlimited number of ElseIf (tests) allowed Else is also allow, but not required –runs only if all If(test) & ElseIf (test) are false Code stops when the next ElseIf is hit. Program continues after End If

Copyright © Don Kussee 1410-Ch5 #10318 If (test) Then code If( ) VB code True False

Copyright © Don Kussee 1410-Ch5 #10319 One choice - only one line of code - Caution If (score >60 ) Then MsgBox( “Pass”) Just one statement, not a block of code Caution - easy to forget & add a line that won’t be subject to the if (test) Recommend always include an End If to indicate the end of the If - End If If (score >99) Then MsgBox( “Pass”) End If

Copyright © Don Kussee 1410-Ch5 #10320 Comparison or Relational operators - used for tests = equal to > greater than < less than >= greater than or equal to <= less than or equal to <> not equal to

Copyright © Don Kussee 1410-Ch5 #10321 Numerical comparisons Based upon the algebraic values of the numbers The sign as well as the magnitude is important. ? 5 > 6 ? 5 > -6 ? -5 > -6

If (t) Then code Else code End If True False VB code If( ) VB code

Copyright © Don Kussee 1410-Ch5 #10323 Nested or Embedded If( ) Within each If (test is true) VB code –Additional If … Then can be built –Different from ElseIf structure –Different from Combined test conditions (And) If (In school district = yes) If (age = 6) If (vaccinations = done) If ( class size < max class size)

Nested If Then Else Statements

Copyright © Don Kussee 1410-Ch5 #10325 Nested If Then Else Statements If a given condition must be tested only when a previous condition has been tested true & 1. alternative actions are required for one or more of the conditions then nest. 2. one or more statements are to be executed before or after the second If-Then-Else logic structure, then a nested If-Then-Else is required.

Nested If Then Else Statements

Copyright © Don Kussee 1410-Ch5 #10333 Nested If structure If (test condition 1) Then Statement code block 1 ElseIf (test condition 2) Then If( test condition 3) Then Statement code block 2 If (test condition 4) Then Statement code block 4 If (test condition 5) Then Statement code block 5 ElseIf (test condition 6) Then Statement code block 6 End If ElseIf (test condition 7) Then Statement code block 7 End If

Copyright © Don Kussee 1410-Ch5 #10334 Nested If syntax Indent the nested ifs to make them easy to read and understand Caution - the computer ignores indenting Else, ElseIf, End If - belong to the next upward unattached If.

Copyright © Don Kussee 1410-Ch5 #10335 Nested If structure If (test condition 1) Then Statement code block 1 ElseIf (test condition 2) Then If( test condition 3) Statement code block 2 If (test condition 4) Statement code block 4 If (test condition 5) Statement code block 5 ElseIf (test condition 6) Then Statement code block 6 End If ElseIf (test condition 7) Then Statement code block 7 End If

Copyright © Don Kussee 1410-Ch5 #10336 Examples of If (t)…ElseIf(t) Grades:>90 A, >80 B, >70 C, >60 D, <60 E Speeding Fines:> 30 miles over limit $500, >20 $200, >10 $100, <10 warning Commission:>$100.2%,>$200.3,>500.4 Bussing: 1 Buss, in between… bussed when snow Or < 20 o F Golf: Walk 9 holes Or >80 o F Or <60 o F Or rain threat Or client Or feels rich Or feels tired Or hurried

Copyright © Don Kussee 1410-Ch5 #10337 Option Button (Radio Button) Control opt Input control, allows one of several choices Programmer defines choices One choice is required More than one choice not allowed Unlimited number of buttons allowed Mouse or Tab key will move to next button Like the old push-button car radios

Copyright © Don Kussee 1410-Ch5 #10338 Option Button Properties Caption - Text appears by button Style - 0 = Text, 1 = Text & Graphic Value = True or False Option buttons are group controls - All option buttons effect each other, click on one - un-clicks all other buttons - Usually an array of Option Buttons

Copyright © Don Kussee 1410-Ch5 #10339 Frame Control fra Separate controls into groups of Objects. Control that groups Option Buttons so that interactions are isolated to that group Default button placement is on the form Frame must be placed first, then the buttons placed on the frame. Buttons can not be moved onto a frame. Cut & paste works. Events are seldom used

Copyright © Don Kussee 1410-Ch5 #10340 Frame Control Properties Caption - Text at top left of frame. Enabled - Frames & buttons are grayed Visible - Frame & buttons not displayed Layers of Form & controls Container control

Copyright © Don Kussee 1410-Ch5 #10341 Two frames to Group Option Buttons

Copyright © Don Kussee 1410-Ch5 #10342 Check Box Control chk Input control, allows many choices Programmer defines choices No choice is required More than one choice is allowed Unlimited number of buttons allowed Mouse or Tab key will move to next button

Copyright © Don Kussee 1410-Ch5 #10343 Check Box Properties Caption - Text appears by button Style - 0 = Text, 1 = Text & Graphic Value = True or False or Disabled Check box are not group controls Often an array of Option Buttons

Copyright © Don Kussee 1410-Ch5 #10344 Income tax program TaskObjectEvent Task Object Event Income txt Number of dependents chk Use programs tax computation opt Click Compute about owed cmd Click Amount of tax prepayment msb OK Display difference lbl

Copyright © Don Kussee 1410-Ch5 #10345 Select Case Statement One test with multiple possible outputs Similar to If … ElseIf … ElseIf … End If Unlimited number of outcomes Case Else available if all others False Selected code runs until next Case reached Program continues after the End Select Numeric or String can be test expression

Copyright © Don Kussee 1410-Ch5 #10346 Select Case syntax Select Case test_expression ‘note indenting Case possible_result 1 Code statements for condition 1 Case possible_result 2 Code statements for condition 2 Case Else Code statements for Else condition End Select

Copyright © Don Kussee 1410-Ch5 #10347 Select Case possible results Case Is = “Feb” ‘exact string match Case Is = 4 ‘ exact number match Case “Jan”, “Mar”,”May”,”Jul”,”Aug” Case Is 10 to 21, 22, “Sop” ‘range of values Case Is >= 23 ‘comparison operator Case Is <> 6 ‘comparison operator Case Else - only if all other tests are False

Copyright © Don Kussee 1410-Ch5 #10348 Select Case Vs If ElseIf One Value tested Many result conditions Easier to read Easier to write Order not important Different values tested Few result conditions Harder to read Harder to write More prone to contain errors Order important

Copyright © Don Kussee 1410-Ch5 #10349 Select Case Selected code –Runs until the next Case Statement –Can include multiple code statements –Can include additional Select Case structures –Can include If… ElseIf … End If code Statement order not critical in Select Case Statement order critical in If…ElseIf…End

Copyright © Don Kussee 1410-Ch5 #10350 Exit Sub Statement Allows the Sub-routine to be exited early. Considered poor programming (subs should have only one entrance and one exit point) Often connected to error conditions, with some correction ability (MsgBox ( )) or the ability to cancel the program.

Copyright © Don Kussee 1410-Ch5 #10351 Chapter 5 keywords Check Box Control Option button controls Condition Outcome Statement block Decision tree Frame control If…Else… End If If…ElseIf… End If Select Case Nested/Embedded If’s Exit