Least Significant Bit

All in all we’re just another bit in the word

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(<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

7 comentarios to “How to teach C to a violinist (Part V)”

  1. rafinhaviolino said

    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 ();
    }

  2. rafinhaviolino said

    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 ();
    }

  3. jayzeegp said

    Well done!😛
    There are 2 left for now😛

  4. rafinhaviolino said

    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 ();
    }

  5. jayzeegp said

    It’s ok but your excuses are bad😛

  6. rafinhaviolino said

    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 ();
    }

  7. jayzeegp said

    Well done, soon you will have a new master class😛

Responder

Por favor, inicia sesión con uno de estos métodos para publicar tu comentario:

Logo de WordPress.com

Estás comentando usando tu cuenta de WordPress.com. Cerrar sesión / Cambiar )

Imagen de Twitter

Estás comentando usando tu cuenta de Twitter. Cerrar sesión / Cambiar )

Foto de Facebook

Estás comentando usando tu cuenta de Facebook. Cerrar sesión / Cambiar )

Google+ photo

Estás comentando usando tu cuenta de Google+. Cerrar sesión / Cambiar )

Conectando a %s

 
A %d blogueros les gusta esto: