General Conditional loops: Loop that repeats while the loop control variable is > 0 Assume we use a certain amount of heating oil each week (a diminishing resource), and we want to display the amount left after each week. printf(“Enter starting amount of oil:”); scanf(“%lf”, &supply); while (supply > 0) { printf(“Enter gallons used:”); scanf(“%lf”, &used); supply -= used; if (supply > 0) printf(“Amount left is %f\n”, supply); else printf(“No oil left\n”); } sample run: Enter starting amount of oil: 1000 Enter gallons used: 90 Amount left is 910 Enter gallons used: 60 Amount left is 850 … Amount left is 50 Enter gallons use: 50 Amount left is Modify loop to print out week # and amount of oil left after each week. Note that week is a counter but is not a loop control variable. week = 0; printf(“Enter starting amount of oil:”); scanf(“%lf”, &supply); while (supply > 0) { week++; printf(“Enter gallons used in week %d:”, week); scanf(“%lf”, &used); supply -= used; if (supply > 0) printf(“Supply at end of week %d is %f\n”, week, supply); else printf(“No oil left at end of week %d\n”, week); } sample run: Enter starting amount of oil: 1000 Enter gallons used in week 1: 90 Supply at end of week 1 is 910 Enter gallons used in week 2: 60 Supply at end of week 2 is 850 … Supply at end of week 19 is 50 Enter gallons used in week 20: 50 No oil left at end of week 20 Let’s change loop so we stop when we run out of oil or we stop after week N (a prior data item or constant). week = 0; printf(“Enter starting amount of oil:”); scanf(“%lf”, &supply); while (supply > 0 && week < N) { week++; printf(“Enter gallons used in week %d:”, week); scanf(“%lf”, &used); supply -= used; if (supply > 0) printf(“Supply at end of week %d is %f\n”, week, supply); else printf(“No oil left at end of week %d\n”, week); } sample run: Enter starting amount of oil: 1000 Enter gallons used in week 1: 90 Supply at end of week 1 is 910 Enter gallons used in week 2: 60 Supply at end of week 2 is 850 … Supply at end of week 9 is 550 Enter gallons used in week 10: 50 Supply at end of week 10 is 500 do while loop: Loop that always executes at least once. Loop repetition condition is tested after each repetition. Loop body repeats if the repetition conditon is true; loop exit occurs if the repetition condition is false. Example: keep reading character data until character * is read. #include #define TERMINATOR '*' int main() { char next; do { printf("Next character or %c:", TERMINATOR); scanf(" %c", &next); } while (next != TERMINATOR); return 0; } Unlike a sentinel controlled loop – this one processes the terminator character (‘*’) like any other. Sample output: Next character or *: x Next character or *: y Next character or *: z Next character or *: * Reason for space in “ %c” – means skip white space (blank or return character). Otherwise, return after x would be treated as 2nd data character, return after y would be treated as 4th data character, and so on. Function get_integer keeps reading numbers until a number between min and max (function arguments) is read. Value of min should be <= value of max. int get_integer(int min, int max) { int next_int; do { printf(“Enter a number between %d and %d:”, min, max); scanf(“%d”, &next_int); } while (next_int < min || next_int > max); return next_int; } Sample call: num = get_int(1, 10); /*num set to value from 1 thru 10*/ volts = get_int(5, 15); /* volts set to value from 5 thru 15 */ Function get_digit keeps reading characters until first digit character is read. Returns first digit character as its “result”. Note: digit characters form a sequence: ‘0’, ‘1’, … ‘9’ char get_digit() { char next; do { printf(“Enter a digit:”); scanf(“ %c”, &next); } while (next < ‘0’ || next > ‘9’); return next; } Sample call: next_ch = get_digit(); What does this function do? char mystery_1() { char next; do { printf(“Enter a character:”); scanf(“ %c”, &next); } while (next < ‘A’ || next > ‘Z’); return next; } Sample call: ch = mystery_1(); What does this function do? char mystery_1() { char next; do { printf(“Enter a character:”); scanf(“ %c”, &next); } while (next < ‘a’ || next > ‘z’); return next; } Sample call: ch = mystery_2();