Notes from October 8. By Annelies Caniglia. (Edited by P. Conrad, 12/14/2004) I give Professor Conrad my permission to copy this file and post it electronically with modifications, provided that this copyright notice is maintained. We started class by going over for loops. A for loop is a shorthand way of writing a while loop. Here is a piece of code with a while loop: int i; i=2; while(i<=10) { printf("%d ", i); i+=2; } Here is a way to write the same piece of code, but with a for loop instead of a while loop: int i; for(i=2; i<=10; i+=2) { printf("%d ", i); } In the for loop the first term is the initialization, the second term is the condition, and the third term is the increment. In a while loop the initialization goes before the while loop, the condition goes in the while statement, and the increment is one of the things that happens in the loop (typically at the end, though not always) After going over for loops, Professor Conrad put the following piece of code on the board: void printMenu(void) { printf("Please select a choice: \n" "F for Farenheit to Celsius \n" "C for Celsius to Farenheit \n" "W for Windchill \n" "Q to Quit \n" "Enter choice: "); return 0; } int main(void) { char options; printMenu(); option=readOneChar(); while(option != 'Q') { switch(option) { case 'F': doFarenheitToCelsius(); break; case 'C': doCelsiusToFarenheit(); break; case 'W': doWindChill(); break; default: Printf("Sorry wrong choice. \n"); } printMenu(); option=readOneChar(); } printf("Bye bye \n"); } This code is an example of a menu driven program. This program is incomplete because it does not include the functions doFarenheitToCelsius(), doCelsiusToFarenheit(), and doWindChill(). However, the program would do the following if it were complete: When the program is run, the program calls the function printMenu(), which prints the menu. Then the function "readOneChar is called, where the user enters which option he or she would like to perform, and the program returns to the main function. Based on the option, one of several functions is called, and an action is performed (the switch statement controls which option results in which action). For example, if the user enters C, the program will convert a celsius temperature to a farenheit temperature. Professor Conrad pointed out that the function printMenu must be declared before main. If it is not declared or defined before main, there will be an error in your program at the part where you call the function. (PS: "declaring" a function means "writing a function prototype"). He also pointed out that when reading or printing a character value, you use %c as opposed to %d or %lf. This was the end of class on October 8.