Q:

Write a GUI function that will graphically demonstrate the difference between a for loop and a while loop. The function will have two push buttons: one that says ‘for’, and the other says ‘while’

0

Write a GUI function that will graphically demonstrate the difference between a for loop and a while loop. The function will have two push buttons: one that says ‘for’, and the other says ‘while’. There are two separate callback functions, one associated with each of the pushbuttons. The callback function associated with the ‘for’ button prints the integers 1 through 5, using pause(1) to pause for 1 second between each, and then prints ‘Done.’ The callback function associated with the ‘while’ button prints integers beginning with 1 and also pauses between each. This function, however, also has another pushbutton that says ‘mystery’ on it. This function continues printing integers until the ‘mystery’ button is pushed, and then it prints ‘Finally!’.

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

loopGUI.m

function loopGUI

f = figure('Visible', 'off','color','white',...

 'Position', [360, 500, 400,400]);

movegui(f,'center')

hbutton1 = uicontrol('Style','pushbutton','String',...

 'for', 'Position',[150,275,100,50], ...

 'Callback',@callbackfn1);

hbutton2 = uicontrol('Style','pushbutton','String',...

 'while', 'Position',[150,175,100,50], ...

 'Callback',@callbackfn2);

hstr = uicontrol('Style','text',...

 'BackgroundColor','white','Position',...

 [150,200,100,100],'FontSize',30,...

 'ForegroundColor','Red','Visible','off');

set(f,'Visible','on');

 function callbackfn1(source,eventdata)

 set([hbutton1 hbutton2],'Visible','off');

 set(hstr,'Visible','on')

 for i = 1:5

 set(hstr,'String', int2str(i));

 pause(1) % pause for 1 second

 end

 set(hstr,'String','Done!')

 

 end

 function callbackfn2(source,eventdata)

 set([hbutton1 hbutton2],'Visible','off');

 set(hstr,'Visible','on')

 cbmb = uicontrol('Style','pushbutton',...

 'String','mystery', 'Position',[300,50,50,50], ...

 'Callback',@cbfn,'Visible','on');

 done = false;

 i = 0;

 while ~done

 i = i + 1;

 set(hstr,'String',int2str(i));

 pause(1)

 end

 set(hstr,'String','Finally!')

 

 function cbfn(source,eventdata)

 done = true;

 end

 end

 end

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now