Section 2 Lecture 21 - Did it differently, and it worked. Is my solution fine?

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.

3 Likes

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.

2 Likes

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 :slight_smile:

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.

1 Like

Glad that I looked through the posts before moving forward :wink: 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.

Privacy & Terms