Because coding is fun!

while loop

Learn to code in the C language (1/3)




Gentle introduction to coding computers, using the C programming language, in well under 90 minutes (this is part 1 of 3).

After the first three "gentle introduction lessons" I will do another series of videos with an example game and more advanced subjects (graphics, sound, etc). You have to start somewhere and this first hour is really a soft way to create some sort of familiarity with what C code looks like and how to read it.

You get a link to a free MagPi magazine issue and links to the example code in the video description on Youtube.

You can see the video and then read the first three chapters of the magazine, but I recommend you do it the other way around: read the first three chapters and then see the video.

Do not be in a hurry to "read every lesson" or "watch every video"! Your time will be better spent trying to write code similar to the examples and experimenting with your new found skills. In fact, it is crucial that you take it slow, type code, make mistakes and learn with them.

Do it in the morning, after a good night sleep (trust me, you will learn faster, caffeine is not a replacement for a good night sleeping). Programming is hard and you are not dumb. If you have questions please leave a comment in the video, I will try to answer as many of those as possible.

Suggestions after the first three lessons:

1 - Write a program that counts up from 0 to 10 (hey, its a reversion of the "ten little Indians" example, how hard can it be?).

2 - how about a program that prints all the numbers that are multiples of 3 (examples 6, 9, 12, etc) bellow 30?

3 - Can you make the previous exercise without declaring floating point numbers (using only type casting, that is explained in this video)?

Have fun! Because learning is meant to be a fun experience. Go for it!




Example 1 used in the video:


/*
* This is a comment... :-)
* A multi-line example
*/
#include


void main(void)
{
// another type of comment... single line
printf("You can do it, programming is hard, and you are not dumb.\n So persevere and you will get it!\n\n");
}


Example 2 used in the video:


/*
* lets learn aboutariables and arithmetic :-)
*/
#include

void main(void)
{
int apples=10;
int persons=3;
float portion;

portion = apples / persons;

printf("%d apples, divided by %d people means you will each one eat %f apples!\n\n", apples, persons, portion);

// opsss... hey, "3" is not the correct answer! This is our first ever computer bug!



// now lets tell the compiler this is an operation that requires the "int" values to be considered "float"
portion = (float) apples / (float) persons;

printf("%d apples, divided by %d people means you will each one eat %f apples!\n\n", apples, persons, portion);

}


Example 3 used in the video:


/*
* lets learn aboutariables and arithmetic :-)
*/
#include

void main(void)
{
float apples=10;
float persons=3;
float portion;

portion = apples + persons;

printf("%f apples, divided by %f people means you will each one eat %f apples!\n\n", apples, persons, portion);
}



Example 4 used in the video:


/*
* Lets learn about conditions and comparisons, and meet the "While" loop :-)
*/
#include

void main(void)
{
int indians=10;

while(indians>0)
{
if(indians==10)
{
printf("Ten little indians gathered in an island\n");
}
else
{
printf("%d little indians left...\n", indians);
}

// the misterious murderer kills another little indian here...
indians = indians - 1;
printf("One little indian died...\n");
}



printf("... and then, there were none...\n\n");
}