Unit IV (Selection / If statements /conditional execution)

CS 1130 Visual Basic—Fall 2016

Day 13 — Unit IV Intro

Logistics

See Unit III notes

Selection

At the start of the course, I mentioned the basics of computing—actions, sequence, selection, iteration, and modularization. All but the first are ways of organizing the actions. Programming is figuring out what actions are needed and then organizing them in a way that accomplishes our overall task.

So, what does today's topic, "selection" look like. You are familiar already with the idea of if something is true, do something. That is selection. In programming we have an If statement; actually we have several variations of the If statement. The two basic forms are:

	' actions required before the IF

	If  true  Then
	    ' set of actions
	End If
	
	' actions required after the IF

and

	' actions required before the IF

	If  true  Then
	    ' set of actions
	Else
	    ' alternate set of actions
	End If
	
	' actions to occur after the IF

Questions? ... Let's explore the various parts and see what we can deduce about the simple If statement.

That is how the if statement works. Questions? Comments? Wonderings?

There is one particular way of using if statements that you should be aware of—as a flag. Sometimes there are multiple occasions to test a condition and any or all of the tests could determine the action to be taken when all tests are completed. The pattern here is something like the following.

	Dim okay As Boolean = True

	...
	
	If  <bad test 1>  Then
	    okay = False
	End If
	
	...
	
	If  <bad test 2>  Then
	    okay = False
	End If
	
	.
	.
	.
	
	If okay Then
		' do okay action
	else
		' do NOT okay action
	End IF

Examples

Next, we will look at slightly more complex examples and alternatives for doing them. Do you have any examples you'd like to explore? (when done or otherwise) Let's look at an example involving a student asking about their grade (its a silly example, but one we are familiar with). Even a simple problem like get a score from the user and determine the grade involves some thinking in order to code it correctly. Some considerations are:

So a simple IF statement could check to see if the user entered a number for a score. Typically we want to do something with the score once we know the input is okay. That would go in the ELSE part of the IF we did above. Ask: What would we want to do? What (if any) data is needed to accomplish that? Do we need to initialize the data or do anything else before the If statement? Where/When do we report (inside the If, after the If, ...?

Here is some example code—what was done in class may differ.

Next Time

Day 14 — questioning RE Unit IV Practice;   CD IIIa

Logistics

Unit IV Questions

(at ≈ 3:10) Unit III Competency Demo

Next Time

Day 15 — questioning RE Unit IV Practice;   CD IIIa

Logistics

Review Unit III Competency Demo

Unit IV Questions

Day 16 — questioning RE Unit IV Practice;   CD IIIa

Logistics

Unit IV Questions

In lieu of you sharing your work on these practice items, I want to address any and all that you have questions on. You still need to put any code you see into your project and check it out.

Next Time

Day 17 — Unit IV Work

Logistics

Unit IV Questions

Project Discussion

Competency Demo IV

Any questions/wonderings before we start?

Next Time