Presentation is loading. Please wait.

Presentation is loading. Please wait.

getting a choice from the user

Similar presentations


Presentation on theme: "getting a choice from the user"— Presentation transcript:

1 getting a choice from the user
function [response] = getchoice( ); % get user choice of problem to solve % needs: % results: response user choice % % ... creator info ... % variable dictionary % response user choice % display choice menu disp(' ') disp('Which problem would you like to solve?') disp(' 1. Entropy of system') disp(' 2. Enthalpy of system') disp(' 3. Free engergy of system') % get choice response = input('\nEnter number of choice ==> '); Might be wise to include error checking!!!! Engr 0012 (04-1) LecNotes 07-01

2 menu command creates pop-up menu menu title button names
choice = menu( 'Enter problem choice', ... 'Entropy', 'Enthalpy', 'Free Energy' ) creates pop-up menu menu title button names click on button name choice assigned “value” of button selected: 1, 2, 3, … Advantage: only valid value can be entered Engr 0012 (04-1) LecNotes 07-02

3 switch…case structure
>> switchexample select a choice take umbrella take hat take hat and sunblock Engr 0012 (04-1) LecNotes 07-03

4 switch…case structure
switchexample.m % switch...case example script % header omitted action = menu( 'weather outside?', ... 'raining', 'cloudy', 'sunny' ); switch action case 1 fprintf( '\n take umbrella \n ' ) case 2 fprintf( '\n take hat \n ' ) case 3 fprintf( '\n take hat and sunblock \n ' ) end % end switch Engr 0012 (04-1) LecNotes 07-04

5 switch…case structure
decision variable name % switch...case structure switch var_name case val1 value 1 actions case {val2, val3} value 2 or 3 actions case val3 value 4 actions otherwise default actions end % end switch possible values for variable must be integer or character actions to take if variable has given value -- may be more than one action! can take same actions for more than one value!!! actions to take if variable has none of the listed values may be omitted Engr 0012 (04-1) LecNotes 07-05

6 information flow in a script
??? how can you be sure that correct information is flowing from function to function through the script? Engr 0012 (04-1) LecNotes 07-06

7 what will be displayed by the following?
% script m = 4 n = 5 [a,b] = confuse1(m,n) ... function [a,b] = confuse1(m,n) a = 2*m; b = 3*n-m; Engr 0012 (04-1) LecNotes 07-07

8 what will be displayed by the following?
% script m = 4 n = 5 [m,n] = confuse2(n,m) ... function [n,m] = confuse2(m,n) m = 3*m n = 2*n-m Engr 0012 (04-1) LecNotes 07-08

9 programming assignment 04
due Wed 24 Sep class activity 07 : ca07_e12(04-1).pdf Engr 0012 (04-1) LecNotes 07-09


Download ppt "getting a choice from the user"

Similar presentations


Ads by Google