cough
function, that was a layer of abstraction of our own.We’ll use a new language, C, that’s purely text, which comes with some cryptic keywords and punctuation:
#include <stdio.h>
int main(void)
{
printf("hello, world\\n");
}
This is equivalent to the “when green flag clicked” and “say (hello, world)” block:
We can compare a lot of the constructs in C, to blocks we’ve already seen and used in Scratch. The syntax is far less important than the principles, which we’ve already been introduced to.
The “say (hello, world)” block is a function, and maps to printf("hello, world\\n");
. In C, the function to print something to the screen is printf
, where f
stands for “format”, meaning we can format the string in different ways. Then, we use parentheses to pass in what we want to print. We use double quotes to surround our text, or string, and add a \\n
which indicates a new line on the screen. (Then, the next time we call printf
, our text will be on a new line. Finally, we add a semicolon ;
to end this line of code in C.
The “set [counter] to (0)” block is creating a variable, and in C we would say int counter = 0;
, where int
specifies that the type of our variable is an integer:
“change [counter] by (1)” is counter = counter + 1;
in C. (In C, the =
isn’t like an equation, where we are saying counter
is the same as counter + 1
. Instead, =
means “copy the value on the right, into the value on the left”.) We can also say counter += 1;
or counter++;
both of which are “syntactic sugar”, or shortcuts that have the same effect with fewer characters to type.
A condition would map to:
if (x < y)
{
printf("x is less than y\\n");
}
{
and }
(as well as indentation) to indicate how lines of code should be nested.We can also have if-else conditions:
And even else if
:
if (x < y)
{
printf("x is less than y\\n");
}
else if (x > y)
{
printf("x is greater than y\\n");
}
else if (x == y)
{
printf("x is equal to y\\n");
}
==
, two equals signs.if (x == y)
in the final condition, since that’s the only case remaining, and we can just say else
.Loops can be written like the following:
while (true)
{
printf("hello, world\\n");
}
The while
keyword also requires a condition, so we use true
as the Boolean expression to ensure that our loop will run forever. Our program will check whether the expression evaluates to true
(which it always will in this case), and then run the lines inside the curly braces. Then it will repeat that until the expression isn’t true anymore (which won’t change in this case).
for (int i = 0; i < 50; i++)
{
printf("hello, world\\n");
}
// block
// -------
int i = 0;
while (i < 50)
{
printf("hello, world\\n");
i++;
}
To write a loop that runs a specific number of times, we use the for
keyword, and first, we create a variable named i
and set it to 0. i
is a conventional name for a variable that keeps track of how many iterations of the loop we’ve already done. Then, we check that i < 50
every time we reach the top of the loop, before we run any of the code inside. If that expression is true, then we run the code inside. Finally, after we run the code inside, we use i++
to add one to i
, and the loop repeats.
We can also get input from the user:
string answer = get_string("What's your name?\\n");
printf("%s\\n", answer);
In Scratch, the response will be stored in a variable called “answer”, but in C we can specify the name of the variable. We’ll choose “answer” too, and the type of this variable is string
, which is just a sequence of characters.
And we’ll use printf
to print the string, but we need to specify how. We first pass in "%s
, the string we want to print, which happens to be just %s
. And %s
is a placeholder, into which printf
will substitute the value of the string we pass in next, which we specify as answer
.
And we need this structure because now, we can convert this:
string answer = get_string("What's your name?\\n");
printf("hello, %s\\n", answer);