Day 2

Day 2: Basics of C# – Part 2

Objective: Continue learning the fundamentals of C#.

Topics Covered:

  1. Control statements (if, if-else, switch)
  2. Loops (for, while, do-while)
  3. Introduction to arrays

Topic 1: Control Statements

Control statements are used to make decisions and execute different parts of the code based on certain conditions.

If-Else Statement

Example:

using System;

namespace IfElseDemo
{
class Program
{
static void Main(string[] args)
{
int number = 10;

if (number > 0)
{
Console.WriteLine("The number is positive.");
}
else if (number < 0)
{
Console.WriteLine("The number is negative.");
}
else
{
Console.WriteLine("The number is zero.");
}
}
}
}

Explanation:

  • if (number > 0) – Checks if the number is positive.
  • else if (number < 0) – Checks if the number is negative.
  • else – Executes if the number is zero.

Switch Statement

Example:

using System;

namespace SwitchDemo
{
class Program
{
static void Main(string[] args)
{
int day = 3;

switch (day)
{
case 1:
Console.WriteLine("Monday");
break;
case 2:
Console.WriteLine("Tuesday");
break;
case 3:
Console.WriteLine("Wednesday");
break;
case 4:
Console.WriteLine("Thursday");
break;
case 5:
Console.WriteLine("Friday");
break;
case 6:
Console.WriteLine("Saturday");
break;
case 7:
Console.WriteLine("Sunday");
break;
default:
Console.WriteLine("Invalid day");
break;
}
}
}
}

Explanation:

  • switch (day) – Evaluates the value of day.
  • case x: – Executes the code block corresponding to the value of day.
  • default: – Executes if none of the cases match.

Topic 2: Loops

Loops are used to execute a block of code repeatedly.

For Loop

Example:

using System;

namespace ForLoopDemo
{
class Program
{
static void Main(string[] args)
{
for (int i = 1; i <= 5; i++)
{
Console.WriteLine("Iteration: " + i);
}
}
}
}

Explanation:

  • for (int i = 1; i <= 5; i++) – Initializes i to 1, runs the loop while i is less than or equal to 5, and increments i by 1 after each iteration.

While Loop

Example:

using System;

namespace WhileLoopDemo
{
class Program
{
static void Main(string[] args)
{
int i = 1;

while (i <= 5)
{
Console.WriteLine("Iteration: " + i);
i++;
}
}
}
}

Explanation:

  • while (i <= 5) – Runs the loop while i is less than or equal to 5.
  • i++ – Increments i by 1 after each iteration.

Do-While Loop

Example:

using System;

namespace DoWhileLoopDemo
{
class Program
{
static void Main(string[] args)
{
int i = 1;

do
{
Console.WriteLine("Iteration: " + i);
i++;
} while (i <= 5);
}
}
}

Explanation:

  • do { ... } while (i <= 5) – Executes the loop body first, then checks the condition. Runs the loop while i is less than or equal to 5.

Topic 3: Introduction to Arrays

Arrays are used to store multiple values in a single variable.

Example:

using System;

namespace ArraysDemo
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 1, 2, 3, 4, 5 };

for (int i = 0; i < numbers.Length; i++)
{
Console.WriteLine("Element at index " + i + ": " + numbers[i]);
}
}
}
}

Explanation:

  • int[] numbers = { 1, 2, 3, 4, 5 }; – Declares and initializes an array of integers.
  • numbers.Length – Gets the length of the array.
  • for (int i = 0; i < numbers.Length; i++) – Iterates through the array elements.

Example Exercise

Write a program to find the largest and smallest numbers in an array.

using System;

namespace ArrayExercise
{
class Program
{
static void Main(string[] args)
{
int[] numbers = { 23, 89, 12, 37, 98, 5 };

int max = numbers[0];
int min = numbers[0];

for (int i = 1; i < numbers.Length; i++)
{
if (numbers[i] > max)
{
max = numbers[i];
}

if (numbers[i] < min)
{
min = numbers[i];
}
}

Console.WriteLine("Largest number: " + max);
Console.WriteLine("Smallest number: " + min);
}
}
}

Explanation:

  • Initializes the array numbers.
  • Sets max and min to the first element of the array.
  • Iterates through the array to find the largest (max) and smallest (min) numbers.

This completes the Day 2 topics for Day 3 click here

I’m Carolyn,

aka FoxyTester

Welcome to our charming little corner of the web—a cozy duck pond where we love to splash around and create ripples. I invite you to join our intimate circle of friends from around the world. Become part of our community and let’s start sharing!

Let’s connect

December 2025
M T W T F S S
1234567
891011121314
15161718192021
22232425262728
293031