1. ## Perodic boundary conditions

I need to write a segment of a code where it allows the program to run in a loop basically...so the range of this code is going to be from 0 - (N-1) for example, I want:

if the code happens to produce -1, I want it to go to N-1 thus creating a loop. Similarly
if the code happens to produce N, I want it to go to 0, again, going back to the beginning to connect the entire sequence...

starting at 0:

Code:
```  if(u < a / 3)
{
dis--;
}
else if(u >= 2 * a / 3)
{
dis++;
}```
dis is the output.

EDIT: I realize that else if statements must end with an else statement, but if the value turns out to be between a/3 and 2a/3, nothing happens, so should I just add:
Code:
```else
{
dis = dis;
}```

so what do I need to add at the end of that if-loop to ensure I won't get any values less than zero and greater than N-1?

2. I have no idea what you're trying to do...maybe an example ?

3. realize that else if statements must end with an else statement
else and else if statements are optional.
Code:
```if(1) {}

if(1) {}
else if(2) {}

if(1) {}
else if(2) {}
else {}```
so what do I need to add at the end of that if-loop to ensure I won't get any values less than zero and greater than N-1?
This?
Code:
```if(value < 0) value = 0;
else if(value > max) value = max;```

4. dis is a value that starts at 0 and will change depending on u:

if u is < a/3, then it will decrease 1.
if u is > 2a/3, then it will increase 1.

if the first value u is < a/3 then the dis will go to -1 and cause the program to end, likewise with when the last u value is > 2a/3 then the dis will go to N, and also cause the program to end. How do I prevent that from occuring?

EDIT: Ah yes dwks, thanks

5. You mean, how do I use a while loop? http://www.cprogramming.com/tutorial/c/lesson3.html

What do you want dis to be after the loop or whatever has ended?

BTW, if you want nothing to happen you can use "{}" or ";":
Code:
```if(1) {}
else ;```

6. as u are decrementing on both the condition

Code:
```if(u==-1)
dis= N;
elseif(u==N)
dis=0;
else
dis--;```
ssharish2005

7. I just want dis to be any number between 0 and N-1. I just think of it as points on a circle. 0..1..2..3...N-1...N(back to 0)...1...2...3..etc. So there will never be N or negative numbers.

8. Code:
```#include<stdio.h>

int main()
{
int num=0, N=5;
int i=0;

while(i++ < 20)
{
if(num == -1)
num = N;
else if(num == N)
num = 0;

printf("%d\t",num++);

if(i%5 == 0)
printf("\n");
}
getchar();
return 0;
}

/*my output
0       1       2       3       4
0       1       2       3       4
0       1       2       3       4
0       1       2       3       4
*/```
something like this

ssharish2005

9. Hm...thank you all for the responses, but there is one problem with my current code. When I run it, it displays nothing. This is the exact code I used previously and now that code(which was fine before) is also displaying nothing. I'm not sure what is the cause of this problem...I can post the code, but it's a bit lengthy and would require a long time to decipher it.

When a program does not run/display anything, usually what are the reasons behind it?

*It does not give me any error when I compile it or any segmentation faults when i run it, just blank*

10. well u can place some printf and see where the control is and finds out where it breaks. Well if u don't get any display most of the time it would in a infinite loop where are no any out statements in that loop or similar. Check that by using some prints statements

ssharish2005

11. ah, figured it out, thanks very much

12. Code:
```  for(trial = 0; trial < N; trial++)
{
nstep = 0;
t = 0.0;
dis = 0;
while(t < to)
{
tau = tao * (1 + g * t);
dt = -log(a) * tau;
u = gsl_ran_flat (r, 0.0, 1.0);
fprintf(stdout, "%lf\n", u);
b[nstep * maxstep + dis]++;
if(u <= 1 / 3)
{
dis--;
}
else if (u >= 2 / 3)
{
dis++;
}
else
{
}
if(dis < 0)
{
dis = 1;
}
else if(dis > 1)
{
dis = 0;
}
t = t + dt;
times[nstep] = t;
nstep++;
}
}```
The purpose of this code is to determine whether a particle hops forward or backwards or stays the same (starting at 0) by using a random number generator. If the number u generated is less than 1/3, then the particle hops backwards by 1 unit. If the number u generated is greater than 2/3, then particle hops forward by 1 unit. If the number u generated is inbetween 1/3 and 2/3, then the particle remains still.

the second part of the code allows the particle to hop continuously:
If a particle hops to dis = N-1, it is to return to 0 and continue the process.
If a particle hops to dis = -1, it is to return to N-1 and continue the process.

The problem with my code is, despite the generated values, the particle does not behave like I intend it to, such as dis does not increase when u > 2/3...etc. Why not?

13. Well, what is the value which is returned by the gsl_ran_flat function. I guess it is not satisfying the if condition and reaching else part of elseif statement without taking any action on dis. Try placing any printf in the else and find the value of dis and check out the if condition.

ssharish2005

14. You seem to be comparing u against the result of an integer division (1/3 = 0). You should tell the compiler that you mean floats: (1.0/3.0)

As to count back and forth, I came up with something like that:
Code:
```#include <stdio.h>
#include <windows.h>

const int MAX = 10;
const int MIN = 0;

int main()
{
int i = 0;
//if i == MIN -> step has to be -1
//if i = MAX -> step has to be 1
//else it won't matter
//You may code proper initialization
int step = -1;
while (1) {
if (i == MAX || i == MIN)
step *= -1;
i += step;
printf("%d ", i);
Sleep(100); //give it a break
}
}```