How to teach C to a violinist (Part V)
Publicado por 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(<condition>){
/**If the condition is satisfied, the program will execute this code**/
}else{
/**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?
Equally ==
Less than <
Greater than >
Less or equal <=
Greater or equal >=
Different !=
Example: Imagine we have two variables (inserted by a user previously) and we want our program to say if they are equal or not:
if(a==b){
printf(“A has the same value that B has”);
}else{
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
rafinhaviolino escribió
Look! Look! Loooooook! x]~
int
main () {
int a;
printf (“Enter a number: “);
scanf (“%d”,&a);
printf (“The number you entered is %d”,a);
getchar ();
getchar ();
}
rafinhaviolino escribió
I did right again!!!! I’m loving it! x]~
int
main () {
float a;
printf (“Enter a number: “);
scanf (“%f”,&a);
if(a>20){
printf (“%f is bigger than 20″,a);
}else{
printf (“%f is not bigger than 20″,a);
}
getchar ();
getchar ();
}
jayzeegp escribió
Well done!
There are 2 left for now
rafinhaviolino escribió
The 3rd one, sir! It took more time because I was busy those days. And you know it! :p~
int
main () {
float a;
float b;
printf (“Enter a number: “);
scanf (“%f”,&a);
printf (“Enter another number: “);
scanf (“%f”,&b);
if(a>b){
printf (“%f is bigger than %f”,a,b);
}else{
printf (“%f is not bigger than %f”,a,b);
}
getchar ();
getchar ();
}
jayzeegp escribió
It’s ok but your excuses are bad
rafinhaviolino escribió
Now the last one, teacher!
Correct me and then give me another class!
hehehehe
I wanna be a genious x]~
int
main () {
float a;
float b;
printf (“Enter a: “);
scanf (“%f”,&a);
printf (“Enter b: “);
scanf (“%f”,&b);
if(a*b>20){
printf (“%f is bigger than 20″,a*b);
}else{
printf (“%f is not bigger than 20″,a*b);
}
getchar ();
getchar ();
}
jayzeegp escribió
Well done, soon you will have a new master class