Azzera filtri
Azzera filtri

Why is this returning "logical 1"

6 visualizzazioni (ultimi 30 giorni)
Rachel Dawn
Rachel Dawn il 7 Feb 2018
Modificato: Matt J il 7 Feb 2018
This is my code:
userAge = input('How old are you?');
if userAge >0 && userAge <7
disp('Oh that''s so cute!')
elseif userAge >= 7 && userAge < 23
disp('What school do you go to?')
elseif userAge >= 23 && userAge < 65
disp('Do you work?')
elseif userAge >= 65 && userAge <=110
disp ('Are you retired yet?')
elseif userAge <0
disp ('That''s impossible.')
else userAge > 110
disp('That''s highly improbable.')
end
When I enter any number that is over 110 (like 112 or 956), Matlab returns this:
How old are you? 956
ans =
logical
1
That's highly improbable.
But with any other number, the "logical 1" does not come out. Is there something wrong with my code?
thank you!!
  1 Commento
Rachel Dawn
Rachel Dawn il 7 Feb 2018
Wait just kidding. I got it. It's because I have "else" on that line rather than "elseif". So then when would we ever use else? When we don't have any condition at all?

Accedi per commentare.

Risposte (1)

Matt J
Matt J il 7 Feb 2018
Modificato: Matt J il 7 Feb 2018
You might use ELSE as follows,
userAge = input('How old are you?');
if userAge >0 && userAge <7
disp('Oh that''s so cute!')
elseif userAge >= 7 && userAge < 23
disp('What school do you go to?')
elseif userAge >= 23 && userAge < 65
disp('Do you work?')
elseif userAge >= 65 && userAge <=110
disp ('Are you retired yet?')
elseif userAge <0
disp ('That''s impossible.')
else
disp('That''s highly improbable.')
end

Community Treasure Hunt

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

Start Hunting!

Translated by