473,411 Members | 2,289 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,411 software developers and data experts.

Problem with nested while

I have this nested while:


int numsx[] = { 11, 22, 33, 44, 55, 77, 88, 99};
int* startx = numsx;
int endx = sizeof(numsx) / sizeof(numsx[0]);
int* endxp = numsx + endx;

int numsy[] = { 1, 2, 3, 9};
int* starty = numsy;
int endy = sizeof(numsy) / sizeof(numsy[0]);
int* endyp = numsy + endy;
while (startx != endxp){
std::cout << " *startx " << *startx << std::endl;

while (starty != endyp) {
std::cout << " *starty " << *starty << std::endl;
starty++;
}
startx++;
}

but when I run it I get:

*startx 11
*starty 1
*starty 2
*starty 3
*starty 9
*startx 22
*startx 33
*startx 44
*startx 55
*startx 77
*startx 88
*startx 99

Any ideas on why the inner while only gets executed once?
Apr 26 '07 #1
2 1428

"desktop" <ff*@sss.comwrote in message
news:f0**********@news.net.uni-c.dk...
>I have this nested while:


int numsx[] = { 11, 22, 33, 44, 55, 77, 88, 99}; int* startx = numsx;
int endx = sizeof(numsx) / sizeof(numsx[0]);
int* endxp = numsx + endx;

int numsy[] = { 1, 2, 3, 9}; int* starty = numsy;
int endy = sizeof(numsy) / sizeof(numsy[0]);
int* endyp = numsy + endy;
while (startx != endxp){
std::cout << " *startx " << *startx << std::endl;
while (starty != endyp) {
std::cout << " *starty " << *starty << std::endl; starty++;
}
startx++;
}

but when I run it I get:

*startx 11
*starty 1
*starty 2
*starty 3
*starty 9
*startx 22
*startx 33
*startx 44
*startx 55
*startx 77
*startx 88
*startx 99

Any ideas on why the inner while only gets executed once?
You need to reset the value of starty, either before or after the inner
loop.

-Howard

Apr 26 '07 #2
On Apr 26, 4:48 pm, desktop <f...@sss.comwrote:
I have this nested while:

int numsx[] = { 11, 22, 33, 44, 55, 77, 88, 99};
int* startx = numsx;
int endx = sizeof(numsx) / sizeof(numsx[0]);
int* endxp = numsx + endx;

int numsy[] = { 1, 2, 3, 9};
int* starty = numsy; // move this line
int endy = sizeof(numsy) / sizeof(numsy[0]);
int* endyp = numsy + endy;

while (startx != endxp){
std::cout << " *startx " << *startx << std::endl;
int* starty = numsy;
while (starty != endyp) {
std::cout << " *starty " << *starty << std::endl;
starty++;
}

startx++;

}

but when I run it I get:

*startx 11
*starty 1
*starty 2
*starty 3
*starty 9
*startx 22
*startx 33
*startx 44
*startx 55
*startx 77
*startx 88
*startx 99

Any ideas on why the inner while only gets executed once?
What is pointer starty set to at the beginning of the second pass of
the inner_loop?
Why should the inner while-loop repeat since starty == endyp following
the first run?
Is that not what you told the program to do?
Apr 27 '07 #3

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Paul Charlton-Thomson | last post by:
Hi, I am trying to execute 2 queries and then output the results in 2 loops but maybe I'm using the wrong approach. Can anyone help me here ... $query_1 = "SELECT field_1 FROM table_1";...
3
by: Ian T | last post by:
Hi, I've got what I think (probably incorrectly) should be a simple SELECT : Two colums with data like col1 col2 1 50 1 51 2 50
36
by: invni | last post by:
I have a nested while. How do I go from the inner while to the beginning of the outer while? Can this be done without using goto? while_1() { some codes here while_2() { if true go to the...
9
by: Gregory Petrosyan | last post by:
I often make helper functions nested, like this: def f(): def helper(): ... ... is it a good practice or not? What about performance of such constructs?
1
by: imranpariyani | last post by:
Hi i have a severe performance problem with one of my views which has 6 to 8 joins .. any help will be appreciated.. the view is: CREATE OR REPLACE VIEW thsn.trade_view AS SELECT...
3
by: monomaniac21 | last post by:
hi all i have a script that retrieves rows from a single table, rows are related to eachother and are retrieved by doing a series of while loops within while loops. bcos each row contains a text...
10
by: sssk28 | last post by:
i have installed fedora core 6 and i am getting following error: type Exception report message description The server encountered an internal error () that prevented it from fulfilling this...
0
by: Maric Michaud | last post by:
Le Tuesday 12 August 2008 23:15:23 Calvin Spealman, vous avez écrit : I was not aware of any "nested classes are unsupported" before and didn't consider nested classes as bad practice till...
8
by: Nathan Sokalski | last post by:
I have several nested For loops, as follows: For a As Integer = 0 To 255 For b As Integer = 0 To 255 For c As Integer = 0 To 255 If <Boolean ExpressionThen <My CodeElse Exit For Next If Not...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.