how I will check the Conditional statement task?

E
Ethan · Dec 10, 2020 · 2K views
Question
Suppose I have a time intervel t = 1:1:100; I can caterize the time intervel as odd and even, So depending on the odd and even,I can execute only to statement inside the conditional if else statement as   if mod(t,2)==1 [task 1] else [task 2] end But how can we check at least 5 tasks i.e say task 1,task 2........... task 5 for each time step starting from t= 1 to 100? More clarly, if t=1, execute task 1 if t=2execute task 2 if t=3, execute task 3 if t=4, execute task 4 if t=5, execute task 5 if t= 6, execute task 1 if t= 7execute task 2 if t= 8, execute task 3 if t=9, execute task 4 if t=10, execute task 5 . if t= 96, execute task 1 if t=97 execute task 2 if t=98, execute task 3 if t=99, execute task 4 if t=100, execute task 5
Expert Answer
Profile picture of Kshitij Singh
Kshitij Singh PhD Expert
Answered Nov 20, 2025

The obvious way is to use a switch construct.

 

for i = 1:100
  switch mod(i,5)
    case 0

    case 1

    case 2

    case 3

    case 4
      
  end
end

If you had more cases, I would probably try to be more creative.

 

Conditional statements enable you to select at run time which block of code to execute. The simplest conditional statement is an if statement. For example:

% Generate a random number
a = randi(100, 1);

% If it is even, divide by 2
if rem(a, 2) == 0
    disp('a is even')
    b = a/2;
end

if statements can include alternate choices, using the optional keywords elseif or else. For example:

a = randi(100, 1);

if a < 30
    disp('small')
elseif a < 80
    disp('medium')
else
    disp('large')
end
Have a different question? Ask here

Get a Free Consultation or a Sample Assignment Review!