How do I break while loop
Ältere Kommentare anzeigen
Hi guys, I'm new to matlab. I have to following code. I want to break the while loop if enter valid promocode(HAPPY10) and when ask_promocode=='N'. How can I do? sending SOS to all the expert here;'(
Thank you in advace for helping me
ask_promocode=input('Do you have any promocode?Y or N ','s');
if ask_promocode=='Y'
while str_promocode=='HAPPY10'
str_promocode=input('Please enter your promocode=','s');
if str_promocode=='HAPPY10'
fprintf('Congrats!You can have the discount capped at RM5\n');
break
end
else
fprintf('Oh dear, you have enter invalid promocode!\n')
end
elseif ask_promocode=='N'
fprintf('Ohh It is sad to heard that! Dear you may proceed to payment now<3 \n');
break
end
else
fprintf('Hi dear, we can not reach you. Try again!\n');
end
end
5 Kommentare
David Fletcher
am 5 Jun. 2021
Bearbeitet: David Fletcher
am 5 Jun. 2021
A few observations that may (or may not) help:
The program will never (or should never) enter the while loop since the input for the promocode is after the decision to enter the loop. In reality it may enter because the == operator probably will give you something you didn't quite expect. You would be better using the strcmp function for the condition on the while loop - try typing 'HAPPY10'=='HAPPY12' - is the result what you expected). Though, as mentioned above the promocode input needs to be before the loop enters or the loop condition will never be satisfied.
The if str_promocode=='HAPPY10' decsion in the while loop is redundant - to get to that point str_promocode must be 'HAPPY10' since it is the condition on the loop. Though again, look at using strcmp instead of the == operator
poor kitty
am 5 Jun. 2021
David Fletcher
am 5 Jun. 2021
Bearbeitet: David Fletcher
am 5 Jun. 2021
That is how the function is applied - in this instance HAPPY10 would contain a true/false result based on a comparison between whatever the user entered in str_promocode and the character 'Y' (though you might want to compare it against the actual promocode of 'HAPPY10' rather than 'Y' since you';ve asked the user to enter a promocode, rather than whether or not they have a promocode)
You may want something more like this - I've left out the 'cannot reach you' bit since I wasn't sure what it meant (possibly if the user enters something other than 'Y' or 'N' to the having a promocode question?).
clear
ask_promocode=input('Do you have any promocode?Y or N ','s');
while strcmp(ask_promocode,'Y')
str_promocode=input('Please enter your promocode=','s');
if strcmp(str_promocode,'HAPPY10')
fprintf('Congrats!You can have the discount capped at RM5\n');
break
else
fprintf('Oh dear, you have enter invalid promocode!\n')
end
end
if strcmp(ask_promocode,'N')
fprintf('Ohh It is sad to heard that! Dear you may proceed to payment now<3 \n');
end
poor kitty
am 5 Jun. 2021
David Fletcher
am 5 Jun. 2021
Ok - you are using loops like if constucts rather than the normal function of a loop, but whatever works for you.
Antworten (1)
Sulaymon Eshkabilov
am 5 Jun. 2021
This has to be removed:
if ask_promocode=='Y'
end
Because "while ... end" loop will take care of this issue that makes a user enter "Y" or "N". This is not very robust though as is, but it works.
The crucial error in your code is that while loop never gets its set condition met, e.g.:
while str_promocode=='HAPPY10'
That can be substituted with:
while isempty(str_promocode) % if a user skips to enter any any answer, then it has to keep asking to input Y or N
str_promocode=input('Please enter your promocode=','s');
...
end
%%
Kategorien
Mehr zu Loops and Conditional Statements finden Sie in Hilfe-Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!