Learn to Code via Tutorials on Repl.it!

← Back to all posts
C# Dash: Level 3 - Conditionalgeist
DungeonMaster00 (190)

C# Dash: Level 3 - Conditionalgeist

So how did you do in the last challenge. Well, I have an example repl at the bottom.

So were about to start level 3. This level will be IMPORTANT, so please pay attention to the stuff in the tutorial.

This tutorial will cover conditionals (loops too) and arrays.

The reader is expected to have read the last few tutorials.

Some info was retrieved from the Microsoft .NET Online C# Documentation

Words like this will appear in the glossary at the bottom.


More tutorials

Scratch Tutorials




SO LET'S GET STARTED!!!!!!!!!!!!!!!!!!!!!!!!




Conditionals

Conditionals are very important in all programming languages because that will make some code only execute if a certain condition is true.

If-else statements

If-else statements will activate a block of code if the conditional statement evaluates to true. If you need to evaluate multiple conditions, people generally use else if to have more than one condition. Here is a basic program if if, else if, and else:

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		int var4ifexample = 3;
		if (var4ifexample == 1) {//false so will not output this
			Console.WriteLine("The variable is equal to 1");
		}
		else if (var4ifexample == 3) {//true so it will output this
			Console.WriteLine("The variable is equal to 3");
		}
		else {//one condition above is true so will not output this
			Console.WriteLine("The variable is equal to some other value");
		}
	}
}

Also, there can (obviously) be more else if statements, as many as you need.

But sometimes, they can become annoying when you need multiple conditions all for the same variable...

Switch statements

Switch statements allow you to have multiple conditions for one variable without a ton of effort.

The statement starts with switch and the variable in ()s. Then there's the brackets.

Then there's cases. A case is where you have the outputs for the condition if the variable equals a certain value.

After the case keyword and the value for the variable, there's a colon (:). Then you have the output.

In the output for each case, the code for the case must end with the following line:

break;

One case must be default. It can appear anywhere on the list of cases, but the important part is that it acts as an else. The code will run in the default case if the value is not equal to any other value in the list of cases.

Here is the same program before, but with a switch statement.

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		int var4swtchexample = 3;
		switch (var4swtchexample) {
			case 1://will not be run
				Console.WriteLine("The variable is equal to 1");
				break;
			case 3://will be run
				Console.WriteLine("The variable is equal to 3");
				break;
			default:
				Console.WriteLine("The variable is equal to some other value");
				break;
		}
	}
}

While statement (loop)

A while statement is how you can generate a simple loop in C#. It will consist of the keyword while and then a condition in parenthesis. Then you have brackets, and of course, the code.

The code will repeat until the condition is false.

Example:

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		bool whileex = true;
		while (whileex == true) {
			Console.WriteLine(whileex);
		}
	}
}

This will generate an infinite loop that just says "True". But there's more complicated loops too.

For statement (loop)

A for loop will allow you to have a local variable, a condition, and something that happens upon loop iteration completion. Those three things in ()s are separated by semicolons (;) Then, there's the brackets and code.

Example:

using System;

class insert_class_name_here {
	public static void Main (string[] args) {
		for (int init = 1/*declares initializer*/; init < 16/*declares condition*/; init++ /*declares action for variable upon a loop iteration completion*/) {//++ at end of variable name will add 1 to the variable
			Console.WriteLine(init);//outputs numbers from 1-15
		}
	}
}

So that's enough about conditionals. Let's move onto arrays.


Arrays

Arrays are like variables that store multiple values. This one example for the syntax of an array:

string[] arex = {"example ", "array."};

For more about arrays, click here.


Level complete!


Next level: Methods and parameters.


Coding challenge (optional)

Write a program that will ask for user input and will print all the numbers from 1-20 or from 1-100 based on user input. Use loops and if-else/switch statements.


Glossary

  • Condition - An expression that evaluates to either true or false, often used to make statements that execute code if a statement is true or false.
  • Local variable - A variable that cannot be used outside of its method, for statement, or wherever it comes from.

Example for yesterday's challenge:

Comments
hotnewtop