Chapter 3 : Selection Structure Flashcards
What is a control structure?
- logical design that controls the order in which statements execute
What is a sequence structure?
- Set of statements that execute in the order that
they appear
What circumstances that lets decision structure execute statements execute its statements?
- Certain condition exists
- Decision structure is also known as selection structure
What type for expression on decision structure?
- Boolean expression that can be evaluated as either true or false
How to write if statements in C#?
if ( expression )
{
statements
statements
}
List out all relational operator ( 6 )
- >
- <
- > =
- <=
- ==
- !=
What is the format for if-else statement?
if ( expression )
{
statements
}
else
{
statements
}
Format for Nested Decision Structures
if ( expression )
{
if ( expression )
{
statement
}
else
{
statement
}
}
else
{
statements
}
Format for if-else-if statement
if ( expression )
{
}
else if ( expression )
{
}
else if ( expression )
{
}
else
{
}
List out 3 logical operator
- && ( And )
- || ( Or )
- ! ( Not )
- ! ( Not Operator )
if ( ! ( temperature > 100 ) )
{
MessageBox.Show(“Ok”)
}
What does switch statement does?
- Lets the value of a variable or an expression determine which path of execution the program will take
- It is a multiple-alternative decision structure
- It can be used as an alternative to an if-else-if statement
Format for switch statement
switch (testExpression)
{
case value_1:
statements
break
case value_n:
statements
break
default:
statements
break
}
What is a testExpression in switch statement?
- Variable or an expression that given an integer, string, or bool value
- It cannot be floating-point or decimal value
List out 3 methods for comparing strings
- Use == operator
string n_1 = “Mark”
string n_2 = “Max”
if ( n_1 == n_2 )
{
}
- Compare string variables with string literals
if ( n_1 = “Max” )
{
} - Use String.Compare
string n_1 = “Mark”
string n_2 = “Max”
if ( String.Compare(name1,name2)==0 )
{
}
List out the syntax for preventing data conversion exception
int.TryParse( string , out targetVariable )
- Examples
- int.TryParse
- double.TryParse
- decimal.TryParse
- out keyword is required, it specifies that the targetVariable is an output variable
What is input validation?
- The process of inspecting data that has been entered into a program to make sure it is valid before it is used
if ( hours > 0 && hours <= 168 )
{
}
else
{
}
- TryParse methods check if the user enters the data, but it doesn’t check the integrity of the data
What is the difference between Radio Buttons & Check Boxes?
- Radio Buttons allow usrs to select one choice from several possible choices
- Check boxes allows users to select multiple options
By clicking on a radio button, the program automatically deselects any others, this is known as what?
- Mutually exclusive selection
What is the common properties for RadioButton ? ( 2 )
- 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
How to determine whether a RadioButton is selected?
if ( choiceRadioButton.Checked ) {
}
else{
}
- Is Same As
if ( choiceRadioButton.Checked == true ) {
}
else{
}
What is raised when a RadioButton or CheckBox control’s Checked property changes?
- CheckedChanged event
- I can write codes to respond to the event
- I can click the control in the Designer to create an empty CheckedChanged event
private void yellowRadioButton_CheckedChanged(object sender, EventArgs e) { }
What is a list box?
- Display a list of items and allow the user to select an item from the list
List out 4 commonly used properties for the List Box
- Text
- Gets or searches for the text of the currently selected item in the List Box
- Items
- Gets the items of the ListBox
- SelectedItem
- Get or sets the currently selected item in the ListBox
- SelectedIndex
- Gets or sets the zero-based index of the currently selected item in a ListBox
What will occur when I try to get the value of a ListBox SelectedItem property when no item is selected?
- Exception will occur
What is the starting index for SelectedItem property?
- 0
What is the index for SelectedIndex when there is no item selected?
- -1
- Selection Structure to check if user didn’t select anything
if (fruitListBox.SelectedIndex != -1) { }