Monday, February 23, 2009

Introduction to Programming :: Switch Statements

The switch statement is a very useful structure in that it will allow for user input to control the flow of the program. In a switch statement there can be multiple cases. Each case can do a particular function. This is beneficial because a program may only need to do a certain operation every other run time. This example code below is from a binary tree program. This example shows a do-while loop controlling the menu and how long the switch statement will last. While the input from user is not 6, the loop will continue to print the menu to the screen and allow for another case to be selected. Much of the following code was left intact in order to show an example of a type of program all computer science majors should expect to see. The focal points of the program are the do-while loop and the switch statement within that loop.

No comments:

Post a Comment