< Return to Video

How To: Beginner Programming: Part 2 - Primitive Variables and Arithmetic

  • 0:06 - 0:09
    Hello, and welcome to How To: Beginner Programming,
  • 0:09 - 0:12
    a multi-part guide to your first steps into the world of programming.
  • 0:12 - 0:16
    In each of the following episodes, I'll be demonstrating the
    basic principles of programming,
  • 0:16 - 0:19
    from creating your first application, to developing your own objects.
  • 0:19 - 0:23
    In the previous episode, we talked about Hello World,
  • 0:23 - 0:26
    setting up your environment, and outputting text to the screen.
  • 0:26 - 0:29
    In this episode I'll be explaining what variables are,
  • 0:29 - 0:32
    the types of variables available for use,
  • 0:32 - 0:34
    and some arithmetic that can be performed on them.
  • 0:34 - 0:38
    Right here I've got all the types of primitive variables...
  • 0:39 - 0:47
    These are all variables; the most basic types of variables.
  • 0:47 - 0:49
    I'm showing an example of how to declare them here...
  • 0:51 - 0:55
    When you declare something,
  • 0:55 - 0:57
    you need to know the type of variable to use first.
  • 0:57 - 0:59
    For example, an integer...
  • 0:59 - 1:04
    You give it a name, and then a value.
  • 1:04 - 1:14
    That's just assigning 293,193 to the variable.
  • 1:14 - 1:18
    I've got an example of all the upper and lower bounds
    of each of these types.
  • 1:18 - 1:26
    Bytes are 1 byte unsigned integers,
  • 1:29 - 1:31
    I'll run the program so you can see.
  • 1:31 - 1:33
    Byte is unsigned. So that's 0 to 255.
  • 1:33 - 1:39
    Short is a two byte signed integer,
    -32,000 up to 32,000.
  • 1:39 - 1:51
    Integer is a four byte signed integer,
    from ~-2 billion to ~2 billion.
  • 1:51 - 1:53
    Long is...
  • 1:53 - 2:05
    ~-9 centillion to ~9 centillion.
  • 2:05 - 2:08
    So, they're getting a bit big here!
  • 2:08 - 2:15
    Double is eight byte floating point numbers.
  • 2:15 - 2:22
    So, for example, 1.7 is a double, or 9.8, and so on.
  • 2:22 - 2:35
    Doubles range from
    ~-1.79×10^308 to ~1.79×10^308
  • 2:35 - 2:40
    Doubles have a huge range!
  • 2:40 - 2:47
    Decimals are sixteen byte decimal numbers,
    and they range from ...
  • 2:47 - 2:54
    I'm not even going to attempt to break that down...
  • 2:59 - 3:08
    It can handle both decimals and whole numbers.
  • 3:08 - 3:16
    Boolean is a true/false value, or 1/0.
  • 3:16 - 3:18
    And they can be assigned like this...
  • 3:24 - 3:27
    Boolean, then a name, then a value.
  • 3:27 - 3:33
    Char is a character, which represents a single unicode character.
  • 3:33 - 3:36
    You can view all of these on the ASCII sheet,
  • 3:36 - 3:40
    basically, anything you can type on the keyboard,
  • 3:40 - 3:43
    but only one character.
  • 3:43 - 3:46
    For example, a space, or a question mark.
  • 3:46 - 3:50
    String are made up of a sequence of chars (or characters),
  • 3:50 - 3:56
    I've just assigned "Hello" to this one,
    but they can hold any string of characters.
  • 3:56 - 3:58
    String accepts absolutely anything on the keyboard as well.
  • 3:58 - 4:06
    Those are all of the data types,
  • 4:06 - 4:10
    I'm now going to show you some math you can do with them.
  • 4:10 - 4:15
    Now, all numeric values can be used in arithmetic calculations.
  • 4:15 - 4:19
    For example, the decimal value...
  • 4:19 - 4:24
    the decimal value of 10.2...
  • 4:24 - 4:35
    can be added to the integer value of 2.
  • 4:35 - 4:41
    And if we write this out to the console...
  • 4:41 - 4:45
    and cast the decimal to a decimal,
  • 4:45 - 4:50
    because the program thinks it's a double,
  • 4:50 - 4:59
    so we put the little 'm' at the end to make sure
    the program knows it's a decimal.
  • 5:05 - 5:09
    And it will give us...
  • 5:16 - 5:20
    12.2
  • 5:20 - 5:26
    Arithmetic can also be used on characters.
  • 5:26 - 5:39
    The program uses the numeric value of a character in the arithmetic.
  • 5:39 - 5:45
    For example, if we have the character 'Z', and add 2 to it...
  • 5:47 - 5:50
    the ASCII value for the 'Z' will be used...
  • 5:54 - 5:56
    this is called casting.
  • 5:56 - 6:02
    Casting will take whatever variable type comes out of this
    and cast it as another type.
  • 6:02 - 6:07
    So this will go two places through the ASCII table from 'Z',
  • 6:07 - 6:31
    which is a backslash.
  • 6:31 - 6:33
    So that's arithmetic with chars.
  • 6:33 - 6:37
    Chars can also be added to strings. For example...
  • 7:03 - 7:09
    These double quotes are for assigning strings,
  • 7:09 - 7:13
    so a string can also be a single char (character).
  • 7:13 - 7:16
    If we want to make sure it's a char we're adding to it,
  • 7:16 - 7:19
    we use single quotes.
  • 7:19 - 7:25
    It still has the same effect, but the difference is:
  • 7:25 - 7:32
    a string uses double quotes, and a char uses single quotes.
  • 7:32 - 7:44
    There are some cool little functions you can do with primitive variables.
  • 7:58 - 8:01
    (Plus plus) will increment it by 1.
  • 8:01 - 8:05
    When we output it again, the answer will be 2.
  • 8:05 - 8:10
    (Minus minus) will decrement it by 1.
  • 8:10 - 8:14
    This value of "test" can be changed (reassigned) like this...
  • 8:17 - 8:32
    We can assign it to the variable name (the old value)
    plus 10.
  • 8:32 - 8:42
    We assigned it there, then we output 1.
    Increment it by 1 = 2. Decrement it by 1 = 1.
  • 8:42 - 8:44
    Add 10 = 11.
  • 8:44 - 8:47
    There are other cool things, but that's the basic stuff.
  • 8:47 - 8:54
    You can also do the same thing with minus,
    and there are some shorthand ways as well.
  • 8:54 - 9:03
    Plus equals (+=) is [increment and reassign].
  • 9:03 - 9:09
    That's all the basics when it comes to primitive variables
    and arithmetic.
  • 9:09 - 9:22
    So, this has been the second part of How To: Beginner Programming.
  • 9:22 - 9:24
    More parts will be released shortly,
  • 9:24 - 9:28
    again I'll leave a gap between videos to allow you to
    ask any questions you might have,
  • 9:28 - 9:30
    and I'll answer them in the next video.
  • 9:30 - 9:32
    I only had one question (last video), and that was,
  • 9:32 - 9:35
    why do I use C# over another language?
  • 9:35 - 9:40
    And that's because C#, I think, is a nicer language to begin with
  • 9:40 - 9:41
    than Visual Basic.
  • 9:41 - 9:45
    Because Visual Basic teaches awful practices,
  • 9:45 - 9:54
    and the structure of the code can be awkward.
  • 9:54 - 9:57
    If you go from Visual Basic to another language,
  • 9:57 - 9:58
    which you probably would,
  • 9:58 - 10:02
    then you carry over some bad practices.
  • 10:02 - 10:08
    C++ is incredibly involved when you get into arrays,
  • 10:08 - 10:10
    and we haven't gotten to arrays yet,
  • 10:10 - 10:19
    but in C++ you're accessing pointers in memory,
    whereas C# handles all of that background stuff for you.
  • 10:19 - 10:21
    In C++ you have to handle it all yourself.
  • 10:21 - 10:27
    That's why I picked C#.
  • 10:27 - 10:29
    Thanks very much for watching!
  • 10:29 - 10:31
    Again, leave any questions in the comment section below.
  • 10:31 - 10:33
    See you next time for episode three!
Title:
How To: Beginner Programming: Part 2 - Primitive Variables and Arithmetic
Description:

more » « less
Video Language:
English, British
Duration:
10:43

English subtitles

Revisions