if/else, switch (classic), loops: for, while, do, foreach
Use if/else to branch, classic switch for discrete cases, and core loops (for, while, do, foreach) to repeat work clearly.
if/else, switch (classic), loops: for, while, do, foreach is a free C# Academy lesson on CoddyKit — lesson 1 of 3. You can read the complete lesson below for free — then practise it hands-on in the browser with a built-in code editor and a 24/7 AI tutor. It is part of the C# Academy learning path, one of 3 lessons in the course, and your progress syncs across the web and the CoddyKit app.
Overview
Goal: Choose actions with if/else, map discrete cases with switch, and repeat work with for, while, do, and foreach.
if/else basics
Use if/else for simple, readable branching; keep conditions small.
using System;
public class Program
{
public static void Main(string[] args)
{
int temperature = 18;
if (temperature >= 25)
{
Console.WriteLine("Warm");
}
else if (temperature >= 15)
{
Console.WriteLine("Mild");
}
else
{
Console.WriteLine("Cold");
}
}
}
switch (classic)
switch selects one case; remember break to prevent falling into the next case.
using System;
public class Program
{
public static void Main(string[] args)
{
int day = 3;
switch (day)
{
case 1:
Console.WriteLine("Mon");
break;
case 2:
Console.WriteLine("Tue");
break;
case 3:
Console.WriteLine("Wed");
break;
case 4:
Console.WriteLine("Thu");
break;
case 5:
Console.WriteLine("Fri");
break;
case 6:
case 7:
Console.WriteLine("Weekend");
break;
default:
Console.WriteLine("Invalid");
break;
}
}
}
for loop
for suits counted loops: init; condition; increment.
using System;
public class Program
{
public static void Main(string[] args)
{
int sum = 0;
for (int i = 1; i <= 5; i = i + 1)
{
sum = sum + i;
}
Console.WriteLine("Sum 1..5 = " + sum);
}
}
while & do-while
while checks before; do checks after, so the body runs at least once.
using System;
public class Program
{
public static void Main(string[] args)
{
// while: check before running
int countdown = 3;
while (countdown > 0)
{
Console.WriteLine(countdown);
countdown = countdown - 1;
}
Console.WriteLine("Go!");
// do-while: runs at least once
int tries = 0;
do
{
tries = tries + 1;
Console.WriteLine("Attempt " + tries);
} while (tries < 1);
}
}
foreach basics
foreach loops over items in arrays and collections with clear, safe iteration.
using System;
using System.Collections.Generic;
public class Program
{
public static void Main(string[] args)
{
int total = 0;
int[] nums = new int[] { 2, 4, 6 };
foreach (int n in nums)
{
total = total + n;
}
Console.WriteLine("Total = " + total);
// Works with collections too
List<string> names = new List<string>();
names.Add("Ada");
names.Add("Alan");
foreach (string s in names)
{
Console.WriteLine("Hi " + s);
}
}
}
do-while fact
Recap
Recap: Pick if/else for simple branches, switch for discrete labels, and the right loop (for, while, do, foreach) for clear repetition.
Frequently asked questions
Is the “if/else, switch (classic), loops: for, while, do, foreach” lesson free?
Yes — the full text of “if/else, switch (classic), loops: for, while, do, foreach” is free to read here on the web, and the C# Academy course includes 3 lessons in total. To practise it interactively (a built-in code editor and a 24/7 AI tutor) and unlock the rest of the C# Academy course, upgrade to CoddyKit PRO.
What will I learn in “if/else, switch (classic), loops: for, while, do, foreach”?
Use if/else to branch, classic switch for discrete cases, and core loops (for, while, do, foreach) to repeat work clearly. You practise C# Academy with hands-on code you run directly in the browser, and a 24/7 AI tutor answers your questions as you work through the lesson.
Do I need any experience to start C# Academy?
No prior experience is required. C# Academy on CoddyKit is structured for beginners through advanced learners; this is — lesson 1 of 3, so you can start here or from the beginning and move at your own pace.
How long does the “if/else, switch (classic), loops: for, while, do, foreach” lesson take?
Most CoddyKit lessons take about 5–10 minutes. Each one is bite-sized and interactive, so you make steady progress and pick up exactly where you left off across the web and the app.
Can I write and run code in this C# Academy lesson?
Yes. Every C# Academy lesson includes a built-in code editor, so you write and run real code right in your browser and get instant AI feedback — no local setup required.
All lessons in this course
- if/else, switch (classic), loops: for, while, do, foreach
- break/continue; goto (avoid); scope; exceptions vs guard checks
- Simple diagnostics: exceptions vs guard checks