This should work, because for all intents and purposes, in an if statement like this, 1 is identical to true.
while (AskToPlayAgain())
while (AskToPlayAgain() == true)
while (AskToPlayAgain() == 1)
These are all essentially identical statements.
Before saying anything else, let me condition my response with this: if youâre the only one looking at your code, then use whatever method you are most comfortably with.
Now, with that said, I myself prefer the first method, because it leaves no doubt as to what Iâm expecting AskToPlayAgain() to return. What I mean is, with the first method (and the second one), you knowâand other developers looking at your code will knowâthat AskToPlayAgain() is returning a boolean value. Whereas, by comparing it to an explicit integer value, some may incorrectly assume that AskToPlayAgain() is returning an integer value, which can lead to misperceptions and headaches.
Thatâs what I did as well. Seems cleaner to me. I think you would only create a variable to hold the return value from the method if you are going to use it more than once. Here we are not doing that, so using it directly seems like the best choice to me.
This is also the solution I came to on my own. Decided to pop in here and see if there were any best practices reasons to do it as shown in the video over what I did.
I did it similarly:
do
{
PrintIntro();
PlayGame();
}
while (AskToPlayAgain());
return 0; // exit application
I assumed that since AskToPlayAgain is returning True, I could just stick that function in the while statement and itâll do the loop until false. As far as I can tell, it worksâŚ
Anyway, I play through the lecture and I got confused. Can you explain what is happening and why to do it this way?
bool bPlayAgain = false;
do
{
PrintIntro();
PlayGame();
bPlayAgain = AskToPlayAgain();
}
while (bPlayAgain);
return 0; // exit application
From what I understand:
You set up bool bPlayAgain, and make it false;
Change it to true by making it equal to AskToPlayAgain();
And do the loop while true.
Can I cut out the bPlayAgain middle man?
Please use code formatting
Hello, I also came up with the solution, which already is stated before in the above discussion:
do
{
PrintIntro();
PlayGame();
} while (AskToPlayAgain());
I got the same question as the others: What are the advantages of doing it the way as it is shown in the lecture?
I came up with all 3 of the answers Bryant_McCraken outlined
The code outlined in the lecture seems redundant to me.
The AskToPlayAgain function returns a boolean, so we donât need to assign this to another boolean variable, but can just use it directly
while (AskToPlayAgain())
or
while(AskToPlayAgain() == True)
Either of these alternatives seem to me to be the simplest and clearest code.
Iâll do it line by line for you:
bool bPlayAgain = false;
Here we set âbPlayAgainâ to false at the beginning of the application. This is always good practice for any boolean you use to loop something with.
bPlayAgain = AskToPlayAgain();
Here we say that âbPlayAgainâ will equal whatever the returned value of âAskToPlayAgain();â ends up being. If the method returns false then bPlayAgain will be stored as âfalseâ, and visa versa.
while (bPlayAgain);
Here we say that we shall repeat the âdoâ part of the loop âwhileâ bPlayAgain is true. This is because while will only loop if the statement is true. In this case our statement will literally be the boolean true or false. Since bPlayAgain has the stored value of what the method AskToPlayAgain returned it will simply be true or false depending on whether y or n was typed in. If bPlayAgain is true then the loop will continue.
There is no real advantage to doing it this way other than it is more obvious what is going on. The reason Ben types it this way is because if you do:
do {
PrintIntro();
PlayGame();
} while (AskToPlayAgain());
Anyone who reads this bit of code will have to ask themselves âHmm. What does the method AskToPlayAgain() do?â. Instead doing the way Ben shows there is no confusion that you are using a boolean that starts as false to loop the do/while loop. Thereâs no need to look at what AskToPlayAgain() does because all that matters is whether the value of the variable bPlayAgain is false or not. As is evidence by anyone who looks at the code.
We can argue all day about this being a relatively simply code block and it probably doesnât apply here; however, your code in main() will soon get much more complex so building this habit now is better than having super clean code. That can be worried about later.
tl;dr: Yes you can cut out bPlayAgain. Caveat is donât expect anyone to easily understand your code without having to go looking at your functions. The purpose of doing it this way is to get into the habit of making code that is easy to understand with a first pass. Itâs very frustrating to look at a main() with a bunch of methods that arenât clear and require extensive lookup to piece together the process.
@ben Please correct me if I am wrong. I am assuming that the paragraph above explains why you chose that way though it wasnât super clear from watching the video lecture.
I came up with the same method as the one in the first post:
do {
PrintIntroduction();
PlayGame();
} while (AskToPlayAgain() == 1);
Iâm glad to see that I wasnât wrong and from @FrankyG170 's post I could understand what @ben did in the lesson. Thank you @FrankyG170 170 for a very good explanation.
Glad that I looked through the posts before moving forward I ended up with a similar solution but was concerned when our instructor said that he couldnât think of a way to accomplish this while being easy to read. Having seen Benâs comment on this I now understand. In case you others didnât notice, Ben referred us to use code formatting ^. Knowing that formatting helps us not only when we are working on our own projects but for others that get their hands on our stuff it makes sense to keep things standardized. One thing I would like to know though, @ben, if you wouldnât mind, if I was writing something for completely personal use and I knew that it wasnât going to be shared, could this method cause conflict in the future? Is your format strictly to adhere to Unrealâs code formatting or is there an underlying reason as well?
Thanks all who read this, I hope you all are enjoying your learning experiences as much as myself.