C# Decisions - Lessons

 

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.

The IF Statement

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 and it branches a different way if the Boolean statement is false. 

Nesting Decision Structures

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.

IF/ELSE/IF

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.

Logical Operators

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

Preventing Data Conversion Exceptions (TryParse)

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.

Input Validation (Range Checking)

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.

The SWITCH Statement

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.

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. 

Radio Buttons

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.

Check Box

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.

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. 

String Comparisons

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.