Least Significant Bit

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

How to teach C to a violinist (Part VI)

Posted by jayzeegp en julio 29, 2008

Today we will talk about loops!

What is a loop?

This is a loop =)

Now you know all you should know about loops.

But if you want to be an expert, continue reading

The first loop that we will learn is the while loop.

The scheme is the following:

while (<condition>){

/**It will execute these commands while the condition is true**/

}

As you can imagine, we can make “infinite loops” (poor skateboarder). For instance:

int a=3;

while (a>1){

printf(“I’m the best violinist programming C\n”);

}

It will never stop printing that on screen. It is funny, but not useful.

Let’s imagine something more useful.

Imagine that you want to do a question with 4 possible answers and just one is the correct one.

For instance the program must ask where was Beethoven born.

int

main(){

int answer=0;

while(answer!=2){

printf(“Where was Beethoven born?\n”);

printf(“1.-France \n2.-Germany\n3.Pernambuco\n4.Italy\n”);

scanf(“%d”,&answer);

}

}

It will continue asking until the user inserts number 2. The condition has the same format than on if-else, so you can do lots of things.

Exercises please!

1.- This one is so easy but you will have to think a little before writing the code. I want you to make a program which ask for a number, and then prints a sentence (be creative :P) on screen this number of times.

For instance:

Insert a number: 4

Hello

Hello

Hello

Hello

2.- Modify the program you did on exercise now to show how many times it remains to appear. In fact this exercise is a “clue” to know how to do the first one.

For instance:

Insert a number: 4

Hello -> Remain 3

Hello -> Remain 2

Hello -> Remain 1

Hello -> Remain 0

5 comentarios to “How to teach C to a violinist (Part VI)”

  1. rafinhaviolino said

    Ohhh!!!
    I tried! Again and again… =(
    But it’s hard!!! I think I’m not the best violinist programming C…
    But I won’t quit!
    Give me more time…😀

  2. rafinhaviolino said

    I did it right! I did it right! I did!!!
    Wanna see?
    hahahahaha^^’
    thanks for the help x]~

    int
    main () {
    int a;
    printf (“Enter a number: “);
    scanf (“%d”,&a);
    while(a>0){
    a=a-1;
    printf (“I did it right!\n”);
    printf(“Now a is %d and when it goes to the beggining of the while again it will go inside if %d is more than 0\n”,a,a);
    getchar();
    }
    getchar ();
    getchar ();
    }

  3. rafinhaviolino said

    Isn’t that the 2nd exercise was easy! But I wanted to do something funny… and I puted one thing more!^^’
    Will you be angry for it? x]~
    I’m so happy cause I did it!
    Hahahahaha
    But it’s sad because I realized one thing. I’m not a genious… just if genious can lose time thinking about silly things!
    Look the detail of the conditional! Hihihihi

    The thing is… how I’m learning C to have fun, let’s have fun!
    I will tell you a story…

    int
    main () {
    int a;
    printf (“Um belo dia, numa floresta nao muito distante daqui, uma familia de coelhinhos foi passear na floresta. Quantos coelhinhos tinha essa familia? “);
    scanf (“%d”,&a);
    while(a>0){
    a=a-1;
    if (a+1>1){
    printf (“Entao os %d coelhinhos foram passear alem da montanha. O lobo mau apareceu e comeu um coelhinho e so %d coelhinhos voltaram de la.\n”,a+1,a);
    getchar();
    }else{
    printf (“Finalmente %d coelhinho foi passear alem da montanha. O lobo mau apareceu e comeu esse coelhinho e nenhum coelhinho voltou de la.\n”,a+1);

    }
    }

    getchar ();
    getchar ();
    }

  4. rafinhaviolino said

    But I have a problem…
    BUAAAAAA!
    (genious with problems?! o.O)
    Well…
    my silly program says “Entao os 2 coelhinhos foram passear na montanha. O lobo mau apareceu e comeu um coelhinho e so 1 COELHINHOS VOLTARAM DE LA.”
    =(

    How can I correct it, teacher?!
    I don’t wanna my coelhinhos to have problems of “personalidade multipla”.^^’

  5. jayzeegp said

    You can make another if else into the first if in order to print the last part of the sentence and use plural or singular.

    if (a>1){
    blablabla
    if(a==2){
    blabla
    }else{
    blabla
    }
    } /**This one is for the first if**/
    else{
    blabla
    }

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: