fix(guide): simplify directory structure

This commit is contained in:
Mrugesh Mohapatra
2018-10-16 21:26:13 +05:30
parent f989c28c52
commit da0df12ab7
35752 changed files with 0 additions and 317652 deletions

View File

@ -0,0 +1,74 @@
---
title: Break Control Statement
---
# Break Control Statement
Terminates the loop and starts the execution of the code that immediately follows the loop. If you have nested loops, the `break` statement will only end the loop in which it is placed.
```java
// Loop 1
for (int i = 0; i < 10; i++)
{
// Loop 2
for (int j = 0; j < 10; j++)
{
if (i == 5 && j == 5)
{
break; // Will terminate Loop 2, but Loop 1 will keep going
}
}
}
```
But if you do want to break out of the outer loop too, you can use a label to exit:
```java
loop1: // This is a label
for (int i = 0; i < 10; i++)
{
// Loop 2
for (int j = 0; j < 10; j++)
{
if (i == 5 && j == 5)
{
break loop1; // Will break out of Loop 1, instead of Loop 2
}
}
}
```
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/CJZA/0' target='_blank' rel='nofollow'>Run Code</a>
`break` statements can be particulary useful while searching for an element in an array. Using `break` in the following code improves efficiency as the loop stops as soon as the element we are looking for (`searchFor`) is found, instead of going on till the end of `arrayInts` is reached.
```java
int j = 0;
int[] arrayOfInts = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int searchFor = 5;
for (int i : arrayOfInts)
{
if (arrayOfInts[j] == searchFor)
{
break;
}
j++;
}
System.out.println("j = " + j);
```
Break statement can also be used under while statement.
```java
int i = 0;
int[] arrayOfInts = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
int searchFor = 5;
while(i < 10){
System.out.println("i = " + j);
if(arrayOfInts[i] > 7){
break;
}
}
```
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/CJZC/0' target='_blank' rel='nofollow'>Run Code</a>

View File

@ -0,0 +1,49 @@
---
title: Continue Control Statement
---
# Continue Control Statement
The `continue` statement makes a loop skip all the following lines after the continue and jump ahead to the beginning of the next iteration. In a `for` loop, control jumps to the update statement, and in a `while` or `do while` loop, control jumps to the boolean expression/condition.
```java
for (int j = 0; j < 10; j++)
{
if (j == 5)
{
continue;
}
System.out.print (j + " ");
}
```
The value of `j` will be printed for each iteration, except when it is equal to `5`. The print statement will get skipped because of the `continue` and the output will be:
0 1 2 3 4 6 7 8 9
Say you want to count the number of `i`s in a the word `mississippi`. Here you could use a loop with the `continue` statement, as follows:
```java
String searchWord = "mississippi";
// max stores the length of the string
int max = searchWord.length();
int numPs = 0;
for (int i = 0; i < max; i++)
{
// We only want to count i's - skip other letters
if (searchWord.charAt(i) != 'i')
{
continue;
}
// Increase count_i for each i encountered
numPs++;
}
System.out.println("numPs = " + numPs);
```
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/CJZH/0' target='_blank' rel='nofollow'>Run Code</a>
Additionally, you can use labels to choose a specific loop out of a nested set to skip to the next iteration.

View File

@ -0,0 +1,50 @@
---
title: Jump Statements
---
# Jump Statements
Jump statements are a type of <a href='https://docs.oracle.com/javase/tutorial/java/nutsandbolts/flow.html' target='_blank' rel='nofollow'><i>control flow</i></a> statements. Basically, you can use them to change the order in which statements are executed from the normal course of execution. In essence, these statements cause the program control to 'jump' away from the next expected point of execution to another place in the program.
The following jump statements are commonly used with loops:
* <a href='http://forum.freecodecamp.com/t/java-loops-break-control-statement' target='_blank' rel='nofollow'>break</a>
* <a href='http://forum.freecodecamp.com/t/java-loops-continue-control-statement' target='_blank' rel='nofollow'>continue</a>
The 'break' control statement breaks out of the loop when the condition is met. This means the rest of the loop will not run.
For example, in the loop below if i reaches 5, the loop breaks, so it does not continue on.
```java
for(int i=0;i<10;i++){
if(i == 5){ //if i is 5, break out of the loop.
break;
}
System.out.println(i);
}
```
Output:
```
0 1 2 3 4
```
The 'continue' control statement is the less intense version of 'break'. It only breaks out of the current instance of the loop and continues on. In the loop below, if i is 5, the loop continues, so it will skip over the print statement below and move on until i reaches 10 and the loop stops.
```java
for(int i=0;i<10;i++){
if(i == 5){ //if i is 5, break out of the current instance loop.
continue;
}
System.out.println(i);
}
```
Output:
```
0 1 2 3 4 6 7 8 9
```

View File

@ -0,0 +1,54 @@
---
title: Do...While Loop
---
# Do...While Loop
The `do while` is similar to the `while` loop, but the group of statements is guranteed to run at least once before checking for a given condition.
An important thing to note is 'while' loop is an exit control loop. while(it will not necessarily be executed), 'do while' is an entry controlled loop(it will be executed at least once , even if the conditon is not true).
```java
do
{
// Statements
}
while (condition);
```
## Example
```java
int iter_DoWhile = 20;
do
{
System.out.print (iter_DoWhile + " ");
// Increment the counter
iter_DoWhile++;
}
while (iter_DoWhile < 10);
System.out.println("iter_DoWhile Value: " + iter_DoWhile);
```
Output:
```
20
iter_DoWhile Value: 21
```
**Remember**: The condition of a `do-while` loop is checked after the code body is executed once.
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/CJYl/0' target='_blank' rel='nofollow'>Run Code</a>
## Exercise
Can you guess the output of the following code snippet?
```java
int i = 10;
do
{
System.out.println("The value of i is " + i);
i--;
}
while (i >= 10);
```

View File

@ -0,0 +1,87 @@
---
title: For Each Loop
---
# For Each Loop
Also called the enhanced `for` loop, it is an extremely useful and simple way to iterate over each item in a collection, array or any object that implements the `Iterable` interface.
```java
for (object : iterable)
{
// Statements
}
```
The loop is read as, "for each element in the `iterable` (could be an array, collectable etc.)". The `object` type must match the element type of the `iterable`.
```java
int[] number_list = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for (int numbers : number_list)
{
System.out.print(numbers + " ");
// Iterated 10 times, numbers 0,1,2...9
}
```
Output:
```
0 1 2 3 4 5 6 7 8 9
```
:rocket:<a href='https://repl.it/CJYs/0' target='_blank' rel='nofollow'>Run Code</a>
Comparing this with the traditional `for` loops :
```java
int[] number_list = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
for(int i=0;i < number_list.length;i++)
{
System.out.print(number_list[i]+" ");
// Iterated 10 times, numbers 0,1,2...9
}
```
Output:
```
0 1 2 3 4 5 6 7 8 9
```
:rocket:<a href='https://repl.it/NJfG/0' target='_blank' rel='nofollow'>Run Code</a>
Both the above pieces of code snippets do the same work , however , clearly, the for each loops offer advantages in making iteration through and accessing of elements of a collection(array,in our case) easier.
With the enhanced for loops we no longer need to mention starting and ending points for the loop,thus reducing OutofBounds errors.
The need for loop counters and manual indexing are removed, and readability of the code is improved.
It is important to note that making changes to the iterating variable for enhanced for loops within the loop causes no changes to the original collection elements.
Enhanced for loops can also be used with multidimensional arrays or other Java collections.
An example of it's usage with multidimenisonal arrays are shown below:
```java
int number_list_new[][]={ { 0, 1, 2},
{ 3, 4, 5},
{ 6, 7, 8} };
// Because 2d arrays are implemented as "arrays of arrays",the first iteration variable iterates
// through 3 such arrays(that is, the 3 rows of testarr[][])
for(int i[] : number_list_new)
{
for(int j : i){
System.out.print(j+" ");
}
}
```
Output:
```
0 1 2 3 4 5 6 7 8
```
:rocket: <a href='https://repl.it/NJhP/0' target='_blank' rel='nofollow'>Run Code</a>
In the above code snippets, `number_list` is an array. If you don't know what this is, don't worry about it. An array is a container object that holds a fixed number of values of a single type, but more on this later.

View File

@ -0,0 +1,73 @@
---
title: For Loop
---
# For Loop
The `for` loop gives you a compact way to iterate over a range of values.
A basic `for` statement has three parts: a variable initialization, a boolean expression, and an increment expression.
```java
for (variable initialization; boolean expression; increment expression)
{
// Statements
}
```
* `initialization` - Initializes the loop and is executed just once, at the beginning.
You can initialize more than one variable of the same type in the first part of the basic `for` loop declaration; each initialization must be separated by a comma.
* `expression` - Evaluated at the beginning of each iteration. If the `expression` evaluates to `true`, `Statements` will get executed.
* `increment` - Invoked after each iteration through the loop. You can increase/decrease the value of variables here. Be sure the increment is working towards the expression value, to avoid an infinite loop.
A common way the `for` loop is used is if you need to iterate your code a specific number of times. For example, if you wanted to output the numbers 0-10, you would initialize the variable for your counter to 0, then check if the value is less than 10, and add one to the counter after every iteration.
Notice that you would check if the value is less than 10, not less than or equal to 10, since you are starting your counter at 0.
```java
for (int iter_For = 0; iter_For < 10; iter_For++)
{
System.out.print(iter_For + " ");
// Iterated 10 times, iter_For 0,1,2...9
}
System.out.println("iter_For Value: " + iter_For);
```
Note: It is also acceptable to declare a variable within the for loop as a single statement.
```java
for (int iter_For = 0; iter_For < 10; iter_For++)
{
System.out.print (iter_For + " ");
// Iterated 10 times, iter_For 0,1,2...9
}
```
Output:
```
0 1 2 3 4 5 6 7 8 9
iter_For Value: 10
```
Another example of a for loop that adds the first 50 numbers would be like this.
i++ means i = i+1.
```java
int addUntil = 50;
int sum 0;
for (int i = 1; i <= addUntil; i++)
{
sum+=i
}
System.out.println("The sum of the first 50 numbers is: " + 50);
```
![:rocket:](https://forum.freecodecamp.org/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") <a href='https://repl.it/CJYr/0' target='_blank' rel='nofollow'>Run Code</a>
### Extras
You cannot use a number (old C-style language construct) or anything that does not evaluate to a boolean value as a condition for an if statement or looping construct. You can't, for example, say if(x), unless x is a boolean variable.
Also, it is important to keep in mind that the boolean expression must, at some point, evaluate to true. Otherwise, your program will be stuck in an infinite loop.

View File

@ -0,0 +1,21 @@
---
title: Loops
---
# Loops
Whenever you need to execute a block of code multiple times, a loop will often
come in handy.
Java has 4 types of loops:
* [While Loop](loops/while-loop)
* [Do...While Loop](loops/do-while-loop)
* [For Loop](loops/for-loop)
* [For Each Loop](loops/for-each-loop)
Loops behaviour can be customized using:
* [Control Statements](loops/control-statements)
* [Break Control Statement](loops/break-control-statement)
* [Continue Control Statement](loops/continue-control-statement)
A special case of loops:
* [Infinite Loops](loops/infinite-loops)

View File

@ -0,0 +1,65 @@
---
title: Infinite Loops
---
# Infinite Loops
An infinte loop is a loop statement (`for`, `while`, `do-while`) which does not end on its own.
The test condition of a looping statement decides whether the loop body will execute or not. So a test condition which is always true will keep on executing the body of the loop, forever. That's the case in an infinte loop.
Examples:
```java
// Infinite For Loop
for ( ; ; )
{
// some code here
}
// Infinite While Loop
while (true)
{
// some code here
}
// Infinite Do While Loop
do
{
// some code here
} while (true);
```
Normally, if your loop is running infinitely, it is an error that should not occur as an infinite loop does not stop and prevents the rest of the program from running.
```java
for(int i=0;i<100;i++){
if(i==49){
i=0;
}
}
```
The loop above runs infinitely because every time i approaches 49, it is set to be 0.This is to say that i never reaches 100 to terminate the loop, so the loop is an infinite loop.
But a program stuck in such a loop will keep using computer resources indefinitely. This is undesirable, and is a type of 'run-time error'.
To prevent the error, programmers use a break statement to break out of the loop. The break executes only under a particular condition. Use of a selection statement like if-else ensures the same.
```java
while (true)
{
// do something
if(conditionToEndLoop == true)
break;
// do more
}
```
The main advantage of using an infinite loop over a regular loop is readability.
Sometimes, the body of a loop is easier to understand if the loop ends in the middle, and not at the end/beginning. In such a situation, an infinite loop will be a better choice.

View File

@ -0,0 +1,40 @@
---
title: While Loop
---
# While Loop
The `while` loop repeatedly executes the block of statements until the condition specified within the parentheses evaluates to `false`. For instance:
```java
while (some_condition_is_true)
{
// do something
}
```
Each 'iteration' (of executing the block of statements) is preceeded by the evaluation of the condition specified within the parentheses - The statements are executed only if the condition evaluates to `true`. If it evaluates to `false`, the execution of the program resumes from the the statement just after the `while` block.
**Note**: For the `while` loop to start executing, you'd require the condition to be `true` initially. However, to exit the loop, you must do something within the block of statements to eventually reach an iteration when the condition evaluates to `false` (as done below). Otherwise the loop will execute forever. (In practice, it will run until the <a href='https://guide.freecodecamp.org/java/the-java-virtual-machine-jvm' target='_blank' rel='nofollow'>JVM</a> runs out of memory.)
## Example
In the following example, the `expression` is given by `iter_While < 10`. We increment `iter_While` by `1` each time the loop is executed. The `while`loop breaks when`iter_While`value reaches `10`.
```java
int iter_While = 0;
while (iter_While < 10)
{
System.out.print(iter_While + " ");
// Increment the counter
// Iterated 10 times, iter_While 0,1,2...9
iter_While++;
}
System.out.println("iter_While Value: " + iter_While);
```
Output:
```
0 1 2 3 4 5 6 7 8 9
iter_While Value: 10
```
![:rocket:](//forum.freecodecamp.com/images/emoji/emoji_one/rocket.png?v=2 ":rocket:") [Run Code](https://repl.it/CJYj/0)