C Programming Tutorial - while Loops






General pattern for while loop.

#include <stdio.h>

int main()
{
    while(test){
        code;
    }
    return 0;
}

Repeating forever Loop

#include <stdio.h>



int main()


{
    int apple = 1;

    while(apple<5){

     printf("apple is now %d \n", apple);

    }

    return 0;

}


set limit to while loop


#include <stdio.h>



int main()


{
    int apple = 1;

    while(apple<5){

     printf("apple is now %d \n", apple);
     apple ++;

    }

    return 0;

}
run it until the text become false 

Calculating amount example

  This is something that you guys probably seen before. my math teacher taught it to me when I was in like a sixth grade or something like that. He's like alright would you rather have million dollars right now or I can take a penny and double it every single day for a month and then you can have however much money that is at the end and everyone is like take a million dollars and so what he did is he actually figured it out.

#include <stdio.h>
int main()
{
    int day = 1;
    float amount = .01;

    while(day<31){

     printf("Day%d \t Amount : $%.2f\n", day, amount);
     amount *=2; 
     day++;

    }

    return 0;

}


Comments