while loops beginner. creating a program but stuck on the concept.
2 visualizzazioni (ultimi 30 giorni)
Mostra commenti meno recenti
Reila01
il 14 Apr 2020
Commentato: Walter Roberson
il 14 Apr 2020
im new to while loops and programming in general and im supposed to create a program where the user will enter their starting balance and they can choose to deposit or withdraw a certain amount of money. With every transaction the program will display their new balance. I will also have to write the program so that if their balance is 0 they will see a message saying they dont have money and if its less than 0 to display they have no money and their overdraft. this is the program i have written so far but im still having a little bit of difficulty understanding the concept behind while loops. so when i run my program the while part doesnt run at all and im not sure if the rest of the program is right either.
balance = input('Enter your starting balance: ');
withdraw = input('withdraw amount: ');
deposit = input('deposit amount: ');
while balance < 0
balance = balance - withdraw
fprintf('You have an outstanding balance of %.2f dollars.', balance);
endwhile
disp('end');
3 Commenti
Walter Roberson
il 14 Apr 2020
Octave is an Open Source software package from the Free Software Foundation. MATLAB is a commercial product from Mathworks. The purpose of the Free Software Foundation is to transform society to make it effectively impossible for commercial software products to exist, with software companies transformed from "owning" software into becoming service organizations that people hire to modify software (and release the modified software to the public).
Octave aims to be able to run MATLAB code specifically to destroy Mathworks as a software-owning company.
I used to know someone who insisted that their group spend several years writing code in non-proprietary languages, in order to avoid paying even $10 for a commercial software license. It was the principle with them: to them, it was better that their software be years later and do less, rather than pay a nominal license fee for software usage rights to a company that had spent tens of millions of dollars developing software.
Risposta accettata
Walter Roberson
il 14 Apr 2020
The requirements you set out for us do not have any termination condition. You are supposed to repeatedly ask the user what transaction they want to make, but there is no way stated for the user to indicate that they want to stop. In such a case, where the while loop is to run forever, the condition you give for the while loop should be a condition that is certain to be true. For example,
while true
or
while 1 < 2
or
the_universe_still_exists = true;
while the_universe_still_exists
I would suggest to you that you enhance the options so that the user has a choice to quit as well. For example,
withdrawl = 1;
deposit = 2;
quit = 3;
choice = menu('Choose a transaction', 'Withdrawl', 'Deposit', 'Quit');
while choice == withdrawl || choice == deposit
now do something here
%then
choice = menu('Choose another transaction', 'Withdrawl', 'Deposit', 'Quit');
end
2 Commenti
Walter Roberson
il 14 Apr 2020
elseif can only be used if the most recent incomplete control structure is "if" or "elseif". You start an if about balance and you have an else for that, and you try to have an else at that point. But as soon as you have an else, you can have a block of statements but then you have to have an end statement to terminate the if.
Valid chain example:
if choice == 1
disp('Withdrawing so soon?')
elseif choice == 2
disp('More! Give me more!)
else
disp('Leaving so soon?')
end
Not valid:
if choice == 1
disp('Withdrawing so soon?')
else
disp('Not withdrawing!)
elseif choice == 2
disp(' illegal section')
end
Valid:
if choice == 1
disp('So you want to withdraw?')
if balance < 0
disp('Warning! You are already in debt!)
else
disp('OK you do not owe money yet')
end
elseif choice == 2
disp('withdrawal section')
end
It is not legal to attach an else to the condition tested by while
invalid:
while choice == 1
disp('withdrawal')
elseif choice == 2
disp('illegal here')
end
As soon as the test is false when evaluated for the while condition, the loop stops running.
Notice that in my sample implementation I tested that the choice was either withdrawl or deposit. When you use that menu() call that I showed, choice would not be (withdrawal or deposit) if the user chooses quit or closes the menu without a choice. If the choice is either of the two then do the body of the loop, and inside the loop you can if/else for which one was chosen.
Più risposte (0)
Vedere anche
Categorie
Scopri di più su Octave in Help Center e File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!