Member-only story
Conditionals and loops in MATLAB
2 min readApr 6, 2022
IF, else, if-elseif, if-elseif-else, for loop,
1. If statements
The basic form of if statements in MATLAB is:-
if logical expression
statements
end
Here, The end statement marks the end of the statements and are executed if the logical expression is true.
2. The else statements:-
The basic form of the else statement is:-
if logical expression
statement 1
else
statement 2
end
Example 1:-
a= input ('enter a=');
b=input ('enter b=');
c=input ('enter c=');
if c==2
disp('c is equal to 2')
elseif a~=c
disp('a is not equal to c')
end
Example 2:- If-elseif-else Statement
x=input('enter the value of x:- ');
if x>10
y=x+1
elseif x<0
y=x+2
else y=sqrt(x)
end
Example 3:- For loop :-
total=0;
for k=1:15
total=5*k.^2-2*k+total;
end
disp('The sum for the 15 term is :-' )
disp(total)
Above script is to compute the sum…