Day 2: Basics of C# – Part 2
Objective: Continue learning the fundamentals of C#.
Topics Covered:
- Control statements (if, if-else, switch)
- Loops (for, while, do-while)
- 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 ofday.case x:– Executes the code block corresponding to the value ofday.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++)– Initializesito 1, runs the loop whileiis less than or equal to 5, and incrementsiby 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 whileiis less than or equal to 5.i++– Incrementsiby 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 whileiis 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
maxandminto 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




