How to teach C to a violinist (Part V)
Posted by jayzeegp en julio 18, 2008
On this entry, we will learn how to use “conditional schemes”.
We use it on our life everyday, we just need to translate it to C.
Imagine something like: “If it rains, I will take my umbrella”. Another example, “If I press this button, I will send the bombs, if I don’t press it, world will be a better place” (this goes better with our picture).
If we think a little, we will see that conditional schemes have always a condition, and a thing to perform if the condition is satisfied.
On C the syntax is like this:
/**If the condition is satisfied, the program will execute this code**/
/**If the condition is not satisfied, the program will execute this code**/
We can do a conditional scheme with just the if part.
How can I express the conditions?
Less than <
Greater than >
Less or equal <=
Greater or equal >=
Example: Imagine we have two variables (inserted by a user previously) and we want our program to say if they are equal or not:
printf(“A has the same value that B has”);
printf(“A hasn’t got the same value that B has”);
This is a brief explanation, we will come back to it on future.
I want to be a big genious! Exercises please!
Some will be to revise things you did before.
1.- Write a program which asks for a number and then says: The number you entered is …
2.- Write a program which asks for a number and then says if that number is bigger than 20
3.- Write a program which asks for 2 different numbers and it says if the first is bigger than the second or not
4.- Write a program which asks for 2 numbers and it says if one number multiplied by the other is bigger than 20