Unit IV (Selection / If statements /conditional execution)

CS 1130 Visual Basic—Fall 2016

Day 12 — Unit IV Intro

Logistics

Review Unit III Competency Demo

Project Overview

This course requires a project. It is about time you started thinking about what your project might be.  [ Discuss expectations as outlined in the project description. ]    Questions? Comments? Wonderings?  

The goal of the project proposal meeting is to get you to thinking about what you want to do and planning as precisely as you can what that will be. I suggest you think about what you want to do and then examine my example proposal for a pizza ordering app  [ Show and discuss the sample proposal. ]    Questions? Comments? Wonderings?  

.

Next Time

Day 13 — Work & Questioning RE Unit IV Practice

Logistics

Selection

At the start of the course, I mentioned the basics of computing—data & actions and the basic ways of organizing actions—sequence, selection, repetition, and modularization. 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 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. 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.

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

If Statements and Complex Boolean Expressions

Our experience with conditionals suggested (I hope) that it is possible to construct quite complex conditional expressions. Once we do, the If statement can be quite simple. However, the complexity can be reversed. A single If statement with a complex conditional can be replaced by a set of (perhaps interconnected) If statements with simple conditionals. One example is leap year.

A leap is a year whose number is divisible by 4 but if it is divisible by 100, it must also be divisible by 400 or is it not a leap year. A single conditional that tests for a leap year is:

    (year Mod 4 = 0) And ((year Mod 100 <> 0) Or ((year Mod 100 = 0) And (year Mod 400 = 0))) 

We could use that expression to construct a simple If statement to test a year value for being a leap year, e.g.,

    If (year Mod 4 = 0) And _
           ((year Mod 100 <> 0) Or ((year Mod 100 = 0) And (year Mod 400 = 0))) Then
        MessageBox.Show(year.ToString & " is a leap year!"
    Else
        MessageBox.Show(year.ToString & " is NOT a leap year!"
    End If

Alternatively, we could construct a more complex If statement that uses simple Boolean expressions in each If, e.g.,

    Dim isLeapYear As Boolean
    If year Mod 4 = 0 Then
        If year Mod 100 <> 0 Then  '(we know year is divisible by 4)
            isLeapYear = True
        Else
            If year Mod 400 = 0 Then     '(we know year is divisible by 100)
                isLeapYear = True
            Else
                isLeapYear = False
            End If
        End If
    Else
        isLeapYear = False                '(we know year is NOT divisibile by 4)
    End If
    If isLeapYear Then
        MessageBox.Show(year.ToString & " is a leap year!"
    Else
        MessageBox.Show(year.ToString & " is NOT a leap year!"
    End If    

In both these cases, we will need to take a substantial amount of care to get things correct. It is just a matter of which of them is easier for you to think about and do the due diligence to make sure your code is correct? Any questions/comments?

Work Time

Use the remaining time to examine/work on the Unit IV learning activity and ask any questions you might have.

Next Time