4 MAKING DECISIONS

4.1 Decision Structures and the if Statement 4.2 The if-else Statement 4.3 Nested Decision Structures 4.4 Logical Operators 4.5 bool Variables and Flags 4.6 Comparing Strings 4.7 Preventing Data Conversion Exceptions with the TryParse Method 4.8 Input Validation 4.9 Radio Buttons and CheckBoxes 4.10 The switch Statement 4.11 Introduction to List Boxes

4.1 DECISION STRUCTURES

A control structure is a logical design that controls the order in which statements execute A sequence structure is a set of statements that execute in the order that they appear A decision structure execute statements only under certain circumstances A specific action is performed only if a certain condition exists AKA selection structure, branch Relational Operators (6) > < >= <= == != About: bool type and relational & logical operators About: if, if-else A Simple Decision Structure: the 'if' statement
         if (boolean expression) // either true or false
         {
            Statements;
            Statements;
            etc.;
         }     
 
         
Exit Button - with confirmation(if) Code: bool & Relational & Logical Operators, Exit with 'if'  

4.2 THE 'if-else' STATEMENT

Test Average (if, w/congrats msg)   An if-else statement will execute one block of statements if true or another block if its Boolean expression is false
      if (expression)
      {
         statements;
      }
      else
      {
         statements;
      }  
      
      
Payroll with Overtime (if)

4.3 NESTED DECISION STRUCTURES

You can create nested decision structures to test more than one condition. Nested means "one inside another".
         if (salary >= 40000)
         {
            if (yearOnJob >= 2)
            {
               decisionLabel.Text = "You qualify for the load.";
            }
            else
            {
               decisionLabel.Text = "Minimum years at current " + "job not met.";
            }
         }
         else
         {
          decisionLabel.Text = "Minimum salary requirement " + "not met.";
         }
         
   
Loan Qualifier (if) The if-else-if Statement You can also create a decision structure that evaluates multiple conditions to make the final decision using the if-else-if statement In C#, the generic format is:
         if (expression)
         {
         
         }
         else if (expression)
         {
         
         }
         else if (expression)
         {
         
         }
         ...
         
         else 
         {
         
         } 

         
Grade Determination (if)

4.4 LOGICAL OPERATORS

The logical AND operator (&&) and the logical OR operator (||) allow you to connect multiple Boolean expressions to create a compound expression The logical NOT operator (!) reverses the truth of a Boolean expression The && operator
            if (temperature < 20 && minutes > 12)
            {
               MessageBox.Show("The temperature is in the danger zone.");
            }  
            
            
The || operator
            if (temperature < 20 || temperature > 100)
            {
               MessageBox.Show("The temperature is in the danger zone.");
            }  
            
            
The ! Operator
            if (!(temperature > 100))
            {
               MessageBox.Show("The is below the maximum temperature.");
            } 
 
            

4.5 'bool' VARIABLES & FLAGS

You can store the values true or false in bool variables, which are commonly used as flags A flag is a variable that signals when some condition exists in the program false - indicates the condition does not exist true - indicates the condition exists Code: bool & Relational & Logical Operators, Exit with 'if'  

4.6 COMPARING STRINGS

You can use certain relational operators and methods to compare strings. The == operator can compare two strings
         string name1 = "Mary";
         string name2 = "Mark";
         if (name1 == name2) 
         {
              
         }  
         
         
You can compare string variables with string literals, too
         if (month ! = "October") 
         {
         
         }  
         
         
The String.Compare method can also compare two strings
         string name1 = "Mary";
         string name2 = "Mark";
         if (String.Compare(name1, name2) == 0) 
         { 
         
         }  
         
         
Secret Word (if, string comparison)

4.7 PREVENTING DATA CONVERSION EXCEPTION: TryParse()

Exception should be prevented when possible The TryParse methods can prevent exceptions caused by users entering invalid data int.TryParse doubel.TryParse decimal.TryParse About TryParse The generic syntax is: int.TryParse(string, out targetVariable) The out keyword is required; it specifies that the targetVariable is an output variable
         // int.TryParse
         int number;
         if (int.TryParse(inputTextBox.Text, out number)) 
         {  
            code if data is good
         } 
         else 
         {
            error message         
         }
 
         
         //double.TryParse
         double number;
         if (double.TryParse(inputTextBox.Text, out number)) ...
         {
         
         }
         
         
         
         //decimal.TryParse
         decimal number;
         if (decimal.TryParse(inputTextBox.Text, out number)) ...
         {
         
         }
         
   
Add 2 Numbers (TryParse && TryParse) Fuel Economy (separate TryParses)  

4.8 INPUT VALIDATION

Input validation is the process of inspecting data that has been entered into a program to make sure it is valid before it is used TryParse methods check if the user enters the data, but it does not check the integrity of the data. In a program that gets test scores, we can limits its data to an integer range of 0 through 100.
         if (testScore >= 0 && testScore <=100) 
         { 
         
         } 
         else 
         {
         
         }       
         
         
Checking for numbers , and checking between 1-10 inclusive
   
       // the user's input is in txtIn
       int number;
       if( int.TryParse( txtIn.Text, out num ) )
       {
          // Check the number's range.
          if (number >= 1 && number <= 10)
          {
              MessageBox.Show("That number is acceptable.");
          }
          else
          {
              MessageBox.Show("That number is NOT acceptable.");
          }         
       }  
       
       

4.9 RADIO BUTTONS & CHECK BOXES

Radio buttons allow users to select one choice from several Clicking on a radio button, the program automatically deselects any others. This is known as mutually exclusive selection Check boxes allows users to select multiple options RadioButton Control The .NET Framework provides the RadioButton Control for you to create a group of radio buttons Properties: Text: holds the text that is displayed next to the radio button Checked: a Boolean property that determines whether the control is selected or deselected In code, use a decision structure to determine whether a RadioButton is selected.
         if (choiceRadioButton.Checked) 
         { 
         
         } 
         else 
         { 
         
         }  
         
         
You do not need to use the == operator, although the following is equivalent to the above
         if (choiceRadioButton.Checked == true) 
         { 
         
         } 
         else 
         { 
         
         } 
 
         
Radio Buttons (Checked property) CheckBox Control .NET Framework provide the CheckBox Control to give the user an option such as true/false or yes/no Properties: Text: holds the text that is displayed next to the radio button Checked: a Boolean property that determines whether the control is selected or deselected In code, use a decision structure to determine whether a CheckBox is selected.
         if (option1CheckBox.Checked) 
         { 
         
         } 
         else 
         { 
         
         } 
 
         
About: radiobutton, groupbox, checkbox The CheckedChanged Event Anything a RadioButton or a CheckBox control's Checked property changes, a CheckedChanged event is raised You can create a CheckedChanged event handler and write codes to respond to the event Double click the control in the Designer to create an empty CheckedChanged event handler similar to:
         private void yellowRadioButton_CheckedChanged(object sender, EventArgs e) 
         { 
         
         }  
          
         
Color Theme (radio button events)  

4.10 THE 'switch' STATEMENT

The switch statement lets the value of a variable or an expression determine which path of execution the program will take Can be used as an alternative to an if-else-if statement switch(testExpression) { case value_1: statements; break; case value_2: statements; break; ... case value_n: statements; break; default: statements; break; }
   switch (month)
   {
     case 1:
     MessageBox.Show("January");
     break;

     case 2:
     MessageBox.Show("February");
     break;

     case 3:
     MessageBox.Show("March");
     break;

     default:
     MessageBox.Show("Error: Invalid month");
     break;
   }  
   
   
Cards (switch, listbox selection, parameter)

4.11 INTRODUCTION TO LIST BOXES

A list box displays a list of items and allow the user to select an item from the list In C#, you can use the ListBox control to create a list box About listBoxes & loops Properties: Text: Gets or searches for the text of the currently selected item in the ListBox Items: Gets the items of the ListBox (stored as an array) Using a ListBox as Output Assignment & Arithmetic (textBox, listBox, parse, append)   More Properties: SelectedItem: Gets or sets the currently selected item in the ListBox. When the user selects an item, the item is stored in this property. You can use the following to get the selected item from a list box: selectedFruit = fruitListBox.SelectedItem.ToString(); SelectedIndex: Gets or sets the zero-based index of the currently selected item in a ListBox SelectedItem An exception will occur if you get the value of a ListBox's SelectedItem property when no item is selected Items in a list box have an index starting with 0 The first item has index 0, the nth item has index n-1 The SelectedIndex property keeps the index. If no item is selected the, value of SelectedIndex is -1
            if (fruitListBox.SelectedIndex != -1) 
            { 
            
            }  
            
            
Cards (switch, listbox selection, parameter) Big SandBox ♥