C examples for Statement:for
Using an infinite loop and the switch statement to implement a menu system.
#include <stdio.h> #include <stdlib.h> int menu(void); int main( void ) { int command = 0; command = menu();/* w w w. ja v a 2 s .c o m*/ while (command != 5 ) { /* Get user's selection and branch based on the input. */ switch(command) { case 1: { puts("\nExecuting task A."); break; } case 2: { puts("\nExecuting task B."); break; } case 3: { puts("\nExecuting task C."); break; } case 4: { puts("\nExecuting task D."); break; } case 5: /* Exit program. */ { puts("\nExiting program now...\n"); } default: { puts("\nInvalid choice, try again."); } } command = menu(); } return 0; } int menu(void){ int reply; puts("\nEnter 1 for task A."); puts("Enter 2 for task B."); puts("Enter 3 for task C."); puts("Enter 4 for task D."); puts("Enter 5 to exit program."); scanf("%d", &reply); return reply; }