break loop.

Pages: 12
1
2
3
4
5
6
7
8
9
10
11
12
13
for(int j=10;j<b;j--)
	{
		if(arry[j]>a)
		{
			arry[j] = arry[j+1];
		}
		else if(arry[j]<a)
		{
			a = arry[j+1];
			break;// i want to break loop here.
		}
		
	}


i would like to break the loop from inside the selection part.can it be done??
Hi all...i'm new here n would really gratefull for any guides.for the above question..please help me. :)
//sorry i forgot the greetings~how rude of me.
Have you even compiled your program yet?
yes of course..but somehow the code doesnt work.do u know how to break the loop from inside the ~else if{}.bcause i dont intend to solve the program here.just want to know the way for the break statement to work like i want.
for(int j=10;j<b;j--)
Are you sure about the j<b part?
If j starts at 10 and is decremented it's going to stay <b until the value of j wraps back round to a positive number.
well...i stored a value into the' b'.so,dont worry about that.
OWh,i see my mistake...thanx alot Mooce.
that solve a problem...then,makes me curious..the compiler doesnt detect that??!!haha..
thanx alot...but my real question doesnt solve yet.i'm looking forward for any suggestion.
closed account (z05DSL3A)
the compiler doesnt detect that?
There is no reason why it should.

but my real question doesnt solve yet.i'm looking forward for any suggestion.
Are you sure that it is possible for arry[j] < a to be true?
Last edited on
owh yeah...i'm noob.haha :>
answer my question please...=.=
should i refreash this page everytime i want to see new post..kind of tedious.doesnt it??
never mind...i just need answer for my question please...=.=
closed account (z05DSL3A)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
#include <iostream>

using namespace std;

int main ()
{
    const int ar_size = 10;

    int int_ar[ar_size] = {1,2,3,4,5,6,7,8,9};
    int a = 6;
    for(int i = 0; i < ar_size; ++i)
    {
        if(int_ar[i] > a)
        {
            int_ar[i] = int_ar[i+1];
        }
        else if (int_ar[i] < a)
        {
            a = int_ar[i+1];
            break;
        }
    }
    std::cout << a << std::endl;
}


As you can see the loop does break from there but only if the execution path can get there, so are you sure that it is possible for arry[j] < a to be true?
Last edited on
yes
Last edited on
does it work..??i want to break the entire loop..not for the selection only.is it the code??
Grey: why cant it be true??'a' got value stored in.then,the comparison is between values n should work.
closed account (z05DSL3A)
I can't answer for your particular case but lets say a is -1 and all values in the array are positive, then no value in the array will ever be less than a so it will never be true and never get to break.

ok..rather than having this chit~chat,i think this would be clearer.
actually,i'm working on a simple sorting project....like this.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
#include <iostream>
using namespace std;

int main()
{
	int a =0;
	int b =0;
	int arry[10] = {1,20,38,4,5,67,78,3,5,8};
	cout<<"the list before sorted: "<<endl;

	for(int h=0;h<10;h++)
	{
		cout<<"list no."<<h+1<<" = "<<arry[h]<<endl;
	}//display all array's element.

	
		for(int i=0;i<10;i++)
		{
			if(arry[i]>arry[i+1])
			{
				arry[i+1]= a;//copy target into a temp.
				b = i+1;
			}
			break;
		}

	for(int j=b;j<0;j--)
	{
		if(arry[j]>a)
		{
			arry[j] = arry[j+1];
		}
		else if(arry[j]<a)
		{
			a = arry[j+1];
			break;// i want to break loop here.
		}
		
	}
	

	cout<<"after sorted: "<<endl;
	for(int k=0;k<10;k++)
	{
		cout<<"list no."<<k+1<<" = "<<arry[k]<<endl;//display sorted array.
	}


	return 0;
}
i got problem to transfer the target into a temporary variable n get it into where it should belong.
n i thinnk this is because of the break statement.am i wrong?

n of course this program doesnt finish yet...
Last edited on
Like Grey Wolf was saying none of these values are <a
 
int arry[10] = {1,20,38,4,5,67,78,3,5,8};

a is always 0.

so the else is never reached and therefore the break command isn't ether.

also

for(int j=b;j<0;j--)
should be
for(int j=b;j>0;j--)
so it loops from b down while j >0
and you need to set b to something other than 0 first.
Last edited on
hmm...i see that now.thanx again mooce however why 'a' couldnt store any value from the array.
arry[i+1]= a;//copy target into a temp
i purposely do this to store value in 'a'.
Pages: 12