Presentation is loading. Please wait.

Presentation is loading. Please wait.

Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering.

Similar presentations


Presentation on theme: "Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering."— Presentation transcript:

1 Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering

2 2 Overview of this Module  This module introduces the most common programming structures used in Excel VBA, including –Selection  If…Then  If…Then…Else  Select Case –Repetition  For…Next  For Each…Next  Do While…Loop  Do Until…Loop  Two useful keywords typically used with programming structures are also covered –Exit and End

3 3 Overview of this Module (cont.)  In many of the programming structures covered in this module, you will need to use arithmetic and comparison operators

4 4 If…Then Statements  If…Then statements allow you to perform conditional actions –If a condition is met, a certain action (or set of actions) is performed –Otherwise, another set of actions may be performed  The general format for the If…Then statement is If condition Then action End If  Excel VBA does not terminate programming structures automatically after you have entered the first line –You have to do this yourself manually

5 5 If…Then Statements (cont.)  Basic flowcharts for If…Then statements

6 6 If…Then Statements (cont.)  The If…Then statement defines the action to be performed if the condition is false by using the keywords –Else, or –ElseIf  The ElseIf keyword helps in streamlining code that may require the use of several nested If…Then…Else statements

7 7 If…Then Statements (cont.)  Constructing If…Then statements with Else and ElseIf If x <= 1000 Then MsgBox “Your number is less than or equal to 1000.” Else If x <= 2000 Then MsgBox “Your number is greater than 1000 but less than or equal to 2000.” Else MsgBox “Your number is greater than 2000” End If

8 8 If…Then Statements (cont.)  Constructing If…Then statements with Else and ElseIf If x <= 1000 Then MsgBox “Your number is less than or equal to 1000.” ElseIf x <= 2000 Then MsgBox “Your number is greater than 1000 but less than or equal to 2000.” Else MsgBox “Your number is greater than 2000” End If

9 9 Logical Checks and Booleans  If…Then statements are also used with logical checks and Boolean variables  Logical checks include the keywords And and Or

10 10 “And” Logical Check  The And logical check requires every condition in the If…Then statement to be true in order for the (True Actions) to be performed  If…Then statement with And logical check If condition1 And condition2 And condition3 And … Then (True Actions) Else (or ElseIf) (False Actions) End If  If only one of the conditions is false, the (True Actions) will not be executed –If an Else or ElseIf statement exists, the (False Actions) will be executed instead –Otherwise, the If…Then statement will end

11 11 “Or” Logical Check  The Or logical check requires only one condition in the If…Then statement to be true in order for the (True Actions) to be performed  If…Then statement with Or logical check If condition1 Or condition2 Or condition3 Or … Then (True Actions) Else (or ElseIf) (False Actions) End If  Every condition must be false to skip the (True Actions) –If an Else or ElseIf statement exists, the (False Actions) will be executed instead –Otherwise, the If…Then statement will end

12 12 Using Boolean Variables  Boolean variables can also be used in If…Then statements –We need to check if their values are True or False  Consider the following two statements If variable = True Then action 1 End If -------------------------------- If variable = False Then action 2 End If

13 13 Using Boolean Variables (cont.)  To check if a Boolean variable is True, you can also just state the name of the variable  Instead of the first statement in the previous slide, you can just type If variable Then action 1 End If  The default Boolean value checked by Excel VBA is True –However, YOU have to assign this value to the Boolean variable in code

14 14 Select Case  The Select Case statement is used to list possible situations in which certain actions should be performed  The general structure of the Select Case statement gives a particular expression (which is to be evaluated) and a list of cases of possible values of that expression Select Case number Case 1 MsgBox “Your number is 1.” Case 2 MsgBox “Your number is 2.” End Select

15 15 Select Case (cont.)  Flowchart representation for the Select Case statement

16 16 Select Case (cont.)  We can also give a range of values as a case instance Select Case number Case 1 To 5 MsgBox “Your number is in the interval [1, 5].” Case 6 To 10 MsgBox “Your number is in the interval [6, 10].” End Select

17 17 Select Case (cont.)  There is also an optional Case Else statement which can be used to specify all other cases which are not listed Select Case number Case 1 To 5 MsgBox “Your number is in the interval [1, 5].” Case 6 To 10 MsgBox “Your number is in the interval [6, 10].” Case Else MsgBox “Your number is smaller than 1 or greater than 10.” End Select

18 18 Select Case (cont.)  You may also include conditions as cases instead of simple instances by using the Is keyword  This is useful when replacing several ElseIf statements in an If…Then structure Select Case number Case Is >= 10 MsgBox “Your number is greater than or equal to 10.” Case Else MsgBox “Your number is less than 10.” End Select

19 19 Repetition Structures  Repetition structures (or loops) are programming structures which allow you to repeat a set of actions a certain number of times  The number of times a loop is repeated can be specified by counting up to (or down to) a certain value –For…Next –For Each…Next  Or the loop can run continuously while or until a certain condition is met –Do While…Loop –Do Until…Loop

20 20 Repetition Structures (cont.)  Most loops are implemented using counter-controlled repetition  Counter-controlled repetition requires:

21 21 For…Next Loops  The For…Next and For Each…Next loops are used to repeat a loop while counting up (or down) toward a certain number –We refer to them both generally as For Loops  The counting is performed using a simple index variable –E.g., i, j, count, iteration –The data type of the counting variable must be Integer

22 22 For…Next Loop  The most common of these two structures is the For…Next loop  The structure of the For…Next loop is as follows: For counter = start To end [Step n] action(s) Next counter  The Step value specifies how much the counter variable should increase or decrease during each loop –If not specified, the default Step value is 1

23 23 For…Next Loop (cont.)  Basic flowchart for the For…Next loop

24 24 For…Next Loop (cont.)  If you want to count up towards a number, the Step value should be positive and the start value should be less than the end value For i = 1 to 10 Step 2 action(s) Next i  If you wish to count down to a number, the Step value should be negative and the start value should be greater than the end value For i = 10 to 1 Step -1 actions Next i

25 25 For Each…Next Loop  The For Each…Next loop works almost identically to the For…Next loop –The only difference is that the For Each…Next loop is generally used to manipulate objects –In each iteration of the loop, actions are performed on a new object  Example code using For Each…Next

26 26 Do Loops  There are two main types of Do Loops –Do While…Loop –Do Until…Loop  These Do Loops perform a set of actions repeatedly while or until a condition is met  There are two main structures for these loops –Specify While or Until condition before actions –Specify While or Until condition after actions

27 27 Do Loops (cont.)  Basic flowcharts for Do Loops Do While…LoopDo Until…Loop

28 28 Do Loops (cont.)  In the structure below, a While condition is considered before a set of actions is performed count = 1 Do While count < 10 actions count = count + 1 Loop  In the structure below, the set of actions are performed and then the While condition is checked before the actions are repeated count = 1 Do actions count = count + 1 Loop While count < 10

29 29 Do Loops (cont.)  Boolean variables can also be used in Do Loops –The Boolean variable becomes the condition for the Do While or Do Until loops  Do Loops that used Boolean variables usually include a nested If…Then statement which would change the value of the Boolean variable once a certain result is found Do While found = False actions If x > 100 Then found = True End If Loop

30 30 Do Loops (cont.)  Failure to provide the body of a Do While…Loop statement with an action that eventually causes the loop continuation condition to become false is a logic error  Failure to provide the body of a Do Until…Loop statement with an action that eventually causes the loop-termination condition to become true is a logic error  Such repetition statements never terminate, resulting in a logic error called an infinite loop

31 31 Exit Statements  As we develop and run longer programming structures such as nested If…Then statements and Do Loops, we may want a way to exit the current set of actions at any time  We may also want to use this option while running any sub procedure or function procedure  VBA provides several Exit Statements which allow current actions to stop –Program control is transferred to  Other statement within the same sub procedure  A different sub procedure  Program terminates

32 32 Exiting Procedures  To exit a procedure, we use either Exit Sub or Exit Function –The choice depends on whether we are currently running a sub procedure or a function procedure  When the Exit Sub statement is used, the sub procedure will stop execution  When Exit Function is used, the function procedure will stop execution –The program will return to the point in the code from which the function was called

33 33 Exiting Loops  To exit a loop, we use Exit For or Exit Do –Exit For  If we are executing For…Next or For Each…Next loops –Exit Do  If we are executing Do…While or Do…Until loops  Exit For will stop executing a For…Next or For Each…Next loop and move to the next line of code after the Next statement  The Exit Do code will stop executing a Do Loop and move to the next line of code after the Loop statement

34 34 Exiting Loops (cont.)  These statements can also be used with nested loops  The exit statement will apply to the inner most loop in which it is used Exit Statement in a For…Next Loop For i = 1 to 100 For j = 1 to 50 StartCell.Offset(i, j).Value = i + j If i + j > 60 Then Exit For End If Next j Next i Exit Statement in a Do Until Loop Do Until x >100 x = x^2 If x mod 2 = 0 Then MsgBox “The number “ & x & “ is even.” Exit Do End If x = x + 1 Loop

35 35 Ending the Program  We can stop executing the entire program by using the End statement –Just as the End Sub, End Function, End If, and End With statements end the execution of the enclosed lines of code, the End statement will stop executing all code and exit all loops and procedures  This can be a useful if there is some significant condition or requirement that must be met before the program can function correctly

36 36 Summary  If…Then statements allow you to perform conditional actions –If a condition is met, a certain set of actions is performed –If it is not, another set of actions may be performed instead  Use the Select Case statement to list possible situations in which certain actions should be performed  Loops are programming structures that allow you to repeat a set of actions a certain number of times  VBA provides several Exit Statements that allow current actions to stop and moves the program to ensuing code


Download ppt "Saeed Ghanbartehrani Summer 2015 Lecture Notes #5: Programming Structures IE 212: Computational Methods for Industrial Engineering."

Similar presentations


Ads by Google