Explain it simply
Practice Exercises: Fundamentals, in plain language
This exercise set is designed to help you revisit and practice the variables, operators, if/else, loops, and arrays concepts covered in the Basic chapter. There's no new teaching here — just a chance to actually write code using the syntax you've already learned. Treat each task as its own little project: create a new console app for it and run it. The tasks are simple, but writing and running the syntax hands-on will help it stick in your memory far better than just reading about it.
Problem → Why → Solution
The problem — why this concept matters
Passing one sample input does not complete an exercise or expose boundary bugs.
A common mistake
Assuming one successful happy path proves the code is correct, maintainable, and production-ready.
Core concept — understand how it works
Turn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.
- requirement
- reason and implement
- test and explain
Explain the core concept and runtime behavior of Practice Exercises: Fundamentals
Core syntax or pattern
// Task 1 starter
for (int i = 1; i <= 50; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
// Task 2 starter
int[] numbers = { 4, 8, 15, 16, 23, 42 };
int sum = 0;
foreach (int n in numbers)
{
sum += n;
}
double average = (double)sum / numbers.Length;
Console.WriteLine($"Sum: {sum}, Average: {average}");
// Task 3 starter
Console.Write("Enter age: ");
int age = int.Parse(Console.ReadLine());
// use if / else if / else to classify Child, Teenager, or Adult
// Task 4 starter
Console.Write("Enter a sentence: ");
string text = Console.ReadLine();
int vowelCount = 0;
// use a loop to count the vowelsTurn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.
Read the code step by step
- requirement
// Task 1 starterThis exercise set is designed to help you revisit and practice the variables, operators, if/else, loops, and arrays concepts covered in the Basic chapter. There's no new teaching here — just a chance to actually write code using the syntax you've already learned. Treat each task as its own little project: create a new console app for it and run it. The tasks are simple, but writing and running the syntax hands-on will help it stick in your memory far better than just reading about it.
- reason and implement
sum += n;Turn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.
- test and explain
// use a loop to count the vowelsImplement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.
- requirement
- reason and implement
- test and explain
Success
Explain the core concept and runtime behavior of Practice Exercises: Fundamentals
Failure
Passing one sample input does not complete an exercise or expose boundary bugs.
Let's think about this for a second
This exercise set is designed to help you revisit and practice the variables, operators, if/else, loops, and arrays concepts covered in the Basic chapter. There's no new teaching here — just a chance to actually write code using the syntax you've already learned. Treat each task as its own little project: create a new console app for it and run it. The tasks are simple, but writing and running the syntax hands-on will help it stick in your memory far better than just reading about it.
Exercises
(1) Print only the even numbers from 1 to 50 using a for loop. (2) Create an integer array (say, 6 elements) and use a foreach loop to find the sum and average of the numbers in the array. (3) Read a user-entered age with Console.ReadLine() and use if/else if/else to categorize it as "Child", "Teenager", or "Adult". (4) Read a string from the user and use a loop to count both the total number of characters and the number of vowels (a, e, i, o, u) in it.
Code Example
// Task 1 starter
for (int i = 1; i <= 50; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
// Task 2 starter
int[] numbers = { 4, 8, 15, 16, 23, 42 };
int sum = 0;
foreach (int n in numbers)
{
sum += n;
}
double average = (double)sum / numbers.Length;
Console.WriteLine($"Sum: {sum}, Average: {average}");
// Task 3 starter
Console.Write("Enter age: ");
int age = int.Parse(Console.ReadLine());
// use if / else if / else to classify Child, Teenager, or Adult
// Task 4 starter
Console.Write("Enter a sentence: ");
string text = Console.ReadLine();
int vowelCount = 0;
// use a loop to count the vowelsAll four tasks will print correct results to the console: the list of even numbers, the sum/average, the age category, and the vowel count.5-Minute Challenge
Modify Task 2 so it also finds the largest and smallest numbers in the array — but without using Math.Max/Math.Min, using a loop instead (5 minutes).
A Quick Heads-Up
Remember that integer division (int / int) only returns an int result — don't forget to cast to (double) for calculations like averages or percentages.
Real-world usage
How to apply it in a real project
Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.
Mini challenge
Build it yourself
Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.
- Rebuild the example without copying it
- Test a normal input and an edge case
- Predict the compiler or output result before running it
// Task 1 starter
for (int i = 1; i <= 50; i++)
{
if (i % 2 == 0)
{
Console.WriteLine(i);
}
}
// Task 2 starter
int[] numbers = { 4, 8, 15, 16, 23, 42 };
int sum = 0;
foreach (int n in numbers)
{
sum += n;
}
double average = (double)sum / numbers.Length;
Console.WriteLine($"Sum: {sum}, Average: {average}");
// Task 3 starter
Console.Write("Enter age: ");
int age = int.Parse(Console.ReadLine());
// use if / else if / else to classify Child, Teenager, or Adult
// Task 4 starter
Console.Write("Enter a sentence: ");
string text = Console.ReadLine();
int vowelCount = 0;
// use a loop to count the vowelsExpected: Explain the core concept and runtime behavior of Practice Exercises: Fundamentals
Show hint
Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.
Quick quiz 1 / 3
Quick quiz 2 / 3
Quick quiz 3 / 3
What to remember
- requirement → reason and implement → test and explain
- Turn fundamentals exercises into small functions with input contracts, boundaries, invalid cases, and expected output rather than syntax-only questions. Predict before running and explain results with compiler or runtime evidence.
- Passing one sample input does not complete an exercise or expose boundary bugs.
- Implement a temperature converter, grade classifier, and array statistics method with at least three normal, boundary, or invalid tests each.
Unit testing C# in .NET — Microsoft Learn