using the for loop

Hi,

I'm new to C++, and I'm trying to write some code.
I have written this loop:

1
2
3
4
5
6
7
8
9
10
11
12
for (i=1 ,j=1; i<10 ; i++)
    {
		
	if (A[i] != A[i-j])
	    cout << A[i] << " ";
		else 
			j++;
	    if (i==j)
		{
			break;
		}
	}



I need to replace "break" with a command that goes back to main for loop (doing i++). I've tried using goto but it's not a solution since the loop will start all over again. I've tried using ";" but it doesn't help.

What am I doing wrong? Is there a command like that?
goes back to main for loop


There is only one for loop. If you want it to keep looping, just don't break. You're not making much sense. What does the code above do that you don't like?
could you be more specific ?? what exactly do you want to do ?
I don't want it to break. The break is there just to show where I want to put a command.
I want to do this : if (i==j) do i++ and continue the loop which means back to line 1.
i still don't get it... but why are you using the j in the loop anyway ?! there's a command continue
which will make you go back to line 1 .
I want to check if a certain value in array A was already printed.
I print out each value once.

j is there to change the array index
compare A[9] to A[8] A[7] .... A[0]
If it's the first time this value occurs print it out.
Last edited on
yea got it... then continue is what you're looking for probably .
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
for (i=1 ,j=1; i<10 ; i++)
    {
		
	if (A[i] != A[i-j])
	    cout << A[i] << " ";
		else 
			j++;
	    
        // at this point, if i==j, do nothing, just roll around to the next loop iteration
        if (i != j)
       {
           // all the other stuff you want to do if i != j
           //   it will all be skipped over if i==j
         }
}
Well.. I've been playing with this problem all day long and finally came to a solution. continue and goto as well as you guys helped me.

The code looks 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
#include <iostream>

using namespace std;

int A[10] = {6,3,3,9,3,4,5,4,9,2};
int i,j;

int main()
{
	cout << "The numbers by order without repeating are: \n";
	cout << A[0] << " ";                  

for (i=1 ,j=1; i<10,j<10 ; i++)
{
            Start:
	        if (i>9)
				break;
	        if (A[i] != A[i-j] && i-j>=0)     
			{
				j++;
				goto Start;
			}
			if (j>i)
			{
				cout << A[i] << " ";
			    j=1;
				continue;
			}
			if (A[i] == A[j]) 
			{
				j=1;
				continue;
			}
}
    cout << "\n\n";
	return 0;
}


I have 2 questions.
1. The for loop doesn't stop when i reaches 10 (i<10 as a condition) so I had to add
1
2
if (i>9)
				break;
, Why's that?
2. I think the way I programmed it is kinda sloppy. How could I make it more efficient?
Just to be clear: what you want is having this input: int A[10] = {6,3,3,9,3,4,5,4,9,2};
and generate this output: 6 3 9 4 5 2.
Right?
Yes.

The user can input 10 numbers and the program will show the numbers by order they were input w/o duplications.
I've stated the numbers for debugging :)
Here it is without having to use labels, goto or continue.

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
#include <iostream>

using namespace std;

int A[10] = {6,3,3,9,3,4,5,4,9,2};
int i,j;

int main()
{
  cout << "The numbers by order without repeating are: \n";
  cout << A[0] << " ";                  

  for (i=1 ,j=1; j<10 ; i++)
  {
    if (i>9) break;

    while (A[i] != A[i-j] && i-j>=0)     
    {
      j++;
    }

    if (j>i)
    {
      cout << A[i] << " ";
      j=1;
    }
    else if (A[i] == A[j]) 
    {
      j=1;
    }
  }
  
  cout << "\n\n";
  return 0;
}


Note that this;
i<10,j<10
does not do what you think it does, and for the purposes of ending the for loop, is identical to this: j<10
Last edited on
Thanks , that's better. In your code I can even delete the if (i>9) break; , works fine when I compile.

It's easier for me to use the for loop for some reason (logically).
I guess I need to practice using the while loop more :)
Topic archived. No new replies allowed.