🎉 Celebrating 25 Years of GameDev.net! 🎉

Not many can claim 25 years on the Internet! Join us in celebrating this milestone. Learn more about our history, and thank you for being a part of our community!

C++ Workshop - Expressions & Statements (Ch. 4)

Started by
41 comments, last by Dbproguy 16 years, 1 month ago
so

int main(){;;;;return 0;}


is legal, but just means "do nothing four times" if I get the "if(){}; issue" right?
Advertisement
It is perfectly legal.
Hello, ( Its not about chapter 4 )
Since the beginning, all the programs we do are executed in the DOS panel. I was wondering if during this workshop we are gonna build programs that use interface. Like, printing "Hello World!" in black letters on a white screen rather than in the DOS command windows. Is this part of the book and if so which day is it ?

Thank You.
I don't think the textbook covers graphical user interfaces.
Quote: Original post by Myotis
Since the beginning, all the programs we do are executed in the DOS panel. I was wondering if during this workshop we are gonna build programs that use interface. Like, printing "Hello World!" in black letters on a white screen rather than in the DOS command windows. Is this part of the book and if so which day is it ?

Strictly speaking, graphical user interfaces are not a feature of C++. You need a library that enables you to generate GUIs for specific target platforms. For Windows the base library is the Win32 C API. For X-Window System workstations, it's Xlib. For Mac OS X, it's Cocoa, with a bit of Objective-C for binding purposes.

It's outside the scope of learning the C++ language.
I have'nt read Day 4 yet, so I'll be catching up with everyone by this weekend, once I've gotten through Day 2 and Day 3.(This workshop is turning out to be more useful then I had imagined, perhaps we will be working on GUIs SOON!!!!)
I Have Awoken.
Quote: Original post by kaydash
I have'nt read Day 4 yet, so I'll be catching up with everyone by this weekend, once I've gotten through Day 2 and Day 3.(This workshop is turning out to be more useful then I had imagined, perhaps we will be working on GUIs SOON!!!!)


This workshop will not be covering Graphical User Interfaces. As Oluseyi already pointed out, GUI Interfaces are something you USE the C++ language to create, they are not inherently part of the language. Generally speaking you use one of a number of available Libraries or API's to assist you in the creation of your GUI's. The scope of this workshop is only the C++ language syntax and semantics.

The Win32 API, or the .NET Framework using C++/CLR is a subject for a later workshop.

<OPINION>
(Nobody should ever be forced to learn MFC) [wink]
</OPINION>

Cheers!
Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints
Quote: Original post by jwalsh
<OPINION>
(Nobody should ever be forced to learn MFC) [wink]
</OPINION>

[opinion]
Except as an illustration of bad design and the consequences of working around the pre-Standardization limitations of a programming language.
[/opinion]

Seriously, AFX_BEGIN_MESSAGE_MAP? *shudder*
Quote: Original post by twoaterisn
so

*** Source Snippet Removed ***

is legal, but just means "do nothing four times" if I get the "if(){}; issue" right?


Yup. It is exactly equivilent to the same code without those four ";" lines.

[ADVANCED]
This is probably slightly beyond the scope of this course, and requires some knowledge of assembly, but it is sometimes handy to see what your code looks like in assembly, and how it compares to similar code to get a better feeel for how your code works on a lower level.

You can get to Visual C++ to output the assembler listings when compiling. This will allow you to see how different pieces of similar code differ from one another. You can make it do this by changing the assembler options output in your project settings in "Common Propperties -> C/C++ -> Output Files -> Assembler Output". After changing that option to "Assembly-Only Listing", when you compile your program, which is made of say main.cpp, it will put a file called main.asm in the same output directory as your executable by default.

I do not recommend this for anyone who doesn't know assembly, and due to the optimized nature of the produced assembly it will only be useful for certain types of issues. But since this is one of them I thought I'd mention it. Also keep in mind that the asm produced by a compiler is unique to that compiler, each one can produce different asm for the same code.
[/ADVANCED]
Greetings All!

Sorry I was unable to post a quiz yesterday, I was unfortunately preoccupied for the majority of the day. However, it's once again QUIZ TIME!!! That's right, listed below are a set of quiz questions to help you test your knowledge and understanding of the material contained in chapter 4. Each chapter builds upon the knowledge obtained in previous chapters, so it can be dangerous and confusing to advance to later chapters without a complete understanding of the material already covered.

NEW: At the end of the chapter quiz you’ll also find chapter exercises. This is new to the weekly quiz, and is now an option because you’ve learned enough information to write useful programs.

In addition to the questions and exercises below, make sure that as you're reading the book you enter the examples into your compiler, build the program, and run the executable. I know this is a time consuming process, but the repeat use of keywords, syntax, and semantics will help ingrain the information into your long-term memory. My advice is to create a simple "driver" project with a function main. As you read, enter the examples into function main, test it, and then erase it for use again in the next example.

PLEASE DO NOT POST THE ANSWERS TO THESE QUESTIONS OR EXERCISES. If you are unable to answer these questions, please ask for assistance, but DO NOT POST THE ANSWERS. Any question which is not marked with [Extra Credit] can be answered by reading your textbook. Questions which are marked [Extra Credit] either have been answered in the thread previously, or can be answered by doing a bit of research.

A new thread will be created in a day or two for you to post your answers to these question.

Chapter 4 Quiz

1. What do statements do in C++?
2. Where can you put a compound statement?
3. What’s another name for a compound statement?
4. What syntax identifies the beginning and ending of a block?
5. What is an expression in C++?
6. Which side of the equal operator can an expression ALWAYS be on? Why not the other side?
7. What do operators act upon?
8. Can an expression be acted upon by operators?
9. What does the assignment operator do?
10. What is an r-value and an l-value? How does this relate to question 6 above?
11. What are the 5 mathematical operators and what do they do? Give examples of inputs and outputs.
12. What happens when you subtract a large unsigned number for a smaller unsigned number?
13. [Extra Credit]What is integer truncation? How can it be avoided?
14. What are the increment/decrement operators, and how are they used. Show examples.
15. What is the difference between prefix and postfix increment/decrement? What are the results? Show examples.
16. What is the precedence of the mathematical operators?
17. Are the operators left-to-right or right-to-left?
18. What about the assignment operator?
19. What operator can ALWAYS be used to change the precedence of an expression?
20. How are nested parentheses read? Show examples.
21. Can all expressions be evaluated for their truth?
22. What data type is exclusively for truthiness? (tip of the hat to Stephen Colbert)
23. What is the ANSI standard size of that data type?
24. What are the six relational operators? Show examples of what values they return with different inputs.
25. What does an ‘if’ statement do?
26. When is an if-block executed, when is it skipped?
27. What does an ‘else’ statement do?
28. When is an else-block executed, when is it skipped?
29. [Extra Credit] What does an ‘else if’ statement do? When is it executed and when is it skipped?
30. What are the 3 logic operators in C++? How are they used? Show examples.
31. What is “Short Circuit Evaluation?” How might it be beneficial?
32. What is the precedence of the logical and conditional operators?
33. What is the conditional operator? How is it used? When might it be useful? Show examples.

Chapter 4 Exercises

1. The equation for distance traveled (dist) with a constant velocity is equal to the rate of movement (rate) multiplied by the time interval (time) traveling. (dist=rate*time) Write a program which prompts the user for rate and time, computes the distance traveled, and then outputs the result to the screen.

2. Write a program that knows a secret number and prompts the user to guess the number. When the user enters the number the program should notify the user whether the number guessed was too high, too low, or juuust right. We will modify this program later when we’ve covered loops in order to allow the user to guess until they get the correct answer or run out of tries. Additionally, we will cover random number generation when we explore functions next week.

Cheers and Good luck!

Jeromy Walsh
Sr. Tools & Engine Programmer | Software Engineer
Microsoft Windows Phone Team
Chronicles of Elyria (An In-development MMORPG)
GameDevelopedia.com - Blog & Tutorials
GDNet Mentoring: XNA Workshop | C# Workshop | C++ Workshop
"The question is not how far, the question is do you possess the constitution, the depth of faith, to go as far as is needed?" - Il Duche, Boondock Saints

This topic is closed to new replies.

Advertisement