Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
Solved

# Stacks

Posted on 2003-10-31
Medium Priority
330 Views
please explain to me how this for statement works:

assuming that stack is a class with stack type set to int and stack capacity to 5.  Give the content of myTop and myArray data memebers of s after code segment has been executed.

Stack s;
for (int i = 0; i<5; i++)  //please help me in this step, what is the first value of i
s.pop();
s.pop();

the answer according to my professor is myTop = 2 and myArray is 0, 2, 4, 6 but please help me understand why it is so. i had this question on an exam and i need to understand it.
0
Question by:needle176
[X]
###### Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

• Help others & share knowledge
• Earn cash & points
1 Comment

LVL 5

Accepted Solution

lwinkenb earned 80 total points
ID: 9662062
If you were going to put that for loop into english, it would sound like:
set an integer 'i' equal to 0.  while 'i' is less than 5 run the corresponding body, and add 1 to 'i' after each iteration through the loop.

So basically the for loop in your example runs 5 times.  The first time i is equal to 0, then 1, then 2, until it reaches 4.  When i is equal to 5 it wont run because the condition in the for loop says to run while i<5.

since you are pushing 2*i onto the stack, you will basically push the values 0, 2, 4, 6, 8.  Then the last two commands will pop 8, and 6 from the stack.
0

## Featured Post

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

IntroductionThis article is the second in a three part article series on the Visual Studio 2008 Debugger.  It provides tips in setting and using breakpoints. If not familiar with this debugger, you can find a basic introduction in the EE article loc…
Introduction This article is a continuation of the C/C++ Visual Studio Express debugger series. Part 1 provided a quick start guide in using the debugger. Part 2 focused on additional topics in breakpoints. As your assignments become a little more …
The goal of the video will be to teach the user the concept of local variables and scope. An example of a locally defined variable will be given as well as an explanation of what scope is in C++. The local variable and concept of scope will be relat…
The viewer will be introduced to the member functions push_back and pop_back of the vector class. The video will teach the difference between the two as well as how to use each one along with its functionality.
###### Suggested Courses
Course of the Month8 days, 1 hour left to enroll