Conditional branching

Conditional branching is a very important part of programming because it will allow your program to behave differently depending on the context. So, let's talk about if and switch.

If

The if expression allows you to test an expression; if it returns true then the following expression will be executed. You may also use the else keyword, followed by an expression, which will be executed if the test returns false. Note that a block of code is an expression.

The following is the general syntax:

if (condition) exprExecutedIfTrue [else exprExecutedIfTestFalse]

Now, let's look at some examples:

if(age<18)
{
trace("you are not an adult.");
} else
{
trace("You are an adult");
}

I think it is obvious here what this block of code does. Notice that this code could have been written in the following way too:

if(age<18)
trace("you are not an adult.");
else
trace("You are an adult");

This can be interesting to know, as it can save some typing when the block has only one line. On the other hand, some people prefer always writing brackets, as it can be more readable.

Or, using what we've learned about blocks:

class TesthaXe
{
public static function main(): Void
{
var age = 18;
trace(
if(age<18)
{
are not an adult.";
}
else
{
are an adult";
}
);
}
}

So, in this example, the program will give the output: You are an adult.

Now, a simple example with else if:

if(age < 16)
{
trace("You are young!")
} else if(age >= 16 && age < 18)
{
trace("You are almost an adult")
} else
{
trace("You are an adult");
}

OK, this is almost the same thing, but it gives you an overview of how you can use else if.

If instructions can also be contained one into another, as shown in the following example:

class TesthaXe
{
public static function main()
{
var age = 18;
var country = "France";
if(country == "France")
{
if(age < 18)
{
neko.Lib.println("You do not have the right to drink everything.");
} else
{
neko.Lib.println("You can drink anything you want.");
}
} else if(country == "USA")
{
if(age < 18)
{
neko.Lib.println("You do not have the right to drink everything.");
} else
{
if(age < 21)
{
neko.Lib.println("Depending on the state, you may or may not drink what you want.");
} else
{
neko.Lib.println("You can drink anything you want.");
}
}
}
}
}

The preceding program is an idea for something that could be interesting to young travelers. Depending on your age and the country you're in (because some drinks may be forbidden to people of different ages depending on the state), it tries to determine if you can drink anything you want or not.

Note that this program is not expected to be accurate nor is it perfectly written. Indeed, it's certainly the worst way to write this algorithm, but it is there only to demonstrate what you can do.

Switch

The switch construct allows you to test the equality of a value against other values. So, let's go to an example directly:

switch(title)
{
case "Mr":
trace('You are man'),
case 'Ms', 'Melle':
trace('You are a woman'),
default:
trace('I don't know if you are a man or a woman'),
}

This block of code uses the title of somebody to tell if it's a man or a woman. As you can see, after the switch keyword and inside parentheses, there is the value to test against. Then, after case, there are one or several values to test for equality. If there are several values, they should be separated by commas.

Note that there is no break keyword needed in haXe, only one case is called and the program will continue executing directly at the end of the switch block when reaching the end of the case.

The default keyword is there to indicate the code to execute if no value matches.

Again, this example could have been written this way:

class TesthaXe
{
public static function main(): Void
{
var title = "Mr";
trace(
switch(title)
{
case "Mr":
'You are man';
case 'Ms', 'Melle':
'You are a woman';
default:
'I don't know if you are a man or a woman';
}
);
}
}

I know I'm repeating myself, but trust me, you'll come to love this way of writing things.

..................Content has been hidden....................

You can't read the all page of ebook, please click here login for view all page.
Reset
18.223.210.71