Control flow statements help you decide which code runs and how many times. This tutorial covers:
for
, while
, do-while
)Checks conditions and runs code based on the result.
int age = 20; if (age >= 18) { Console.WriteLine("You are an adult."); } else if (age >= 13) { Console.WriteLine("You are a teenager."); } else { Console.WriteLine("You are a child."); }
Selects one code block to run based on a variable.
char grade = 'B'; switch (grade) { case 'A': Console.WriteLine("Excellent!"); break; case 'B': case 'C': Console.WriteLine("Well done."); break; case 'D': Console.WriteLine("You passed."); break; default: Console.WriteLine("Try again."); break; }
Repeat code multiple times.
for (int i = 1; i <= 5; i++) { Console.WriteLine("Count: " + i); }
int i = 1; while (i <= 5) { Console.WriteLine("Count: " + i); i++; }
int i = 1; do { Console.WriteLine("Count: " + i); i++; } while (i <= 5);
Change the values and conditions, then click Run to see control flow in action.
Help others discover Technorank Learning by sharing your honest experience.
Your support inspires us to keep building!