returning to the start of the game

1 view (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;
WLT(player1_index,3) = WLT(player1_index,3) + 1;
WLT(player2_index,3) = WLT(player2_index,3) + 1;
case 2
disp('main menu')
playOn = false;
case 1 works however case 2 chould go back to the beginging but it does not, any ideas on how to fix this
  1 Comment
Voss on 6 Jan 2022
What if you change break to continue?

Sign in to comment.

Answers (1)

Harikrishnan Balachandran Nair
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!

Community Treasure Hunt

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

Start Hunting!