returning to the start of the game

3 views (last 30 days)
i am writting the code for battleships and there is a section that if yuo answer no then it should take you back to the beginging however this does not work for me and it finishes the progam.
the whole code is withing a while loop which works, but for some reasson when i hit no it does not go back to the start.
this is what it looks like
s=input ('would you like to see the game stastics 1,yes 2,no:')
switch s
case 1
num_player = 20;
WLT = zeros(num_player,3);
%disp stastics
if have_a_winner
WLT(winner_index,1) = WLT(winner_index,1) + 1;
WLT(loser_index,2) = WLT(loser_index,2) + 1;
else
WLT(player1_index,3) = WLT(player1_index,3) + 1;
WLT(player2_index,3) = WLT(player2_index,3) + 1;
end
case 2
disp('main menu')
playOn = false;
break
end
case 1 works however case 2 chould go back to the beginging but it does not, any ideas on how to fix this

Answers (1)

Harikrishnan Balachandran Nair
Hi,
It is my understanding that you have a switch case inside a while loop , and the loop is being terminated on executing the 'break' statement.
This is the expected behaviour as the 'break' statement exits the for/while loop completely .As suggested in the comments, changing the 'break' statement to ' continue' might resolve the issue that you are facing, as 'continue' passes control to the next iteration. Hope this helps!

Categories

Find more on Loops and Conditional Statements in Help Center and File Exchange

Community Treasure Hunt

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

Start Hunting!