How to use if statements in MNScript [Hydro's Series Part #4]

Hydro

Community Leader
Com. Leader
Lead Dev
Dec 19, 2020
95
80
21
Hello everyone! Welcome to part 4 of this tutorial series that I'm making. In this chapter, we'll be looking at if/else statements, and how to use them.

As always, this tutorial series will assume that you are somewhat familiar with Maxnet, and that you have followed along with all previous instalments of this tutorial series. Let's begin!

Part 1). What are if and else statements?

If statements in MNScript provides the simplest and most powerful way for you to enable your programs to make "decisions". They allow you to run different "branches" of code if some criteria that you've specified is met. Let's take a simple example, you're writing a program to determine whether someone is allowed into your night club. The mayor has created a law, this law states that you must be at least 21 years old to enter the night club and in order to comply with this you decide to write a program to check if somebody is allowed in the club. Below is a very simple example of a potential solution to this problem.

b5bdcba8191d4f8c726c3de89386ebef.png
Code:
using Console;

number age = 20;

if (age > 21) {
    
    Console.WriteLine("You are old enough to enter the night club");
    
} else {
    
    Console.WriteLine("You are not old enough to enter the night club"); 
    
}

808ea4fbc2fe0d9ffc8145b976a4304f.png

What's going on here? Let's break this down so we can see what's happening.


  • Firstly, we tell the compiler that we'll be using the Console library. This will let us access all of the functions in the console library
  • Secondly, we define the variable age to be twenty
  • Thirdly, we use a new MNScript semantic, aptly named if. We define the prerequisite condition to be that age > 21 (age needs to be greater than 21)
  • We state that if this condition is met, that the program should use the library function Console.WriteLine to tell the user that they are old enough to enter the night club
  • After the character }, we use yet another piece of new syntax, the else keyword.
  • We tell the program that if the condition age > 21 is not met, that we should instead use the Console.WriteLine to tell the user that they are not old enough to enter
  • Finally, we end the program with a }

The meat of this program is really defined by the if/else combination, these constructs have enabled this program to evaluate the expression age > 21, and it's allowed the program to dynamically change the code that should be executed. The interesting part about these lines is that for every if keyword, it has a pair of regular brackets (), and a pair of curly braces {}.

Let's work backwards here, curly braces {} are used to define a block of code that can be executed; and in the instance of our if statement it's simply defining the code that should be executed if the statement's condition is met.

The other set of brackets that we need to consider are the regular () brackets. The code that's contained within these brackets forms the condition that the if statement will evaluate before running. If the condition evaluates to the bool value of true, the code within the curly braces will be executed. Alternatively, if the condition evaluates to false then the code defined in the else statement will be executed. Note: else statements are optional, always have the exact opposite condition of their paired if statement, and will never execute if the paired if statement's condition evaluates to true.

To add to this point, please note that else statements can only be defined directly after the closing curly brace of an if statement. You can feel free to add as much or as little spacing as you please, the MNScript compiler will strip out all spacing so just focus on making your code as readable as possible. Additionally, you can choose to omit else statements altogether if your application doesn't need to do anything special if when the if block's condition fails.

Lastly, notice that in the code example provided that some lines of code start further along the page than others. This is known as indentation in code. Lines of code which follow opening curly braces { are typically "indented" by one additional tab (this stacks). Lines of code which starts with }, or which comes after this character have their indentation reduced by one (this also stacks); in your code, curly braces should always line up with the beginning of your else/if statement. Whilst this isn't a strict requirement implemented in the language, it's one of the most important recommendations I can give you as following this advice makes your code much easier to maintain and much easier to debug.

With this in mind, please try to understand what's happening in this annotated screenshot here.

2534f69a66133ac2630e453038b606b4.png



Part 2). A more complex example

Look at the flow chart below, and consider how you would implement this in MNScript given the concepts that we've already covered.

e456830d85cbe2d7bbad527f7f0fe520.png

I've taken the liberty of implementing this solution. I strongly recommend that instead of just reading through this code and moving onto the next tutorial that you take this code and use it on your own Maxnet terminal. You should experiment with the concepts we've covered; try change the code the application is expecting, or try adding a secondary code that the application will accept. Simply reading the code in these tutorials is not enough to learn the concepts, you will need to use and modify the code samples provided if you want to develop an understanding about how to use it.

db487a335e5e130809be3e7a9852e014.png

Code:
using Console;
using Util;
using Application;

// Ask the user to enter the code, and read it in once they type it
Console.WriteLine("Please enter the numeric code");
string userInput = Console.ReadLine();

// Convert the text the user entered into a number
number enteredNumber = Util.ToNumber(userInput);


// Check if the entered number is equal to -1.
// The documentation states that inputs which can't be converted to numbers will be converted into -1

if (enteredNumber == -1) {
    Console.WriteLine("The number you entered isn't valid");
    Application.Exit();
}

if (enteredNumber == 1337) {
    Console.WriteLine("Correct!");

    // More code here that would make your turret friendly
    // More code here that would open your fading doors

} else {
    Console.WriteLine("Incorrect");
    
    // More code here that would make your turret aggressive
    // More code here that would notify your gang members
}



Part 3: Additional Tasks

Take the advanced code snippet in the spoiler above and create a new MNScript file from it (we've covered this in every previous tutorial if you've forgotten how to do this!) (file create <name.msc>). Once you've got this code up and running on your own Maxnet terminal, attempt to make the following changes to the code below. If you get stuck you're free to ask for help in the #mnscript-dev channel on our discord (just please ask your question in it's entirety, and avoid asking xy problems)

  1. Add some functionality into the program so that the user can optionally type a second pass code. Once complete, the program should support both the 1337 and 5531 as valid passwords. (The big table of operators in the variables tutorial will be useful for this!)
  2. Add an easter egg into the application; if the user types ping then write to the console pong
  3. Move the supported pass codes into variables that are defined at the top of the code file. This will help you ensure that your program is more maintainable in the future.



n.b. Apologies in advance if this is confusing, it's super difficult to explain through text and screenshots. We're planning to make a YouTube series for this eventually, but for the meanwhile, we're sticking with forum tutorials as they're easy to reference!
 
  • Like
Reactions: Marco