Exit "FOR" loop if number 0 is entered

Page 1 of 1

11 Replies - 7549 Views - Last Post: 02 December 2008 - 09:37 AMRate Topic: //<![CDATA[ rating = new ipb.rating( 'topic_rate_', { url: 'http://www.dreamincode.net/forums/index.php?app=forums&module=ajax&section=topics&do=rateTopic&t=74009&amp;s=1ed03380fff9fd38f92976de916936e5&md5check=' + ipb.vars['secure_hash'], cur_rating: 0, rated: 0, allow_rate: 0, multi_rate: 1, show_rate_text: true } ); //]]>

#1 kingeedorah

• New D.I.C Head

Reputation: 1
• Posts: 7
• Joined: 28-November 08

Exit "FOR" loop if number 0 is entered

Posted 28 November 2008 - 03:35 PM

Hello!

1st of all, I'm new to the forums, and I would like to say that it's great and it helped me alot on many projects!

The problem is that I have to exit the for loop if user enters 0, but now it writes 0 for the whole lenght of field.

```

int[] numbers = new int[10];
int a, b, temp, length= 10;

for (int i = 0; i < 5; indeks++)
{
Console.WriteLine("Enter {0}. number: ", i + 1);

if (numbers[i] == 0)
{
break;
}
}

for (a = 1; a < length; a = a + 1)
{
for (b = length- 1; b >= a; b = b - 1)
{
if (numbers[b - 1] > numbers[b])
{
temp= numbers[b - 1];
numbers[b - 1] = numbers[b];
numbers[b] = temp;
}
}
}

for (int i = 0; i < length; i++)
{
Console.Write(" " + numbers[i]);
}
```

Is This A Good Question/Topic? 0

Replies To: Exit "FOR" loop if number 0 is entered

#2 modi123_1

• Suitor #2

Reputation: 13954
• Posts: 55,700
• Joined: 12-June 08

Re: Exit "FOR" loop if number 0 is entered

Posted 28 November 2008 - 04:53 PM

Change this:
```for (int i = 0; i < 5; indeks++)
```

To this:
```for (int i = 0; i < 5; i++)
```

#3 kingeedorah

• New D.I.C Head

Reputation: 1
• Posts: 7
• Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 29 November 2008 - 03:36 AM

I forgot to change this when I was translating it to english. Otherwise I got it right

#4 kingeedorah

• New D.I.C Head

Reputation: 1
• Posts: 7
• Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 29 November 2008 - 03:46 PM

anyone?

#5 WushuMonster

Reputation: 13
• Posts: 187
• Joined: 29-February 08

Re: Exit "FOR" loop if number 0 is entered

Posted 29 November 2008 - 04:21 PM

It writes a bunch of zeros before the numbers the user enters because you set the array to 10 elements, but your loop for getting user input only goes for 5. So when you display the output all the unused elements are zero.

#6 oasisjoel

• D.I.C Regular

Reputation: 4
• Posts: 274
• Joined: 11-September 08

Re: Exit "FOR" loop if number 0 is entered

Posted 30 November 2008 - 12:34 AM

if (condition) then

{
exit for;
}

#7 kingeedorah

• New D.I.C Head

Reputation: 1
• Posts: 7
• Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 30 November 2008 - 06:37 AM

```			int[] numbers = new int[10];
int a, b, temp, length= 10;

for (int i = 0; i < 10; indeks++)
{
Console.WriteLine("Enter {0}. number: ", i + 1);

if (numbers[i] == 0)
{
break;
}
}

```

ok it should be like this... (lots of errors at translating the code to english, now onder I wrote it at 2:35 )

But still, if length is 5, and for loop condition is up to 5, and lets say 3rd number that user enters is 0, it will still output 3 zeros and other 2 numbers.

#8 WushuMonster

Reputation: 13
• Posts: 187
• Joined: 29-February 08

Re: Exit "FOR" loop if number 0 is entered

Posted 30 November 2008 - 10:03 AM

In C++ I would create a dynamic array (an array that can take an unknown number of elements). I'm not sure how to do that in C# yet though :-P If you hard code a specific number of elements into an array then all the elements have to have values.

BTW, the easiest way to end a for loop at a certain moment is to just set the counter to whatever your cut off is.

Example:

for (i = 0; i < 10; ++i)
<code to get input>
if input = 0
i = 10

Or you could set up a while loop.

quit = true
while (!quit)
get input
if input = 0
quit = true
else
add number to array
increment array

This post has been edited by WushuMonster: 30 November 2008 - 10:14 AM

#9 kingeedorah

• New D.I.C Head

Reputation: 1
• Posts: 7
• Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 01 December 2008 - 09:48 AM

hm... well I've been thinking.... what if I could reduce the array length for each number.

If 2nd number = 0 then reduce length by 4
If 3th number = 0 then reduce length by 3
If 4th number = 0 then reduce length by 2
If 5th number = 0 then reduce length by 1
and so on...

But I dont have a clue how I could implement it in my code with a loop

```

if (števila[indeks] == 0)
{
dolžina = dolžina - 1; // what a hell :D
break;
}

```

This post has been edited by kingeedorah: 01 December 2008 - 09:50 AM

#10 fremgenc

Reputation: 3
• Posts: 119
• Joined: 15-November 07

Re: Exit "FOR" loop if number 0 is entered

Posted 01 December 2008 - 01:42 PM

```

int[] numbers = new int[10];
int a, b, temp, length= 10;

for (int i = 0; i < 5; indeks++)
{
Console.WriteLine("Enter {0}. number: ", i + 1);

if (numbers[i] == 0)
{
break;
}
}

for (a = 1; a < length; a = a + 1)
{
for (b = length- 1; b >= a; b = b - 1)
{
if (numbers[b - 1] > numbers[b])
{
temp= numbers[b - 1];
numbers[b - 1] = numbers[b];
numbers[b] = temp;
}
}
}

for (int i = 0; i < length; i++)
{
Console.Write(" " + numbers[i]);
}
```

try this, i starred changes i made to your code
```

int[] numbers = new int[10];
int a, b, temp,
int length= 0;//****

for (int i = 0; i < 10; i++)
{
Console.WriteLine("Enter {0}. number: ", i + 1);

if (numbers[i] == 0)
{
break;
}

length++; //*******

}

for (a = 0; a < length; a++)
{
for (b = length- 1; b >= a; b = b - 1)
{
if (numbers[b - 1] > numbers[b])
{
temp= numbers[b - 1];
numbers[b - 1] = numbers[b];
numbers[b] = temp;
}
}
}

for (int i = 0; i < length; i++)
{
Console.Write(" " + numbers[i]);
}
```

that should do the trick. The thing you were missing was the length++ part in your first loop. This will increment the length for each good number(NON-Zero) input.

then when sorting, it will only go up to the amount of numbers put it
and same for displaying.

#11 kingeedorah

• New D.I.C Head

Reputation: 1
• Posts: 7
• Joined: 28-November 08

Re: Exit "FOR" loop if number 0 is entered

Posted 02 December 2008 - 07:38 AM

Thanks alot for your help mates!!! Works like a charm

@fremgenc
Only had to change a from 0 to 1 in bubble sort, 1st for loop

#12 fremgenc

Reputation: 3
• Posts: 119
• Joined: 15-November 07

Re: Exit "FOR" loop if number 0 is entered

Posted 02 December 2008 - 09:37 AM

Ahh yes I thought it was your mistake, evidently mine. Cheers