# Arduino Tutorial for Beginners – Arduino Strings and Loops

In this post on Arduino Tutorial For Beginners, this topic about Strings and Loops

+ Loops, we have two common loop types that we often use in Arduino:
– The for loop (which I used in the previous topic)
– The while loop

This is the syntax how to create a “for” loop, in the “for” loop we have 4 parts:
– Initialize loop variables (create a begin value)
– Check condition expression (check condition with the end value in order to continue or exit)
– Execute the statements (run program)
– Update variables (increase or decrease)
It’s corresponding to 4 positions below:
```for(Initialize; Condition; Update variables) { Execute; }```
This is an example:
```for (int j = 0; j < 3; j++) { Serial.println(j); }```
The code above will:
– Initialize the integer j = 0
– Then check the condition if j = 0, less than 3 or greater than 3. If less than 3, it will execute the program and increase j by 1, but if j greater than 3. The program will exit the loop

We can initialize the integer j = 0 outside like this:
```int j = 0; for (j; j < 3; j++) { Serial.println(j); }```

That’s the syntax of “for” loop, I will run step by step for you to understand:
– First when j = 0, j is less than 3 -> the program will print 0, then increases j by 1 (j=1 now)
– Next when j = 1, j is less than 3 -> the program will print 1, then increase j by 1 (j=2 now)
– Next when j = 2, j is less than 3 -> the program will print 2, then increase j by 1 (j=3 now)
– At this time, j = 3, j cannot be less than 3 -> it will not print anymore -> your program will exit the loop.
Let’s try to compare the results on Serial Monitor with the code above then try the example as the previous article.

+ Next we will move to the “while” loop, actually “while” loop is similar to “for” loop, but it’s slightly different from the syntax.
This is the syntax and how to create a “while” loop, in the “while” loop we also have 4 parts:
– Initialize loop variables (we must initialize this variable outside the loop, if not, your program will be error)
– Check condition expression
– Execute the statements
– Update variables
It’s corresponding to 4 positions below:
```Initialize integer j While (Condition) { Execute; Update variables; }```

We will re-use the example above.
```int j = 0; while (j < 3) { Serial.println(j); j++; }```
That’s the syntax of “while” loop, I will run step by step for you to understand:
– First when j = 0, j is less than 3 -> the program will print 0, then increases j by 1 (j=1 now)
– Next when j = 1, j is less than 3 -> the program will print 1, then increase j by 1 (j=2 now)
– Next when j = 2, j is less than 3 -> the program will print 2, then increase j by 1 (j=3 now)
– At this time, j = 3, j cannot be less than 3 -> it will not print anymore -> your program will exit the loop.
Let’s try to compare the results on Serial Monitor with the code above then try the example as the previous article.
+About the continue and break, you can use both of them in all loops:
– Continue, this will ignore the program below continue line inside loop, and then update the variable j
– Break, this will exit the loop
Let try code below and see how Continue and Break works:

```for(int j = 0; j < 5; j++) { if(j == 1) continue; // Ignore the program below```

```if(j == 4) // Exit the loop break;```

Serial.println(j);
}

Okay, Now we can use for or while to make a loop in our project.

+ Second is String, in the previous article i used the string, but now I will introduce some applications about String.

When using Serial.println() function you usually put a string inside: Serial.println (“string”);
But now I will transfer a String parameters to this function, let’s see an example:

String myWelcomeString = “Welcome to Programming Knowledge”;
Then I will print it out via Serial.println(myString);
This way it is easy to modify the string as you want

And then to join the string together, we just put the plus sign between them like this
I have another String: String myStartString = “Let’s go”
Now i will print out : Serial.println(myStartString + “with plus sign”);
Run to see what’s happend.

Instead, we will create another String
String myCombine = myWelcomeString + myStartString;
And then put to Serial.println(myCombine);
That’s easy right