Control flow in C# refers to the order in which the statements in a program are executed. C# goes in sequential order unless otherwise specified by different structures. There are 3 main types of control structures.
Conditional statements include if, else if, else, ternary operators, and switch statements. You use conditionals in order to determine whether certain information should be provided to the user or certain user should run. Here is an example.
Console.Write("What is your age: ");
int age = Int32.Parse(Console.ReadLine());
if(age >= 18) {
Console.WriteLine("You are able to vote!");
}
else {
Console.WriteLine("You are not able to vote!");
}
Reading conditional statements as plain English is really useful when trying to figure out what code should do. In the code example above: If I am older than 18(sorry for my non US people), I can vote. Else, I cannot vote.
The else if statement is used when you have multiple conditions to evaluate in a sequential manner. It's typically used in situations where you want to check a series of conditions, and as soon as one of them evaluates to true, you want to execute a specific block of code and skip the rest of the conditions. A good example of this is converting a percentage to a letter grade.
int score = 75;
if (score >= 90)
{
Console.WriteLine("Grade: A");
}
else if (score >= 80)
{
Console.WriteLine("Grade: B");
}
else if (score >= 70)
{
Console.WriteLine("Grade: C");
}
else if (score >= 60)
{
Console.WriteLine("Grade: D");
}
else
{
Console.WriteLine("Grade: F");
}
Once one of the conditions are true, it will not check the rest of the conditions.
Ternary operators are shorter versions of if-else statements that are to be used when the if statement is short. The main learning for ternary operators is syntax. The `?` represents the if. If the condition before the `?` is true, the statement after will execute. The `:` represents the else. If the condition before the `?` is not true, then, the statement after the `:` will execute. The functionality is the same as using an if and else statement.
Basic Ternary Structure
Condition ? executeIfConditionIsTrue : elseDoThis;
In the example below, I will show the "translations." between using if else and ternary operators.
int number = 10;
String result = (number % 2 == 0) ? "Even" : "Odd";
If we were to use the normal if else statement, the code would look like...
int number = 10;
String result;
if (number % 2 == 0) {
result = "Even";
} else {
result = "Odd";
}
As you can see, the functionality is the same but with ternary operators, it is more concise. Ternary operators are more common when making methods and returning.
Switch statements are basically glorified else if statements but there are some differences. Switch statements help to cover different cases of an expression. For example, if you provide three different choices for a user, you could use a switch statement.
Console.WriteLine("Choose an option:");
Console.WriteLine("1. View Profile");
Console.WriteLine("2. Edit Profile");
Console.WriteLine("3. Logout");
int choice = int.Parse(Console.ReadLine());
switch (choice)
{
case 1:
Console.WriteLine("Viewing Profile...");
break;
case 2:
Console.WriteLine("Editing Profile...");
break;
case 3:
Console.WriteLine("Logging out...");
break;
default:
Console.WriteLine("Invalid choice. Please select a valid option.");
break;
}
The default option is used if none of the cases are met and will execute the default code. If a case is met, the default condition is not executed.
Loops are used in C# when you want to repeat certain lines of code. There are two main loops in C# loops. For loops and while loops. For loops are used when you know the number of iterations that will be executed. While loops are used when you do not know the number of iterations. An iteration is considered a block of code being executed once. However, any for loop implementation can be recreated using a while loop. Loops are very common when iterating through lists and data structures.
Basic For Loop Structure
for (initialization; condition; increment) {
// Code to be executed
}
The first section of the for loop is the initialization. We initalize a variable that will act like a counter. The second part is the condition. Usually this condition is the limit to the counter. There can be other conditions passed in as well by using the logical operators(&& and ||). Lastly, the increment section is used, as the name suggests, to increment the counter. The counter gets incremented every single iteration of the for loop. Here is a basic example where we count till 5.
for (int i = 1; i <= 5; i++)
{
Console.WriteLine(i);
}
Return:
1
2
3
4
5
i is a very common variable used for the counter in for loops. The variable can be named anything you want. In this example, the counter starts at 1 and goes till it is less than or equal to 5. Each time, it prints out the counter.
Basic While Loop Structure
while (condition) {
// Code to be executed
}
While loops are much simpler. The code in the body is ran till the condition is false. Literally. That's it. A good example of when to use while loops is running a program till a user inputs a specific value(console applications).
string userInput = "";
Console.WriteLine("Enter some text (type 'exit' to quit):");
while (userInput != "exit")
{
userInput = Console.ReadLine();
Console.WriteLine("You entered: " + userInput);
}
Console.WriteLine("You exited the program.");
Sample return
You entered: hello
You entered: world
You entered: I love C#
You entered: I hate debugging
You entered: exit
You exited the program.
Break statements are used to exit a loop early.
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
Console.WriteLine("Breaking the loop at i = 5");
break; // This will exit the loop when i equals 5.
}
Console.WriteLine("i = " + i);
}
Return
i = 1
i = 2
i = 3
i = 4
Breaking the loop at i = 5
Continue statements are used to skip the iteration of the loop. Not the entire loop
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
Console.WriteLine("Skipping i = 5");
continue; // This will exit the loop when i equals 5.
}
Console.WriteLine("i = " + i);
}
Return
i = 1
i = 2
i = 3
i = 4
Skipping i = 5
i = 6
i = 7
i = 8
i = 9
i = 10
Exception handling is used in order to prevent crashes of your application. Exceptions can occur due to user misinput or bad programming. The try-catch statement is the main way most programmers handle exceptions. When an exception occurs inside the try block, the program skips down to the catch block and runs whatever code is located in there.
try
{
string userInput = Console.ReadLine();
int number = int.Parse(userInput);
Console.WriteLine($"You entered: {number}");
}
catch (Exception)
{
Console.WriteLine("Invalid input.");
}
Return
4
You entered: 4
You entered: aowrigrhawr
Invalid input.
As you can see, the code does not return a massive error. It just sends what is in the catch block.
In our withdraw method, we have to make sure that the account has enough money to withdraw the specified amount of money. Let's edit the method in order to acccomodate this.
static double WithdrawBalance(double balance, double balanceToSubtract)
{
if(balance - balanceToSubtract < 0)
{
Console.WriteLine($"Funds insufficient
Max Funds that can be deposited: {balance}");
return balance;
}
Console.WriteLine($"Withdrew {balanceToSubtract}.");
return balance - balanceToSubtract;
}
With this new method, if the user tries to withdraw a value greater than their current balance, a message will inform them about it.
Now, in order for this program to be functional, we need the user to be able to input information and that information to reflect accurately in code. We can do this by letting the user input different parameters and for different things to happen.
static void Main(string[] args)
{
string name = "John Doe";
int age = 13;
double balance = 500.00;
while (true) {
Console.Write($"Hello {name}. You currently have ${balance}.
If you want to deposit money, enter deposit. To withdraw, enter withdraw.
If you would like to leave the program, type in quit or q. ");
var input = Console.ReadLine();
if (input.ToLower().Equals("deposit"))
{
Console.Write("How much money would you like to deposit?: ");
var num = Convert.ToInt32(Console.ReadLine());
balance = DepositBalance(balance, num);
}
else if (input.ToLower().Equals("withdraw"))
{
Console.Write("How much money would you like to withdraw?: ");
var num = Convert.ToInt32(Console.ReadLine());
balance = WithdrawBalance(balance, num);
}
else if (input.ToLower().Equals("quit") || input.ToLower().Equals("q"))
{
Console.WriteLine("Exited the program.");
break;
}
}
}