Exam Notes

icon picker
Week 2 C# Fundamentals: Control Flow, Loops, and Methods

1. Control Flow in C#

If Statements: Executes a block of code only if the condition is true.
csharp
Copy code
if (condition) {
// Code executes if condition is true
}

If-Else Statements: Provides an alternative block of code if the condition is false.
csharp
Copy code
if (condition) {
// Executes if condition is true
} else {
// Executes if condition is false
}

Else If Statements: Used to check multiple conditions in sequence.
csharp
Copy code
if (condition1) {
// Executes if condition1 is true
} else if (condition2) {
// Executes if condition2 is true
} else {
// Executes if neither condition1 nor condition2 are true
}

Switch Statements: Tests a single expression against multiple values.
csharp
Copy code
switch (variable) {
case value1:
// Code for value1
break;
case value2:
// Code for value2
break;
default:
// Code if no cases match
break;
}

Key Points:
Use break to exit a case and prevent "fall-through" (unintentional execution of subsequent cases).
The default case is optional but recommended as a fallback when no cases match.

2. Loops in C#

Loops allow repeating a block of code as long as a condition is met.
While Loop: Executes as long as the condition is true. If the condition is false initially, the loop does not execute.
csharp
Copy code
while (condition) {
// Loop body
}

Do-While Loop: Executes the loop body at least once since the condition is checked after the loop body.
csharp
Copy code
do {
// Loop body
} while (condition);

For Loop: Commonly used when the number of iterations is known in advance.
csharp
Copy code
for (initialization; condition; update) {
// Loop body
}

Example:
csharp
Copy code
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}

Foreach Loop: Simplifies iterating over collections and arrays by automatically accessing each element in the collection.
csharp
Copy code
foreach (var item in collection) {
// Loop body
}

Example:
csharp
Copy code
int[] numbers = { 1, 2, 3 };
foreach (int num in numbers) {
Console.WriteLine(num);
}

Note: Values accessed in a foreach loop are read-only within the loop, so they cannot be modified directly.
Jump Statements in Loops:
break: Immediately exits the nearest loop or switch.
continue: Skips the rest of the code in the current loop iteration and proceeds to the next iteration.
return: Exits the method and, if specified, returns a value (applicable only within methods, not loops).

3. Methods in C#

Methods are reusable blocks of code that perform specific tasks. They can accept parameters, execute code, and optionally return values.
Defining a Method:
csharp
Copy code
returnType MethodName(parameterList) {
// Method body
}

Example:
csharp
Copy code
public int Add(int a, int b) {
return a + b;
}

Method Components:
Return Type: Specifies the type of value the method returns (e.g., int, string, void).
Method Name: Follows PascalCase naming convention.
Parameter List: Defines parameters passed to the method with types and names, separated by commas.
Return Statement: Ends method execution and returns a value (if specified).
Calling a Method:
Example of calling a method with parameters and capturing the return value:
csharp
Copy code
int result = Add(5, 3); // Calls the Add method with arguments 5 and 3

Examples of Method Types:
Method with Parameters and Return Type:
csharp
Copy code
public int Multiply(int x, int y) {
return x * y;
}

Void Method: A void method does not return a value.
csharp
Copy code
public void DisplayMessage() {
Console.WriteLine("Hello, World!");
}

Overloading Methods:
You can create multiple methods with the same name but different parameter lists. This is called method overloading.
csharp
Copy code
public int Add(int a, int b) {
return a + b;
}

public double Add(double a, double b) {
return a + b;
}

Note: Overloaded methods must differ in their parameter lists (number or types of parameters).

4. Arrays and Collections

Arrays and collections store groups of elements in C#.
Arrays: A fixed-size, ordered collection of elements of the same type.
csharp
Copy code
int[] numbers = new int[3] { 1, 2, 3 };

Accessing Elements: Array elements are accessed by index, starting at 0. For example, numbers[0] would give 1.
Iterating Over Arrays:
Using a for loop:
csharp
Copy code
for (int i = 0; i < numbers.Length; i++) {
Console.WriteLine(numbers[i]);
}

Using foreach loop (simpler for read-only access):
csharp
Copy code
foreach (int num in numbers) {
Console.WriteLine(num);
}

Collections: Dynamic data structures like List, Dictionary, and Queue are more flexible than arrays.
List: A resizable array-like collection.
csharp
Copy code
List<int> numbers = new List<int>() { 1, 2, 3 };
numbers.Add(4); // Adds 4 to the list

Dictionary: Stores key-value pairs.
csharp
Copy code
Dictionary<string, int> ages = new Dictionary<string, int>();
ages.Add("Alice", 25);
ages.Add("Bob", 30);

Summary: Key Points for Week 2

Control Flow: Use if, else, and switch statements for conditional execution.
Loops:
Use for and while loops for repeated execution.
Use foreach loops for simpler iteration over arrays and collections.
Understand break (exit loop/switch), continue (skip to next iteration), and return (exit method).
Methods:
Define methods with a return type, name, and optional parameters.
Use void for methods that perform actions but don’t return a value.
Method overloading allows multiple methods with the same name but different parameter lists.
Arrays:
Fixed-size collections that store elements of the same type.
Use for or foreach loops to access and iterate through array elements.
Want to print your doc?
This is not the way.
Try clicking the ⋯ next to your doc name or using a keyboard shortcut (
CtrlP
) instead.