how to find smallest number among 10 numbers in a if loop?

Hi all I need find the smallest number among 10 numbers using if loop or for loop.Please need help.

1 Comment

There is no such thing as an "if loop". "if" executes the body exactly 0 or 1 times, which is not a loop; "for loop" and "while" (usually) execute multiple times.

Sign in to comment.

 Accepted Answer

Try this:
minValue = inf; % Initialize to infinity
for k = 1 : 10
if array(k) < minValue;
% Surely you can fill in the rest!
end
end
It's probably too much of a hint but it's hard to do much less since it's so short and trivial.

5 Comments

Thank u sir...Please reply for my work.I just have some linelengths as given below
linelen3=l3;
linelen4=l4;
linelen5=l5;
linelen6=l6;
linelen7=l7;
linelen8=l8;
linelen9=l9;
linelen13=l13;
linelen14=l14;
linelen15=l15;
linelen16=l16;
Now i want to find which lines length is the shortest. I tried as
shortestline=linelen3;
linenum=3;
for k=4:16
if linelen(k)<shortestline
shortestline=linelen(k);
linenum=k;
end
end
Please tel me wat mistake i did...
Remove the semicolons from the end of your sentences and see the results of each line.
Calling a variable "linelen5" is a bad idea and a standard mistake of beginners. Use an array instead: "linelen(5)" etc.
linelen = [13, 14, 15, 16, 17, 18, 19, 113, 114, 115, 116]
shortestline = linelen(1)
for k = 2 : length(linelen)
etc.
Thank u all, i got my result

Sign in to comment.

More Answers (0)

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!