?? apd.htm
字號(hào):
<B>3. Is it possible to write a <TT>for</TT> loop with a body that is never executed?</B><BR>
<BR>
Yes, if the condition is <TT>FALSE</TT> after the initialization, the body of the
<TT>for</TT> loop will never execute. Here's an example:
</DL>
<PRE><FONT COLOR="#0066FF">for (int x = 100; x < 100; x++)
</FONT></PRE>
<DL>
<DD><B>4. Is it possible to nest <TT>while</TT> loops within <TT>for</TT> loops?</B><BR>
<BR>
Yes. Any loop can be nested within any other loop.<BR>
<BR>
<B>5. Is it possible to create a loop that never ends? Give an example.<BR>
</B><BR>
Yes. Following are examples for both a <TT>for</TT> loop and a <TT>while</TT> loop:
</DL>
<PRE><FONT COLOR="#0066FF">for(;;)
{
// This for loop never ends!
}
while(1)
{
// This while loop never ends!
</FONT></PRE>
<PRE><FONT COLOR="#0066FF">
}
</FONT></PRE>
<DL>
<DD><B>6. What happens if you create a loop that never ends?<BR>
</B><BR>
Your program hangs, and you usually must reboot the computer.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading23"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1.</B> What is the value of <TT>x</TT> when the <TT>for</TT> loop completes?
</DL>
<PRE><FONT COLOR="#0066FF">for (int x = 0; x < 100; x++)
100
</FONT></PRE>
<DL>
<DD><B>2.</B> Write a nested <TT>for</TT> loop that prints a 10x10 pattern of <TT>0</TT>s.
</DL>
<PRE><FONT COLOR="#0066FF">for (int i = 0; i< 10; i++)
{
for ( int j = 0; j< 10; j++)
cout << "0";
cout << "\n";
}
</FONT></PRE>
<DL>
<DD><B>3</B>. Write a <TT>for</TT> statement to count from 100 to 200 by 2s.
</DL>
<PRE><FONT COLOR="#0066FF">for (int x = 100; x<=200; x+=2)
</FONT></PRE>
<DL>
<DD><B>4.</B> Write a <TT>while</TT> loop to count from 100 to 200 by 2s.
</DL>
<PRE><FONT COLOR="#0066FF">int x = 100;
while (x <= 200)
x+= 2;
</FONT></PRE>
<DL>
<DD><B>5.</B> Write a <TT>do...while</TT> loop to count from 100 to 200 by 2s.
</DL>
<PRE><FONT COLOR="#0066FF">int x = 100;
do
{
x+=2;
} while (x <= 200);
</FONT></PRE>
<DL>
<DD><B>6.</B> BUG BUSTERS: What is wrong with this code?
</DL>
<PRE><FONT COLOR="#0066FF">int counter = 0
while (counter < 10)
{
cout << "counter: " << counter;
counter++;
}
</FONT></PRE>
<DL>
<DD><TT>counter</TT> is never incremented and the <TT>while</TT> loop will never
terminate.<BR>
<BR>
<B>7.</B> BUG BUSTERS: What is wrong with this code?
</DL>
<PRE><FONT COLOR="#0066FF">for (int counter = 0; counter < 10; counter++);
cout << counter << "\n";
</FONT></PRE>
<DL>
<DD>There is a semicolon after the loop, and the loop does nothing. The programmer
may have intended this, but if <TT>counter</TT> was supposed to print each value,
it won't.<BR>
<BR>
<B>8.</B> BUG BUSTERS: What is wrong with this code?
</DL>
<PRE><FONT COLOR="#0066FF">int counter = 100;
while (counter < 10)
{
cout << "counter now: " << counter;
counter--;
}
</FONT></PRE>
<DL>
<DD><TT>counter</TT> is initialized to <TT>100</TT>, but the test condition is that
if it is less than 10, the test will fail and the body will never be executed. If
line 1 were changed to <TT>int counter = 5;</TT>, the loop would not terminate until
it had counted down past the smallest possible <TT>int</TT>. Because <TT>int</TT>
is <TT>signed</TT> by default, this would not be what was intended.<BR>
<BR>
<B>9.</B> BUG BUSTERS: What is wrong with this code?
</DL>
<PRE><FONT COLOR="#0066FF">cout << "Enter a number between 0 and 5: ";
cin >> theNumber;
switch (theNumber)
{
case 0:
doZero();
case 1: // fall through
case 2: // fall through
case 3: // fall through
case 4: // fall through
case 5:
doOneToFive();
break;
default:
doDefault();
break;
}
</FONT></PRE>
<DL>
<DD><TT>Case 0</TT> probably needs a <TT>break</TT> statement. If it does not, it
should be documented with a comment.
</DL>
<H3><A NAME="Heading24"></A><FONT COLOR="#000077">Day 8</FONT></H3>
<H4 ALIGN="CENTER"><A NAME="Heading25"></A><FONT COLOR="#000077">Quiz</FONT></H4>
<DL>
<DD><B>1. What operator is used to determine the address of a variable?<BR>
</B><BR>
The address of operator (<TT>&</TT>) is used to determine the address of any
variable.<BR>
<B><BR>
2. What operator is used to find the value stored at an address held in a pointer?<BR>
</B><BR>
The dereference operator (<TT>*</TT>) is used to access the value at an address in
a pointer.<BR>
<BR>
<B>3. What is a pointer?<BR>
</B><BR>
A pointer is a variable that holds the address of another variable.<BR>
<BR>
<B>4. What is the difference between the address stored in a pointer and the value
at that </B>address?<BR>
<BR>
The address stored in the pointer is the address of another variable. The value stored
at that address is any value stored in any variable. The indirection operator (<TT>*</TT>)
returns the value stored at the address, which itself is stored in the pointer.<BR>
<BR>
<B>5. What is the difference between the indirection operator and the address of
oper-ator?<BR>
</B><BR>
The indirection operator returns the value at the address stored in a pointer. The
address of operator (<TT>&</TT>) returns the memory address of the variable.<BR>
<BR>
<B>6. What is the difference between <TT>const int * ptrOne</TT> and <TT>int * const
ptrTwo</TT>?<BR>
</B><BR>
The <TT>const int * ptrOne</TT> declares that <TT>ptrOne</TT> is a pointer to a constant
integer. The integer itself cannot be changed using this pointer.<BR>
The <TT>int * const ptrTwo</TT> declares that <TT>ptrTwo</TT> is a constant pointer
to an integer. Once it is initialized, this pointer cannot be reassigned.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading26"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1.</B> What do these declarations do?
<DL>
<DD><B><BR>
a.</B> <TT>int * pOne;</TT><BR>
<B>b</B>. <TT>int vTwo;</TT><BR>
<B>c.</B> <TT>int * pThree = &vTwo;</TT><BR>
<B><BR>
a.</B> <TT>int * pOne;</TT> declares a pointer to an integer.<BR>
<B>b.</B> <TT>int vTwo;</TT> declares an integer variable.<BR>
<B>c.</B> <TT>int * pThree = &vTwo;</TT> declares a pointer to an integer and
initializes it with the address of another variable.
</DL>
<DD><B><BR>
2.</B> If you have an <TT>unsigned short</TT> variable named <TT>yourAge</TT>, how
would you declare a pointer to manipulate <TT>yourAge</TT>?
</DL>
<PRE><FONT COLOR="#0066FF">unsigned short *pAge = &yourAge;
</FONT></PRE>
<DL>
<DD><B>3.</B> Assign the value <TT>50</TT> to the variable <TT>yourAge</TT> by using
the pointer that you declared in Exercise 2.
</DL>
<PRE><FONT COLOR="#0066FF">*pAge = 50;
</FONT></PRE>
<DL>
<DD><B>4.</B> Write a small program that declares an integer and a pointer to integer.
Assign the address of the integer to the pointer. Use the pointer to set a value
in the integer variable.
</DL>
<PRE><FONT COLOR="#0066FF">int theInteger;
int *pInteger = &theInteger;
*pInteger = 5;
</FONT></PRE>
<DL>
<DD><B>5.</B> BUG BUSTERS: What is wrong with this code?
</DL>
<PRE><FONT COLOR="#0066FF">#include <iostream.h>
int main()
{
int *pInt;
*pInt = 9;
cout << "The value at pInt: " << *pInt;
return 0;
}
</FONT></PRE>
<DL>
<DD><TT>pInt</TT> should have been initialized. More importantly, because it was
not initialized and was not assigned the address of any memory, it points to a random
place in memory. Assigning <TT>9</TT> to that random place is a dangerous bug.<BR>
<BR>
<B>6.</B> BUG BUSTERS: What is wrong with this code?
</DL>
<PRE><FONT COLOR="#0066FF">int main()
{
int SomeVariable = 5;
cout << "SomeVariable: " << SomeVariable << "\n";
int *pVar = & SomeVariable;
pVar = 9;
cout << "SomeVariable: " << *pVar << "\n";
return 0;
}
</FONT></PRE>
<DL>
<DD>Presumably, the programmer meant to assign <TT>9</TT> to the value at <TT>pVar</TT>.
Unfortunately, <TT>9</TT> was assigned to be the value of <TT>pVar</TT> because the
indirection operator (<TT>*</TT>) was left off. This will lead to disaster if <TT>pVar</TT>
is used to assign a value.
</DL>
<H3 ALIGN="CENTER"></H3>
<H3><A NAME="Heading27"></A><FONT COLOR="#000077">Day 9</FONT></H3>
<H4 ALIGN="CENTER"><A NAME="Heading28"></A><FONT COLOR="#000077">Quiz</FONT></H4>
<DL>
<DD><B>1. What is the difference between a reference and a pointer?<BR>
</B><BR>
A reference is an alias, and a pointer is a variable that holds an address. References
cannot be null and cannot be assigned to.<BR>
<BR>
<B>2. When must you use a pointer rather than a reference?<BR>
</B><BR>
When you may need to reassign what is pointed to, or when the pointer may be null.<BR>
<BR>
<B>3. What does <TT>new</TT> return if there is insufficient memory to make your
<TT>new</TT> object?<BR>
</B><BR>
A null pointer (<TT>0</TT>).<BR>
<BR>
<B>4. What is a constant reference?<BR>
</B><BR>
This is a shorthand way of saying "a reference to a constant object."<BR>
<BR>
<B>5. What is the difference between passing by reference and passing a reference?<BR>
</B><BR>
Passing by reference means not making a local copy. It can be accomplished by passing
a reference or by passing a pointer.
</DL>
<H4 ALIGN="CENTER"><A NAME="Heading29"></A><FONT COLOR="#000077">Exercises</FONT></H4>
<DL>
<DD><B>1</B>. Write a program that declares an <TT>int</TT>, a reference to an <TT>int</TT>,
and a pointer to an <TT>int</TT>. Use the pointer and the reference to manipulate
the value in the <TT>int</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">int main()
{
int varOne;
int& rVar = varOne;
int* pVar = &varOne;
rVar = 5;
*pVar = 7;
return 0;
}
</FONT></PRE>
<DL>
<DD><B>2.</B> Write a program that declares a constant pointer to a constant integer.
Initialize the pointer to an integer variable, <TT>varOne</TT>. Assign <TT>6</TT>
to <TT>varOne</TT>. Use the pointer to assign <TT>7</TT> to <TT>varOne</TT>. Create
a second integer variable, <TT>varTwo</TT>. Reassign the pointer to <TT>varTwo</TT>.
</DL>
<PRE><FONT COLOR="#0066FF">int main()
{
int varOne;
const int * const pVar = &varOne;
*pVar = 7;
int varTwo;
pVar = &varTwo;
return 0;
}
</FONT></PRE>
<DL>
<DD><B>3.</B> Compile the program in Exercise 2. What produces errors? What produces
warnings?<BR>
You can't assign a value to a constant object, and you can't reassign a constant
pointer.<BR>
<BR>
<B>4.</B> Write a program that produces a stray pointer.
</DL>
<PRE><FONT COLOR="#0066FF">int main()
{
int * pVar;
*pVar = 9;
return 0;
}
</FONT></PRE>
<DL>
<DD><B>5.</B> Fix the program from Exercise 4.
</DL>
<PRE><FONT COLOR="#0066FF">int main()
{
int VarOne;
int * pVar = &varOne;
*pVar = 9;
return 0;
}
</FONT></PRE>
<DL>
<DD><B>6.</B> Write a program that produces a memory leak.
</DL>
<PRE><FONT COLOR="#0066FF">int FuncOne();
int main()
{
int localVar = FunOne();
cout << "the value of localVar is: " << localVar;
return 0;
}
int FuncOne()
{
int * pVar = new int (5);
return *pVar;
}
</FONT></PRE>
<DL>
<DD><B>7.</B> Fix the program from Exercise 6.
</DL>
<PRE><FONT COLOR="#0066FF">void FuncOne();
int main()
{
FuncOne();
return 0;
}
void FuncOne()
{
int * pVar = new int (5);
cout << "the value of *pVar is: " << *pVar ;
}
</FONT></PRE>
<DL>
<DD><B>8.</B> BUG BUSTERS: What is wrong with this program?
</DL>
<PRE><FONT COLOR="#0066FF">1: #include <iostream.h>
2:
3: class CAT
4: {
5: public:
6: CAT(int age) { itsAge = age; }
7: ~CAT(){}
8: int GetAge() const { return itsAge;}
?? 快捷鍵說(shuō)明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號(hào)
Ctrl + =
減小字號(hào)
Ctrl + -