Problem with nestled while-loop and if-statements

Hi!
I need to create a script where the number of months gets input an indefinite number of times for an indefinite number of individuals as a number between 1 and 12 (e.g. the numbers for winter are 12, 1 and 2 and the numbers for summer are 6, 7 and 8.) The question should be repeated an indefinite number of times and the code should be cancelled when the sum quantity of winter and summer months in total is bigger than 5 or if a number outside the interval (1-12) is input.
Finally, the number of people born in the summer and winter needs to be printed out in the end.
The code that I have written thus far is written down below, but I can't seem to get the total winter and summer months added up. For example: If I write 12 as an input, the total number of winter months is still 0. I suspect it has something to do with the nestled while-loop and if-conditions.
I am also trying to get the code to continue asking for month of birth as an input.
month=0; %% variable for number of months that is defined
n=0; %% variable for quantitiy of numbers for input is defined
sum=0; %% variable for winter and summer months is defined
summer=0; %% variable for summer months is defined
winter=0; %% variable for winter months is defined
month=input('Input month of birth as a number: '); %% month of birth input
while (month >= 6) && (month <=8 ) && (month == 12) && (month <= 2) && (month > 0) %% while-loop: conditions for winter and summer
sum=sum+1; %% winter and summer months is defined
while (sum <= 5) && (month >= 1) && (month <= 12) %% while-loop condition: sum can't be higher than 5 and input of numbers must be between 1-12
if (month >= 6) && (month <= 8) %% if-loop condition: number of input is between 6 and 8
n=n+1; %% number of input gets counted
summer=summer+1; %% months of summer gets counted
elseif (month == 12) | (month == 1) | (month == 2) %% elseif-loop condition: executed if number of input is 12,1 or 2
n=n+1; %% number of input gets counted
winter=winter+1; %% months of winter gets counted
else
n=n+1; %% else-loop condition: number of months gets counted
end %% if-loop terminated
end %% second while-loop terminated
end %% first while-loop terminated
disp(['Quantity of people born in the summer = ', num2str(summer)]); %% output for months of summer that has been input
disp(['Quantity of people born in the winter = ', num2str(winter)]); %% output for months of winter that has been input

3 Comments

Hi Andy,
Just to begin with, there is no number that can satisfy all the 'and' conditions in the first while loop.
Thanks for the response!
I understand. I changed the code accordingly and substituted three AND-operators with three OR-operators in the two while-loops as written down below.
The problem with the end sums still remains though (that is, entering 12 as an input still yields the total number of winter months as being equal to 0).
while (month >= 6) && (month <=8 ) || (month == 12) || (month <= 2) && (month > 0) %% while-loop: conditions for winter and summer
while (sum <= 5) && (month >= 1) && (month <= 12) %% while-loop condition: sum can't be higher than 5 and input of numbers must be between 1-12
I managed to solve it in the end! :)

Sign in to comment.

Answers (0)

Products

Release

R2018b

Asked:

on 5 Feb 2019

Commented:

on 9 Feb 2019

Community Treasure Hunt

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

Start Hunting!