C Programming Tutorial - do while Loops





Do while loop general pattern

#include <stdio.h>



int main()
{
    do{ 
        this code;
    } while(this test is true);

return 0;

}

Code to calculate test average

#include <stdio.h>
int main()
{
    float grade = 0;
    float scoreEntered = 0;
    float numberOfTests = 0; (we can increment it each time)
    float average = 0;
    
    printf("Press 0 when complete. \n\n");
    
    do { 
        printf("Tests:%.0f (to bring this similar to integer) Average: %.2f  \n", numberOfTests, average); (print the stats)
        printf("\nEnter test score: "); (prompt)
        scanf("%f", &scoreEntered); (not an array so use ampersand infront)
        grade += scoreEntered;
        numberOfTests++;
        average = grade / numberOfTests;
    }while(scoreEntered !=0); (program is going to end if we type 0)

return 0;

}

Comments