Used to execute particular statements whether the value of the given condition evaluates to true or false
Selection structures in C#
if and if...else statements
switch statement
if-else statement
1. Evaluate expression
2. Execute statements if true
3. Execute statements if false
if-else-if statement
1. Evaluate if statement
2. Evaluate else if statements in order
3. Execute statements if true
4. Execute else statement if all else if false
switch statement
Executes a block of statements based on the variable to be tested for equality against a list of values
The variable or expression in the switch statement can only be an integer, a floating-point number, a character, or a string type</b>
Each case label must contain a break statement to stop the flow control of the switch statement
default statement
Executes statements when none of the cases match the specified variable
Conditional (ternary) operator
A special type of decision-making operator used to perform an operation that evaluates a logical expression then selects between two single statements depending on whether the defined expression evaluates to true or false
Conditional operator usage
int sum = 4 > 2 ? 4 + 2 : 4 - 2
string result = exam_score >= 60 ? "Student passed the exam." : "Student failed the exam."
Loop
A construct that enables a program to execute a block of statements or a loop body repetitively as long as the defined condition evaluates to true
Looping structures in C#
while
do...while
for
while loop
1. Evaluate condition
2. Execute loop body
3. Reevaluate condition
while loop
Repeats a block of statements as long as a given condition is true
Evaluates the condition first before executing the loop body
do...while loop
1. Execute loop body
2. Evaluate condition
do...while loop
Executes the loop body first before evaluating the given loop condition
Terminates the loop when the condition evaluates to false
for loop
1. Initialization
2. Condition evaluation
3. Update condition
4. Execute loop body
for loop
Executes a block of statements for a specific number of times
Specifies the elements of counter-controlled-repetition in a single line
break statement
Terminates a loop or a switch statement and transfers the flow of program execution to the statements following the enclosing loop or switch statement
continue statement
Skips the remaining statements in the loop body and immediately reevaluates the condition if it's a while or do...while loop, or it jumps to the update step if it's a for loop
Array
A set of fixed number of values called elements that can be accessed using integer index. Elements on an array are of the same data type. Indices in an array starts at 0.
Array
Used to store multiple values of the same data type at a time without declaring a different variable name for each value
Array elements can be of any data type
Defining a one-dimensional array
1. Declare a variable that refers to an array
2. Create an instance of the array through the new operator
One-dimensional array
int[] numbers = new int[10]
double[] grades = { 2.50, 2.75, 1.25, 5.0, 1.50 }
Accessing elements of a one-dimensional array
1. Use index number
2. Use foreach statement to iterate through the array
Two-dimensional array
Array elements are arranged in rows and columns, accessed by using two indices (row and column)
Represents a mutable string of characters that allows the user to expand the number of characters in the string object without allocating additional memory space
Creating a StringBuilder object
StringBuilder strComputer = new StringBuilder("Computer");
strComputer.Append(" is a great invention.");
StringBuilder class properties
char - Gets and sets the character at the specified index
Length - Gets the length of the current StringBuilder object