<< Prev Next >>
Code flow
Until now, our code executed from first line to last line passing through all lines. Sometimes we don't want this, as we want to perform some operations only if certain conditions are met. For this, we have control flow instructions, which will allow us to execute or not blocks of code based on certain conditions.
The most simple control flow instruction is if
Code: Select all
if (expression1)
{
// Code executed if expression1 is true
}
else if (expression2)
{
// Code executed if expression1 is false and expression2 is true
}
else
{
// Code executed if all previous expressions are false
}Code: Select all
switch(variable)
{
case value1:
// Code executed if variable == value1
break;
case value2:
// Code executed if variable == value2
break;
default:
// Code executed if none of the previous cases was executed
}The while loop is a loop control flow statement. This means the block of code enclosed between the while braces could execute more than once. This is very useful when we want to do an operation several times until a condition is fulfilled.
Code: Select all
while(expression)
{
// This code will be executed as long as expression is true
}Another way of writing a while loop is
Code: Select all
do
{
// This code will be execute at least once
} while(expression);Another very used loop construct is the for loop. You can think of this as a special while loop where you do some stuff automatized, so you don't have to include extra code inside the loop
Code: Select all
for(expression1; expression2; expression3)
{
// This code will execute as long as expression2 is true
}The for loop suits better when we know we're going to loop a code a fixed number of times. For example, to calculate the average of an array of numbers, we can use a for loop:
Code: Select all
#include <stdio.h>
// Pre-processor directive: defines
#define ARRAY_SIZE 8
int main()
{
int array[ARRAY_SIZE] = {2, 5, 9, 10, 45, 5, 1, 3};
int total = 0;
// We sum all the numbers on the array
int i;
for (i = 0; i < ARRAY_SIZE; i++)
{
total += array[i];
}
// We calculate the average value of the array and print it
float average = (float) total / (float) ARRAY_SIZE;
printf("The average of the array is %f\n", average);
return 0;
}The #define preprocessor directive allows us to use symbols instead of literals. This is NOT processed by the compiler, this is not real code. What the preprocessor actually does is replace all instances of the ARRAY_SIZE symbol by its value (8) before the file is passed to the compiler. This allows us fast modification of this value if needed, since we only have to modify the #define and not all occurences of the number 8.
So now some exercises to practice this new stuff
For all exercises:
- It's not allowed to use any external library to do the main task of the exercise (string.h is forbidden!
). - Use the #define directive instead of using literals.
- Write a program that given an array of 10 numbers, prints only the even numbers.
- Write a program that given an array of 10 numbers, prints only the odd positions.
- Write a program that given an array of 10 numbers, prints only the 2 first multiple of 3 numbers.
- Write a program that given one string, prints its length.
- Write a program that given 2 ASCII stings as an input, checks if they are exactly equal.
- Write a program that checks if one string is part of another (if it's a substring).
- Write a program that given an array of 10 numbers, it orders it ascendantly.
- Write a program that given 2 ASCII stings as an input, checks if they are exactly equal, but case-insensitive (that is, given "hello" and "HeLLo", it should say they are equal) This one could be a bit tricky

- Write a program that given two matrices float a[3][3] and float b[3][3], adds them.
<< Prev Next >>


