You know when you type something really long and then get sick of typing but don't want to just leave it be since it took you 6 hours to type?
Well, um, yeah. I really wasted too much time on this to not share it. It's far from complete (I didn't even get to the part about taking Input), and has quite a few incomplete thoughts (even incomplete
near the end), but I'm going to have to just post it as it is because I was typing from 2:00 to 8:00 before it finally occured to me that I'll never finish it. I have homework to do and an RPG script that is only two hours away from completion if I ever manage to get back to it.
What am I going to do with myself when I have a job... sigh...
Secreted for length.
×
Here's the best way, in my opinion, to learn any language:
For beginners, you definitely want to start programming in a language with a good IDE. An IDE lets you do all this and more:
Click here to view the secret text
×It spots bugs as you type them
It formats your code
It color-codes your code for easy reference
It keeps track of variables for you
It lets you import images and sounds into your project
It lets you spread your work across multiple
For window-based languages, the IDE will let you create dialogs visually
And most importantly:
With one click of a button, it can compile your entire code into an executable program that has all your code, all your images/sound... everything you need.
And you can also debug (test-run) it easily, too; this will let you run your program from the IDE. If something isn't working right, you can usually tell it to run your code one line at a time, and to track certain variables for you so you can see where they got messed up.
Anywho, I recommend Microsoft's Visual Studios [2008] for several programming languages (MSVS has C++, C#, VB, J#, and FoxPro). Java has some good IDEs too, but I don't know any of their names.
Some things are programmed in the things that use the code, rather than a separate IDE. For examples, you write Flash ActionScript in your Flash Maker, and you write your graphing calculator's TI-BASIC programs using the calculator's built-in programmer.
When you install the IDE, do a custom install. Make sure that all of the Documentation (all the help files) will be installed, unless you plan on reading a hundred online tutorials.
So, in your IDE, make a new program/project/solution/whatever it's called. When learning a language, it's best to start with a Console program (if there's any template like that to choose from). You can worry about graphics and windows later.
The IDE will then usually start you off in a basic code file with the absolute bare necessesities included.
If this default code has a bunch of brackets and/or stuff nested in eachother, the language is class-based. Most of these languages are very similar, since they're based off of C. Generally, the most interior set of brackets there is something like a "
Main()"
function, which is where the program starts. Put most of your code here. Put other functions you make outside the Main() function. Classes you make just go somwhere within the Namespace or Module (most likely the lowest level). The stuff at the top outside of everything else is just compiling instructions, telling your IDE what needs to be included in your .exe for it to work.
If it doesn't have this sort of heirarchy, then it's not a class-based language. You can just simply write code.
Now for learning the language.
1. Learn basic text output first.
Before you learn how to set variables or make If.. structures or even just use proper syntax, you want to know how to make the program say something. Because if your program doesn't ever say anything, how do you know it's working?
Check your documentation and try searching for words that usually have some connection to basic text output. These would be stuff like "
out"
, "
disp"
, "
Console"
, "
Write"
, etc.
When you find the function for basic text output, try making a simple program that just uses that function to say "
Hello world!"
If it doesn't work at first when you debug the program, you must have an error in how you typed it (for instance, you forgot to put a semicolon at the end of a line in Java or C), or you're missing something else you need (such as something to #INCLUDE in C++). Hopefully, the IDE will pinpoint the mistake for you. Keep on tinkering with this simple program until it works. Some examples:
C#
Click here to view the secret text
×using System;
using System.Collections.Generic;
using System.Text;
namespace ConsoleApplication1
{
class Program
{
static void Main(string[] args)
{
Console.Write("Hello World.");
}
}
}
TI-BASIC (TI-83 Plus)
Click here to view the secret text
×isp "HELLO WORLD!"
2. Mess with variables.
Find out how you set variables of different types. Try to make an integer (numbers like 1, 120, and -46), a floating point number (numbers like 13.37), a string (text, like "
hello"
), a boolean (True or False), and an array (a set of numbers, like { 1 1 2 3 5 8 11 }).
Be aware that lots of languages actually call floating point numbers Singles and Doubles (which vary by precision).
You should learn how to declare (create) variables of these types (if the language requires declaration), and how to set their values. Check the documentation to find out how to do these.
Pretty much all c-based languages declare like this:
int aNumber;
But some do it other ways (for instance, VB does "
Dim [varname] As [type]"
). As for non-c-based languages... well, I've yet to see one that needs declaration.
In c-based languages, variables have the scope they are declared in. If you declare a variable in a function, the variable will exist only for that function. If you declare a variable in a class, all functions in that class can use the variable, and it will have a different value for each instance of the class. If you declare it in the namespace, the entire program within the namespace can use it.
Also learn how to add comments to your code, so that you can mark it up for your reference. How you do this is different in almost every language, so you'll have to look it up.
Examples of declaring and setting variables:
Visual Basic
Click here to view the secret text
×[color=Blue]Module Module1
Sub Main() 'The Main subroutine is run when the program starts
Dim Inty As Integer = 67
Dim Floaty As Single = 12.34
Dim string1, string2 As String
string1 = "Curdled"
string2 = " milk"
'concatenate the strings
Dim stringy = string1 + string2
Dim booly As Boolean
'make booly test if the concatenation worked
Dim contstr = "Curdled milk"
booly = (stringy = contstr)
booly = Not booly 'negates booly
Dim listy(2) As Integer
listy(0) = 1
listy(1) = 2
listy(2) = 3
'write it all
Console.WriteLine(Inty)
Console.WriteLine(Floaty)
Console.WriteLine(stringy)
Console.WriteLine(booly)
'write each element in listy
For Each n In listy
Console.Write(n)
Console.Write(" ")
Next
Stop 'Make the program wait
End Sub
End Module
TI-BASIC (TI-92 Titanium)
Click here to view the secret text
×:testprgm()
rgm
:Local v1,s1,b1,m1
:7099.4099->v1
:"conca" & "tenate"->s1
t(True)->b1
:{1,2,3}->m1
isp v1,s1,b1,m1
:EndPrgm
Learn the tricks to declaring and modifying variables in the language you're trying. You should probably first try equals signs like in the VB example above. Most languages do assign a value to a variable with an = sign. There are shortcuts to modifying a variable that you should try out. See if these work:
a += b; (there's also -=, *=, /=, and %=)
Increases the value of A by B (short for a = a+b)
a++; (there's also a--)
Increments or decrements A by 1 (short for a +=1)
Try these operators to see if they work.
3. Tests, control blocks, functions/subroutines.
No more examples from now on... it takes way too long to format them.
Now you want to learn stuff that lets you run code depending on certain conditions. Check the documentation on each thing to see how to use it in your language.
Here are operators that return boolean values (or, in other words, these are tests). When I list more than one way to type them, try all the things listed until one works; those vary by language. But most of these do exist in some way or form in each language.
Click here to view the secret text
×-FOR ANY OBJECT-
Reference equals (a=b, a==b, a IS b, a.RefEq(b))
Returns True if a and b are the same variable (as in, they're both stored in the same place in memory, and changing one changes the other).
-FOR NUMBER VALUES-
Equality (a=b, a==b)
Returns True if a is equal in value to b.
Greater than (a>b)
Greater than or equal (a>=b, a=>b)
Less than (a<b)
Less than or equal (a<=b, a=<b)
No short way to explain, but I'm sure you can guess.
Not equal to (a!=b, a><b, a<>b)
Returns true if a is not equal in value to b.
-FOR BOOLEAN VALUES-
Not (~a, a!, NOT a, not(a))
Returns the opposite of a.
And (a&b, a And b)
Shortcircuit And (a&&b, a AndAlso b)
Returns true if a and b are both true. The first one always evaluates both, the shortcircuit doesn't bother evaluating b if a is false.
Or (a|b, a OR b)
Shortcircuit Or (a||b, a OrElse b)
Returns true if either a or b is true. The first one always evaluates both, the shortcircuit doesn't bother evaluating b if a is true.
XOr (a^b, a XOR b)
Returns true if a or b, but not both, are true.
Regular And/Or operations differ from their shortcircuit variants only if b is a function. If it is, && and || might be noticeably faster than & and |, but will not always perform the other actions in b.
Here are common control blocks.
If, Then, and Else
Lets code be run only if something is true. Else code is run when it is false.
For
Runs the code multiple times, incrementing a variable each time (stopping when the variable reaches a certain number).
Foreach
Not in every language, but makes it easier to run code for each element in a list.
Switch and Case
Lets specific code be run depending on each possible value of a variable.
Once you learn how to test stuff in your language and you practice using some of the above control blocks (especially For; it's the most useful, and most frustrating).
Functions are important, and you should probably learn how to make one, now. They let you run a piece of code whenever, or to find something from another variable.
At first, you'll probably stay away from normal functions and stick to subroutines. A subroutine doesn't return a value; it just runs.
In a lot of programs, you Declare a variable muc
In a lot of programming languages, functions are given types based on the type of variable they return. If your function returns
Practice making Functions.
Also known as ExpHP everywhere else.