Programming in Any Language

Decision Structure Introduction

Up to now we have been doing straight line programming.  One line of code follows another.  And then it’s done.  That pretty much limits what we can do.  So, it only makes sense that we expand on that. The tools in the decision structure allow our programs to branch off in other directions.  And there is a lot of them. PDF: Slides


The IF Statement

Click to enlarge

In a decision program flows logically from the start and enters what we call a Boolean Expression.  That expression is interrogated and if TRUE the program branches to a specific block of code (or single statement). If the expression is interrogated to be FALSE then that specific block of code is ignored and program flow continues normally.


The IF/ELSE Statement

The IF/ELSE statement is similar to the IF statement. What it does is branch one way if a Boolean statement is true.

It branches a different way if the Boolean statement is false.  PDFs: Demo1; Demo2


Nesting Decision Structures

Click to enlarge

Nesting is placing code inside another piece of code.  In other words, we make one decision, and then based on that decision we make another decision. PDF: Demo


IF/ELSE/IF

Click to enlarge

Nested IF statements is just one way of asking multiple questions in a program that needs to make decisions.  Another technique is the IF/ELSE/IF statement. PDF: Demo


Logical Operators

Logical Operators allow you to connect multiple Boolean expressions together.  The operators are “And”, “Or” and “Not”.  PDF: Demo


Preventing Data Conversion Exceptions (TryParse)

Click to enlarge

Preventing Data Conversion Exceptions mean getting ahead of the users, predicting they will do something we didn’t expect.  We’ll get one step in front of the users and bulletproof our code, so it continues to work. PDSs: Demo1; Demo2; Demo3


Input Validation (Range Checking)

Click to enlarge

Sometimes data gets into a program but it’s not reasonable.  What we are trying to do is not only make sure the data is not garbage but also that the results of any operation are desirable.  We are helping the user enter meaningful data. PDFs: Demo1; Demo2; Demo3


The SWITCH Statement

Click to enlarge

The SWITCH statement is known as a multiple-alternative decision structure.  What that means is it allows you to test the value of a variable or an expression and then use that value to determine which statement or set of statements to execute. PDF: Demo


List Box Intro

The ListBox is basically an object that displays more than one item.  It’s like a super label in a way.  In other words, a collection of items can be displayed at once. 

If the collection is too large to display in its entirety a scroll bar automatically appears allowing you navigate up and down through the list.  PDF: Demo


Radio Buttons

Click for more info

Radio buttons allow users to select one choice from several possible choices.  When a user clicks on a radio button the program automatically deselects any others. When you set up a program with radio buttons you typically place them inside a group box tool or a panel. PDF: Demo


Check Box

Click for more info

Check boxes allow users to select many choices from a group of possible choices.  In fact the user can select them all! While they can be used independently, when you set up a program with check boxes you typically place them inside a group box tool or a panel just as you would the radio buttons. PDF: Demo


Boolean Variables

It’s a one-byte variable that can only be set to zero or one.  It represents true or false.  Can’t be anything else.  But it is very useful when needed. A simple “if” statement can interrogate the Boolean value and send your program down one path or another.  PDF: Demo


String Comparisons

Click to enlarge

For comparing strings we have the string.Compare method that is part of the C# language. Basically you compare two strings. One and two. If one is less than two the comparison evaluates to less than zero. If one is greater than two the comparison evaluates to greater than zero. If one is equal to zero the comparison evaluates to zero.