switch within if statement vs if/elseif efficiency

8 vues (au cours des 30 derniers jours)
Shuai Shao
Shuai Shao le 10 Nov 2017
Commenté : Steven Lord le 10 Nov 2017
Hi!
I'm doing an assignment where I have to code Conway's Game of Life.
Currently I'm attempting to create a GUI for ease of entering initial conditions.
prev=false(m,n)
[rows,cols]=size(prev);
clf
plotroutine(prev)
title({'Instructions'})
gate=true;
while gate==true
[x,y,key]=ginput(1);
if key~=[] %This is line 9!
switch key
case 1
if 0<x&&x<cols&&0<y&&y<rows
col=ceil(x);
row=rows-floor(y);
prev(row,col)=~prev(row,col);
clf
plotroutine(prev)
title({'Instructions'})
end
case 114
prev=logical(randi([0,1],rows,cols));
clf
plotroutine(prev)
title({'Instructions'})
case 32
gate=false;
title([])
end
end
While I was testing edge case scenarios, more specifically the [Enter] and [Windows] key, ginput returned []. However, this is not a valid argument for switch. Thus I had to write line 9 instead.
My question is whether this implementation, or merging the if and switch statements to a single if/elseif statment is more efficient/preferrable from a best practice point of view?
  4 commentaires
Stephen23
Stephen23 le 10 Nov 2017
Modifié(e) : Stephen23 le 10 Nov 2017
@Shuai Shao: I don't see any problem with using if and nested switch, if it makes the code intent clear. Both of these are going to be fast, so I don't think you should be prematurely optimizing this code.
Shuai Shao
Shuai Shao le 10 Nov 2017
Okay, thanks for your input!

Connectez-vous pour commenter.

Réponse acceptée

Stephen23
Stephen23 le 10 Nov 2017
Use whichever one is clearest for understanding the program. You will spend more time reading and fixing the code than one if statement will spend running.

Plus de réponses (1)

Steven Lord
Steven Lord le 10 Nov 2017
Your code doesn't handle the case where the user does something to finish the ginput call other than click the left mouse button, press the 'r' key, or press the space key. Therefore the while loop simply continues. If you want a "catch all" that lets you handle those other cases differently than simply continuing on, use the otherwise keyword.
Inside the otherwise block you could:
  • check if key isempty as Stephen suggested
  • display some text telling the user what they need to do
  • issue a warning
  • throw an error
  • let the while loop simply continue while the otherwise block makes it explicitly clear that that's how you want those other cases to be handled
  • or some combination of any or all of the above.
  2 commentaires
Shuai Shao
Shuai Shao le 10 Nov 2017
Modifié(e) : Shuai Shao le 10 Nov 2017
I think you might've misunderstood the question slightly.
In my implementation the key variable is the argument for the switch statement. Since a 0x0 empty array isn't a valid argument for a switch, I wouldn't be able to check key isempty in an otherwise-keyword in this switch.
As far as I've understood I'm pretty much forced to use an if statement with isempty(key) as argument and redirect to the current switch statement should it not be the case in order to prevent the switch from crashing the program if the user presses an key which returns [], e.g. [Enter].
My question was simply how to handle the flow control;
Is it preferrable to have this nested structure with a switch statement, whose use many seem to advocate.
or
Compile the structure into a single if statement, as such.
if isempty(key)
elseif key==1&&0<x&&x<cols&&0<y&&y<rows
*do something*
elseif key==114
*do something*
elseif key==32
*do something*
As for the need to "catch all", my intention was to let the while loop continue indefinitely until the user pressed spacebar as the full 'instructions' reads:
title({'[LMB] - edits the state of induvidual cells','[R] - generates random values for all cells','[Space] - finalises the initial configuration'})
Sending an error message every time the user presses an insignificant key seems redundant, I simply don't want the program to crash in case the user does (Solved by Stephen C.).
He also provided an answer to the main question, however, I don't know how to mark a topic as solved if the answer was in a comment.
(Edit: The user is selecting cells which are to be alive for the initial state of Conway's Game of Life in case that wasn't clear.)
Steven Lord
Steven Lord le 10 Nov 2017
Ah yes, I had forgotten about switch requiring the expression to be a scalar or a char vector.
You could combine the two.
if isempty(key)
% handle the empty case
else
switch key
% add case and/or otherwise blocks here
end
end
or
if ~isempty(key)
switch key
case 1
% etc
end
end

Connectez-vous pour commenter.

Catégories

En savoir plus sur Board games dans Help Center et File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by