Chapter 2. Basic Syntax and Branching
Basic constructs and making decisions.
In this chapter, we will learn about basic haXe constructs and pieces that make a program. We will also learn about branching (those are indeed constructs too), so that your program can make decisions and choose different paths according to conditions.
In fact, this chapter is one of the most important to get you started because it will teach you how to tell a haXe program what to do.
In this second chapter, we are going to learn quite a lot of things. The great thing is that after this chapter, you will have the knowledge to create programs that won't always do the same things. You will know about all haXe constructs too and therefore, should be able to have fun with haXe.
In this chapter, we will:
Learn about modules, packages, and classes
Learn about constants
Talk about binary and unary operators
Learn what blocks are and their particularities
Learn about variables and scope
Talk about how to access fields and call methods...
Modules, packages, and classes
If you're familiar with
Object Oriented Programming (OOP) languages, then there are chances that you at least know the words "packages" and "classes"; if that's not the case, don't panic, you are going to learn all of it here.
Packages are a convenient way of splitting code into groups. Doing so, allows one to have several classes with the same name in different packages. This can be really useful, as you may, for example support two scripting languages in an application and need to write an interpreter class for each one.
Packages are represented by folders under your source directory on your filesystem. Each package has a path, which is a string obtained by joining the folders' name with dots. So, for example, if your source folder is /dev/myProject/src
and you have a folder /dev/myProject/src/proj/dao
, you have a package whose path is proj.da
(you also have a package "proj"). There's a special package that has an empty path; it is named the top-level...
There are six types of constants in haXe. We will now take a look at all of them. Some of them are composed of several kinds of them.
The first type of constants is one that is spread most across programming languages: Booleans. For your information, in haXe Booleans have the type
Bool
. The following are two expressions, which are Boolean constants:
That's both easy and important. By the way, in haXe Bool is indeed an Enum (if you don't know what this is, don't worry, we will learn about it later). This Enum has two values, and true and false are just reference to these values.
Booleans are generally used to represent the truthiness of things. For example, when one is presented with a checkbox in an interface, the "checked" property of the checkbox can be of type Bool.
This one is easy; you can use integers constants by simply writing their values, for example:
You can also use the hexadecimal notation by starting your value with...
Binary and unary operators
Binary and unary operators are two very important concepts in programming because they can both respectively be used to manipulate data. So, let's start with binary operators.
There are several operators, some of which you may already be familiar with, and some that you may not know, even if you have some programming experience. So, take a look at see all of them!
There are several operators that can assign a value to an expression:
There are several
comparison operators, all of them returning either true or false.