Because coding is fun!

For loops

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




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

After the first three "gentle introduction lessons", in part 1, here is a pack of the following four chapters. 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.

Example 4 used in the video:


/* Chapter 7 of Essencials_C_v1.pdf
* Arrays & strings
*/

#include

int main(int argc, char *argv[])
{
// declaring the array of chars without initiaziling it (giving it no values)
char a_string[10];
// declaring the array of chars and initiaziling it (giving it some values for some of the letters)
char b_string[10]="world";

// arrays of int's
int some_number[3];
int more_numbers[3]={ 4, 5, 6};

// initialing the array of numbers we previously declared but did not initialize...
some_number[0]=1;
some_number[1]=2;
some_number[2]=3;

// initializing the array we previously declared but did not initialize... the hardest way possible way
// exactly like we did with numbers would look like this:
/*
a_string[0]='h';
a_string[1]='e';
a_string[2]='l';
a_string[3]='l';
a_string[4]='o';
a_string[5]=0; // strings are null terminated!
*/

// there is a function to replace this (takes less code and it is the same as the manual procedure)
// you can use it like this:
sprintf(a_string, "hello");
// a_string is a POINTER to the memory address of the first element of the string: a_string[0]

// formating code is important, take advantage of multiple lines and make it more readable.
printf("%s %s %d %d %d %d %d %d\n", a_string,
b_string,
some_number[0], some_number[1], some_number[2],
more_numbers[0], more_numbers[1], more_numbers[2]);

return(0);
}



Example 5 used in the video:


/*
* Chapter 4 of Essencials_C_v1.pdf
* "for" loops & fow control using "if" / "else if" /else
*/

#include

int main(int argc, char *argv[])
{
// the "for" loop syntax: set the starting value; end condition; change each loop step
for(int a=1; a<=10; a=a+1)
{
printf("%d", a);

// there is more than one way to do flow control: using "if / else" this time
if(a==10)
{
printf("... last value reached! have a nice day\n");
}
else if(a==1)
{
printf("... just starting... we have a long way to go...\n");
}
else
{
printf("\n");
}
}

return(0);
}



Example 6 used in the video:


/*
* Chapter 4 of Essencials_C_v1.pdf
* "for" loops & fow control using "switch"
*/

#include

int main(int argc, char *argv[])
{
// the "for" loop syntax: set the starting value; end condition; change each loop step
for(int a=1; a<=10; a++) // instead of "a=a+1" you could use "a++" or "a += 1". same thing!
{
printf("%d", a);

// there is more than one way to do flow control: using switch this time...
switch (a) {
case 10: printf("... last value reached! have a nice day\n");
break;

case 1: printf("... just starting... we have a long way to go...\n");
break;

default: printf("\n");
}
}

return(0);
}



Example 7 used in the video:


/*
* Chapter 4 of Essencials_C_v1.pdf
* Endlessloops and breaking out of them
*/

#include

int main(int argc, char *argv[])
{
int a=1;

for(;;) // the "for" loop syntax endless loop
{
printf("%d... just starting... we have a long way to go...\n", a);
a += 1; // the same as: a=a+1
break;
}

while(1) // any value other then 0 is "true", so this results is a endless loop
{
if(a==10)
{
printf("%d... last value reached! have a nice day\n", a);
break;
}
else
{
printf("%d\n", a);
}

a++; // the same as: a=a+1
}
return(0);
}



Example 8 used in the video:


/*
* Chapter 5 of Essencials_C_v1.pdf
* Pointers
*/

#include

int main(int argc, char *argv[])
{
int myvalue=5;
int othervalue=10;

// declaring with an "*" before the name means "it is a pointer"
// placing an "&" before a variable means "pointer to the variable"
int *ptr_myvalue = &myvalue;
int *ptr_othervalue = &othervalue;

// an "*" before the variable name means "value of content stored in pointer"
printf("At memory location %p we have stored the value %d\n", &myvalue, *ptr_myvalue);
printf("At memory location %p we have stored the value %d\n\n", &othervalue, *ptr_othervalue);

// so, *******think******* and... figure these out! Really, understand the results, do not skip ahead!
// Programming is hard and you are not dumb. So use that amazing brain of yours!
myvalue = *ptr_othervalue * 3;
printf("%d\n", myvalue);

myvalue = *ptr_othervalue + othervalue;
printf("%d\n", myvalue);

myvalue = *ptr_myvalue + *ptr_othervalue;
printf("%d\n", myvalue);

// that is not all you need to learn about pointers, but these are important concepts...
// more pointer talk comming, when we learn about arrays...

return(0);
}



Example 9 used in the video:


/* Chapter 6 of Essencials_C_v1.pdf
* Functions... reuse code in smaller, smarter and easier to read/manage blocks
*/

#include

void show_address_and_content(int *a) // passing a pointer to a function that returns no value
{
printf("At memory location %p we have stored the value %d\n", a, *a);
}

int multiply(int a, int b) // passing two integers to a
{
printf("%d\n", a * b);
return(a * b);
}

int sum(int a, int b)
{
printf("%d\n", a + b);
return(a + b);
}

int main(int argc, char *argv[])
{
int myvalue=5;
int othervalue=10;
int *ptr_myvalue = &myvalue;
int *ptr_othervalue = &othervalue;

show_address_and_content(ptr_myvalue);
show_address_and_content(ptr_othervalue);
myvalue = multiply(*ptr_othervalue, 3);
myvalue = sum(*ptr_othervalue, othervalue);
myvalue = sum(*ptr_myvalue, *ptr_othervalue);

return(0);
}


Example 10 used in the video:


/* Chapter 7 of Essencials_C_v1.pdf
* Arrays & strings
*/

#include

int main(int argc, char *argv[])
{
// declaring the array of chars without initiaziling it (giving it no values)
char a_string[10];
// declaring the array of chars and initiaziling it (giving it some values for some of the letters)
char b_string[10]="world";

// arrays of int's
int some_number[3];
int more_numbers[3]={ 4, 5, 6};

// initialing the array of numbers we previously declared but did not initialize...
some_number[0]=1;
some_number[1]=2;
some_number[2]=3;

// initializing the array we previously declared but did not initialize... the hardest way possible way
// exactly like we did with numbers would look like this:
/*
a_string[0]='h';
a_string[1]='e';
a_string[2]='l';
a_string[3]='l';
a_string[4]='o';
a_string[5]=0; // strings are null terminated!
*/

// there is a function to replace this (takes less code and it is the same as the manual procedure)
// you can use it like this:
sprintf(a_string, "hello");
// a_string is a POINTER to the memory address of the first element of the string: a_string[0]

// formating code is important, take advantage of multiple lines and make it more readable.
printf("%s %s %d %d %d %d %d %d\n", a_string,
b_string,
some_number[0], some_number[1], some_number[2],
more_numbers[0], more_numbers[1], more_numbers[2]);

return(0);
}