This article will be give You’ve Got Everything You Need to Start Writing Programs in Any Language.
Variables in programming are a little different from the “X”s you remember
from high school algebra. In programming, a variable is like an empty
container—it can hold a number, a word, or any other data or data structure you
want to use in your program. The program can read and change the variable’s
value as it runs, letting you keep track of and manipulate data. Variables are
the basic building block of a program, and most lines of code in any program
will include a variable in some form. In some languages, such as Python, a
single variable can contain one type of data (say, a number), then can be
assigned to hold a different type of data (like a word). In other languages,
such as C and C#, a variable is declared, with a particular type, and then can
only hold that type of data for the rest of the program. This is the distinction
between dynamically typed and statically typed programming languages.
Most programs do not run in a vacuum, they accept some form of user input. To
deal with the uncertainty that this brings, we need to be able to write code
that is flexible, and to do that, we need conditionals. Conditionals are places
where the code branches. In most modern languages, they take the form of an if
statement, which joins an expression that is either true or false (called a
Boolean expression) and a block of code. The if statement says, in a nutshell,
“If this Boolean expression is true, execute the following code. Otherwise, skip
it.” In most languages, if statements can also include an else clause, which
allows you to specify a second block of code that will only be executed when the
Boolean expression is false. For example, on the opposite page, the
99BottlesOfBeer function includes an if statement that checks to see if the
“age” variable is greater than or equal to 21, and sets a different variable
called “drink” to an age appropriate libation.
Another way you can control the order in which code is executed is with a
loop. Where an if statement allows you to execute or not execute a certain block
of code, a loop allows you to keep executing the same block of code multiple
times. There are several different types of loops, but the two that you’ll find
in almost any programming language are the while loop and the for loop. A while
loop works a lot like an if statement. You attach a Boolean (true or false)
statement to the while loop, and as long as that statement is true, the loop
keeps repeating. Basically it says “as long as this statement is true, keep
going.” As a consequence, something inside the looping code has to make a change
that could cause the Boolean statement to become false, or else the loop will
never end. For example, the following code will print out the word “hello” 10
times, then stop:
while(x < 10) {
x = x + 1;
print(“hello”);
}
Notice that we used the variable x as a loop counter, to control the number
of times the loop runs. The other most common type of loop, the for loop, is
basically just a while loop with a built-in loop counter. You tell the loop
right away how many times you want it to run, like this:
for(int x; x < 10; x = x + 1)
{
print(“hello”);
}
The part after “for” just defines a counter. It says “start with a number
(integer) we’ll call x, and keep looping as long as x is less than 10. At the
end of every loop, increment x by one.”
The most powerful way to control the flow of a program is with functions,
which allow you to reuse code. Also called a subroutine, a function is a block
of code that you’ve given a name to, so you can reuse it any time you want. For
example, you could define a function called PrintHelloThenGoodbye by doing the
following:
void
PrintHellothenGoodbye()
{
print(“hello”);
print(“goodbye”);
}
Then, if you called that function three times in your code, as follows:
PrintHellothenGoodbye();
PrintHellothenGoodbye();
PrintHellothenGoodbye();
Your program would output “hello goodbye hello goodbye hello goodbye.” A
function can also take variables as inputs, and return an output value. So, for
instance, you could write a function that takes a number as an input, and
returns that number squared. It would like look like this:
int square(int tosquare)
{
return tosquare *
tosquare;
}
Notice the “return” keyword. That passes the following value back to whatever
part of the code called the function. So, if somewhere else in the code we
called the function like this:
print(square(5));
The program would print out the number 25.
Maybe the most intimidating thing about programming is the syntax—the strange
punctuation marks and cryptic words that make a page of code look like a foreign
language. Fortunately, in most programming languages, syntax is really only a
couple of rules that you have to remember, and a lot of syntax is shared between
languages. It’s all dependent on what language you’re programming in, but here
are a couple of syntactical elements that are common across many popular
languages:
SEMICOLON.
The semicolon is like the coding equivalent of a period each line of code
ends with one. It’s important, because in many languages, line breaks are just
for readability, and don’t have any effect on the execution of the code.
PARANTHESES
Parentheses are used after functions (see above) to contain that function’s
parameters (or inputs). You might remember this usage from your high school math
classes, when f(x) was a function that operated on the variable x.
CURLY BRACES
In a number of languages (particularly those derived from C), curly braces
“{}” are used to enclose and group blocks of code. They’re used, for instance,
after the control structures described on these pages (if statements, loops, and
functions), to designate the block of code that the statement refers to.
INDENTATION
Because all of the control structures can benested inside each other, code
tends to take on a sort of hierarchy. A particular line of code might be inside
an if statement, which is inside another if statement, which is inside a loop
that’s inside a function. All that can get hard to keep track of! To make it
easier, code is written with variable levels of indentation. The more indented a
line of code is, the more deeply nested it is. In most languages, indentation is
purely for readability, but in a few (like Python), it actually controls the
grouping of code, and is used instead of curly braces.