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