How to go from if..end to switch..end?

Hi, simple question, how can i go from if..end to switch..end?
soma=0
for j=1:5
if j==2 | j==4
continue
end
soma=soma+j
end
Thank you!

 Accepted Answer

Hi Dinis,
Is that what you are looking for?
soma=0
for j=1:5
switch j
case 2
continue
case 4
continue
end
soma=soma+j
end

2 Comments

YES!
Thank you!!
Your welcome :) Please accept answer if it works correctly.

Sign in to comment.

More Answers (1)

Kevin Phung
Kevin Phung on 21 Jan 2019
Edited: Kevin Phung on 21 Jan 2019
Not sure what you are doing with your loop, but according to what you have, switching from if/else to switch/case:
soma=0
for j=1:5
switch j
case {2,4} % will check if j is 2 OR 4
continue % if true, then run this
end
soma=soma+j
end

Categories

Find more on MATLAB in Help Center and File Exchange

Tags

Asked:

on 21 Jan 2019

Commented:

on 21 Jan 2019

Community Treasure Hunt

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

Start Hunting!