quot;Welcome to C#, {name}!"); ``` --- ## Lesson 2 – Variables of Power ### Objectives - Understand data types and declarations. - Perform basic operations and type conversion. ### Core Types |Type|Example|Description| |---|---|---| |`int`|`int x = 5;`|Whole numbers| |`double`|`double y = 2.5;`|Floating-point| |`char`|`'A'`|Single character| |`string`|`"text"`|Sequence of chars| |`bool`|`true/false`|Logical values| ### Arithmetic Example ```csharp int a = 10; int b = 3; double result = (double)a / b; Console.WriteLine(result); // 3.3333 ``` ### Casting & Conversion ```csharp string s = "42"; int value = int.Parse(s); ``` ### Mini-Project — Unit Converter Prompt for kilometres and convert to miles: ```csharp Console.Write("Enter distance in km: "); double km = double.Parse(Console.ReadLine()); double miles = km * 0.621371; Console.WriteLine(quot;{km} km = {miles:F2} miles"); ``` --- ## Lesson 3 – Logic and Flow ### Objectives - Apply `if`, `else if`, `switch`. - Use loops: `for`, `while`, `foreach`. - Combine conditions with logical operators. ### Conditional Example ```csharp Console.Write("Enter age: "); int age = int.Parse(Console.ReadLine()); if (age >= 18) Console.WriteLine("Adult"); else if (age >= 13) Console.WriteLine("Teenager"); else Console.WriteLine("Child"); ``` ### Loop Examples ```csharp for (int i = 1; i <= 5; i++) Console.WriteLine(i); int x = 0; while (x < 5) { Console.WriteLine(x); x++; } ``` ### Mini-Project — Number Guessing Game ```csharp Random rnd = new Random(); int target = rnd.Next(1, 21); int guess = 0; Console.WriteLine("Guess a number 1-20:"); while (guess != target) { guess = int.Parse(Console.ReadLine()); if (guess < target) Console.WriteLine("Too low!"); else if (guess > target) Console.WriteLine("Too high!"); else Console.WriteLine("Correct!"); } ``` --- ## Lesson 4 – Debugging Like a Detective ### Objectives - Use breakpoints and step-through debugging in VS Code. - Inspect variables and call stack. - Understand common runtime errors. ### Tools 1. **Set Breakpoint:** Click left of line numbers. 2. **Run Debug:** `F5` → launch configuration `.vscode/launch.json`. 3. **Watch Variables:** Check values while stepping. ### Common Errors |Error|Cause|Fix| |---|---|---| |`NullReferenceException`|Using a null object|Initialise before use| |`FormatException`|Bad input to `Parse()`|Validate input| |`IndexOutOfRangeException`|Invalid array index|Check bounds| ### Mini-Project — Bug Hunt Broken code: ```csharp string[] names = null; for (int i = 0; i <= names.Length; i++) Console.WriteLine(names[i]); ``` **Fix:** ```csharp string[] names = { "Ana", "Ben", "Cara" }; for (int i = 0; i < names.Length; i++) Console.WriteLine(names[i]); ``` Run under debugger and observe each iteration. --- ## Lesson 5 – Collections and LINQ ### Objectives - Work with arrays and generic collections. - Understand basic LINQ queries. - Build a small in-memory data manager. ### Arrays ```csharp int[] scores = { 90, 70, 80 }; foreach (int s in scores) Console.WriteLine(s); ``` ### Lists and Dictionaries ```csharp using System.Collections.Generic; List<string> students = new List<string> { "Ava", "Ben", "Cara" }; students.Add("Dan"); Dictionary<string, int> grades = new(); grades["Ava"] = 95; ``` ### LINQ Basics ```csharp using System.Linq; var highScores = from g in grades where g.Value >= 90 orderby g.Key select g; foreach (var g in highScores) Console.WriteLine(quot;{g.Key}: {g.Value}"); ``` ### Mini-Project — Student Manager Implement add/remove/display for students with scores. ```csharp Dictionary<string, int> students = new(); while (true) { Console.Write("Command (add/list/quit): "); string cmd = Console.ReadLine(); if (cmd == "add") { Console.Write("Name: "); string name = Console.ReadLine(); Console.Write("Score: "); int score = int.Parse(Console.ReadLine()); students[name] = score; } else if (cmd == "list") { foreach (var s in students.OrderByDescending(s => s.Value)) Console.WriteLine(quot;{s.Key} - {s.Value}"); } else if (cmd == "quit") break; } ``` --- ### ✅ End of Phase 1 Summary By now you can: - Write, compile and debug C# programs. - Use variables, operators and control structures. - Handle collections and simple data queries. Next: **Phase 2 – Object Power (Lessons 6–9)** We’ll dive into Classes, OOP principles, and design patterns. --- Confirm when you’ve copied Phase 1 into your workspace and I’ll immediately generate **Phase 2 (Lessons 6–9)**.