4th day in class, no idea what to do... How to stop a loop?

It is my fourth day in a high school robotics class, and we do not have a textbook because the internet.I am going to pick one up soon!
Until then, my homework is to "figure out" how to make this loop of Fibonacci numbers stop after 10 numbers in such a way that he could walk up, know nothing about Fibonacci numbers or anything, and just change the 10 to 20, 25, etc. I thought I could just make it so that while x is greater than 35 to stop, but then he changed the instructions from "just make it stop after 10" to that.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
int x = 0; 
int y = 1; 
int fib; 
void setup ( )
{ 
  Serial.begin(9600);   
  Serial.println(x); }
void loop ( ) 
{   
  x=y;   
  y=fib;   
  fib=x+y; 
  Serial.println(fib);
  delay(500); 
}


He also said that if there's a more efficient way to code this, to do so and get the fewest lines of code possible.

Thank you very much!
-Claire
Could you describe more about what you're trying to do? I wasn't able to get a clear mental picture from your description.
What is the full assignment?
What do you mean by "stopping after 10"?

Once some of that is cleared up I'll gladly help you along the way!

-Thumper
There are three kinds of loops:
1. The check is at the start of a loop
2. The check is at the end of a loop
3. Loop a fixed number of times.

Additionally, in C and C++, you can break out of a loop and you can prematurly go back the the top of a loop.

http://www.cplusplus.com/doc/tutorial/control/
I don't know about java, but in c++, here is a way:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
int NumberOfFibToPrint = 10;
int temp = 0;

void loop ( )
{
    x=y;
    y=fib;
    fib=x+y;
    cout << fib << " ";
    ++temp;
    if (temp == NumberOfFibToPrint)
    {
        temp = 0;
        return;
    }
    else
    {
        sleep(500);
        loop();
    }    
    
}


So I guess:

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
const int NumberOfFibToPrint = 10;
int x = 0;
int y = 1;
int fib;
int temp = 0;

void setup ()
{
    Serial.begin(9600);
    Serial.println(x);
}

void loop ( )
{
    x=y;
    y=fib;
    fib=x+y;
    Serial.println(fib);
    ++temp;
    if (temp == NumberOfFibToPrint)
    {
        temp = 0;
        return;
    }
    else
    {
        delay(500);
        loop();
    }    
    
}
Last edited on
Excuse me! He said that he wanted it to stop after the first 10 digits of the Fibonacci sequence, and he wanted to easily be able to change the number "10" to a different one to view that many numbers in the Fibonacci sequence.

I will try these suggestions! Thank you very much!
Should anyone else need it, this was my solution:

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
int x = 0; 
int y = 1; 
int fib; 
int purple = 2;
void setup ( )
{ 
  Serial.begin(9600);   
  Serial.println(x); 
}
void loop ( ) 
{   
   while ( purple <= 10 )
  { 
    x=y;   
    y=fib;   
    fib=x+y; 
    Serial.println(fib); 
    delay(500);   
    purple++;  
}   
  while (purple > 10)
  {
  }
}
Topic archived. No new replies allowed.