10/12/05 CISC105, Fall 2005 Summary of today's topics: (1) Equivalence of for loops and while loops. (2) Menu driven programs with switch statements. ************ Details (1) 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) (2) Menu driven programs, and the switch statement. void printMenu(void) { /* note string concatentation in next printf */ printf("Please select a choice: \n" "F for Farenheit to Celsius \n" "C for Celsius to Farenheit \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; 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 readOneChar(). 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. The function printMenu() must be declared or defined 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 either "writing a function prototype") Note that when reading or printing a character value, you use %c as opposed to %d or %lf. See tempMenu.c, tempMenu1.c, tempMenu2.c (3) The readOneChar() function Reading exactly one character means being careful about the problem of excess newlines. We should use fflush(stdin) before we do a scanf("%c",&myChar); to be sure that any excess newlines are gotten rid of. (4) Parameters to functions func1.c **** Based in part on notes originally transcribed from P. Conrad's lecture of from October 8, 2004 by Annelies Caniglia. "I give Professor Conrad my permission to copy this file and post it electronically with modifications, provided that this copyright notice is maintained."