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 first if condition
2. If true, execute statements and skip other conditions
3. If false, evaluate next else if condition
4. If none true, execute else statements
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
The switch statement may or may not contain a default statement, which must appear at the end of switch statement
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
Ternary operator usage
int sum = 4 > 2 ? 4 + 2 : 4 - 2
int exam_score = 75; string result = exam_score >= 60 ? "Student passed the exam." : "Student failed the exam."
The ternary operator is used as an alternative to if...else statements that contains a single statement body
If statement
A selection statement that executes a block of code if the condition is true.
If-else statement
A selection statement that executes one block of code if the condition is true, and another block of code if the condition is false.
If-elif-else statement
A selection statement that allows for multiple conditions to be evaluated and executed accordingly.
False
A value that represents the condition not being satisfied.
True
A value that represents the condition being satisfied.
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
Continues to execute the loop body until 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
String
A sequential collection of characters that is used to represent text
String variable initialization
Directly assigning a string literal
Using the new keyword and calling the String class constructor
String variable initialization
string word = "Computer";
string strGreet = new string(word);
String class properties
char - Gets the character at a specified index position
Length - Gets the total number of characters in the string
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
StringBuilder class methods
Append(string value)
Equals(string value)
Clear()
Replace(char oldValue, char newValue)
ToString()
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