Amazon.com Widgets Chapter 5 - Program Looping
Welcome, Guest. Please login or register.
Did you miss your activation email?
July 23, 2014, 06:53:00 PM
Home Help Search chat Login Register 
News: Read this please.The Great Kangaroo Escape Looking for reviews of the 4th ed on Amazon!   Twitter:  @skochan
                     

+ Official Forum for Programming in Objective-C (the iPhone Programming Language) - Stephen Kochan
|-+ Study Group
| |-+ Objective-C Quizzes
| | |-+ Chapter 5 - Program Looping
| | | |-+ Chapter 5 - Program Looping
Pages: [1] Go Down
Print
Author Topic: Chapter 5 - Program Looping (Read 4675 times)
skochan
Administrator
Hero Member
*****
Posts: 3114







on: March 20, 2009, 08:42:01 AM

This is a 10 question quiz.

Chapter 5 Quiz.
Logged
amuso
Jr. Member
**
Posts: 68


Hope to make a living coding...

amuso@scw.net




Reply #1 on: October 07, 2009, 11:09:15 PM

These quizes are great. I myself have learned a few things by taking them. Thanks for making them for us.

I have a slight problem with one of the questions in this quiz though. So here is a really newbie question Smiley

You ask what the output from the following code is:

Code: (Objective-C)
for (int i=5, x=0; i>0; --i)
   x -= i;

NSLog (@"%i", x);

While I finally understood what the result would be, I had problems at first doing it in my head. So I pasted into Xcode to see if I did it right. However Xcode didnt like this and complained x was not initalized. So actually it doesnt produce a result. But by changing the code a bit I got it right.

Code: (Objective-C)
int x, i;
for (i=5, x=0; i>0; --i)
   x -= i;

NSLog (@"%i", x);

But was your intention to throw us off with the forcing of the type integer, or was it intended to declare the variables x and i in the for-loop ?
Logged

MacBook MB063 4GB OS X 10.7.3 (Xcode 4.3)
iMac MA876 3GB OS X 10.7.3
iPhone 4S 16GB 5.0.1 / iPad 2 16GB 5.0.1
Need translations to Norwegian? Happy to help.
skochan
Administrator
Hero Member
*****
Posts: 3114







Reply #2 on: October 08, 2009, 11:37:28 AM

No, that's my fault.  In the example, since x and i are declared in the start of the for loop, their scope is limited to the loop.   So x and i both go out of scope (and are therefore flagged as undeclared) when the loop is done.

Cheers,

Steve Kochan
Logged
Pages: [1] Go Up
Print
Jump to:



Login with username, password and session length

Powered by MySQL Powered by PHP Powered by SMF 1.1.11 | SMF © 2006-2009, Simple Machines LLC Valid XHTML 1.0! Valid CSS!
Entire forum contents (c) 2009 classroomM.com. All rights reserved.