Mel Script - the switch statement

 

 

 

A switch statement allows us to perform specific actions based upon a single integer's value. This is often useful when you are dealing with menu's and other user selection. The general form of the switch statement is shown below. If the value of <selection> happens to be one of the <val>'s associated with a case, then the code executed from the case up until the next break statement will be executed. If none of the <val>'s match the <selection> value then the default case is used.

 

 

 


		switch( <selection> ) {
		case <val> :
			// do something 
			break;
		case <val> :
			// do something else 
			break;
		default:
			// do default processing
			break;
		}

 

 

An example

A common place you will see a while loop is when you are reading a file. Since we do not know the size of the file, we need to loop until we reach the end.

 

 



		// this function is called whenever a button is pressed.
		proc buttonPressed(int $val) {
			switch($val) {
			case 1:
				print( "button1 pressed\n" );
				break;
			case 2:
				print( "button2 pressed\n" );
				break;
			case 3:
				print( "button3 pressed\n" );
				break;
			default:
				break;
			}
		}

		{
			// create a window
			window;
				// define the layout of controls added 
				// to the window.
				columnLayout;
					// create a few buttons
					button -label "button1" -c "buttonPressed(1);";
					button -label "button2" -c "buttonPressed(2);";
					button -label "button3" -c "buttonPressed(3);";

			// show the window we last created
			showWindow;
		}