From c631645400f0f36e8d67572fe1e2de01069944d6 Mon Sep 17 00:00:00 2001 From: Arghya Ghosh <71373838+uiuxarghya@users.noreply.github.com> Date: Wed, 9 Feb 2022 13:42:47 +0530 Subject: [PATCH 1/3] Create for-loop.mdx --- src/pages/docs/for-loop.mdx | 253 ++++++++++++++++++++++++++++++++++++ 1 file changed, 253 insertions(+) create mode 100644 src/pages/docs/for-loop.mdx diff --git a/src/pages/docs/for-loop.mdx b/src/pages/docs/for-loop.mdx new file mode 100644 index 00000000..59e321b1 --- /dev/null +++ b/src/pages/docs/for-loop.mdx @@ -0,0 +1,253 @@ +--- +title: Java for Loop +description: In this tutorial, we will learn how to use for loop in Java with the help of examples and we will also learn about the working of Loop in computer programming. +--- +import NextImage from 'next/image'; + +In computer programming, loops are used to repeat a block of code. For example, if you want to show a message 100 times, then rather than typing the same code 100 times, you can use a loop. + +In Java, there are three types of loops. + +- for loop +- while loop +- do...while loop + +This tutorial focuses on the for loop. You will learn about the other type of loops in the upcoming tutorials. + +## Java for Loop + +Java `for` loop is used to run a block of code for a certain number of times. The syntax of `for` loop is: + +```java +for (initialExpression; testExpression; updateExpression) { + // body of the loop +} +``` + +Here, + +1. The **initialExpression** initializes and/or declares variables and executes only once. +2. The **condition** is evaluated. If the **condition** is `true`, the body of the `for` loop is executed. +3. The **updateExpression** updates the value of **initialExpression**. +4. The **condition** is evaluated again. The process continues until the **condition** is `false`. + +To learn more about the conditions, visit [Java relational](operators#3-java-relational-operators) and [logical operators](operators#4-java-logical-operators). + +## Working of for loop in Java with flowchart + +
+ + + +

Flowchart of Java for loop

+ +
+ + +### Example 1: Display a Text Five Times + +#### Input + +```java +// Program to print a text 5 times + +class Main { + public static void main(String[] args) { + + int n = 5; + // for loop + for (int i = 1; i <= n; ++i) { + System.out.println("Java is fun"); + } + } +} +``` + +#### Output + +```text +Java is fun +Java is fun +Java is fun +Java is fun +Java is fun +``` + +Here is how this program works. + +| Iteration | Variable | Condition: i <= n | Action | +| :-------: | :--------------: | :---------------: | :-------------------------------------------------: | +| 1st | `i = 1`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **2**. | +| 2nd | `i = 2`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **3**. | +| 3rd | `i = 3`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **4**. | +| 4th | `i = 4`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **5**. | +| 5th | `i = 5`, `n = 5` | `true` | `Java is fun` is printed `i` is increased to **6**. | +| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | + +### Example 2: Display numbers from 1 to 5 + +#### Input + +```java +// Program to print numbers from 1 to 5 + +class Main { + public static void main(String[] args) { + + int n = 5; + // for loop + for (int i = 1; i <= n; ++i) { + System.out.println(i); + } + } +} +``` + +#### Output + +```text +1 +2 +3 +4 +5 +``` + +Here is how the program works. + +| Iteration | Variable | Condition: i <= n | Action | +| :-------: | :--------------: | :---------------: | :---------------------------------------: | +| 1st | `i = 1`, `n = 5` | `true` | `1` is printed `i` is increased to **2**. | +| 2nd | `i = 2`, `n = 5` | `true` | `2` is printed `i` is increased to **3**. | +| 3rd | `i = 3`, `n = 5` | `true` | `3` is printed `i` is increased to **4**. | +| 4th | `i = 4`, `n = 5` | `true` | `4` is printed `i` is increased to **5**. | +| 5th | `i = 5`, `n = 5` | `true` | `5` is printed `i` is increased to **6**. | +| 6th | `i = 6`, `n = 5` | `false` | The loop is terminated. | + +### Example 3: Display Sum of n Natural Numbers + +```java +// Program to find the sum of natural numbers from 1 to 1000. + +class Main { + public static void main(String[] args) { + + int sum = 0; + int n = 1000; + + // for loop + for (int i = 1; i <= n; ++i) { + // body inside for loop + sum += i; // sum = sum + i + } + + System.out.println("Sum = " + sum); + } +} +``` + +#### Output: + +```text +Sum = 500500 +``` + +Here, the value of `sum` is **0** initially. Then, the for loop is iterated from `i = 1 to 1000`. In each iteration, `i` is added to `sum`and its value is increased by **1**. + +When `i` becomes **1001**, the test condition is `false` and `sum` will be equal to `0 + 1 + 2 + .... + 1000`. + +The above program to add the sum of natural numbers can also be written as + +```java +// Program to find the sum of natural numbers from 1 to 1000. + +class Main { + public static void main(String[] args) { + + int sum = 0; + int n = 1000; + + // for loop + for (int i = n; i >= 1; --i) { + // body inside for loop + sum += i; // sum = sum + i + } + + System.out.println("Sum = " + sum); + } +} +``` + +The output of this program is the same as the **Example 3**. + +## Java for-each Loop + +The Java for loop has an alternative syntax that makes it easy to iterate through [arrays](arrays) and collections. For example, + +#### Input + +```java +// print array elements + +class Main { + public static void main(String[] args) { + + // create an array + int[] numbers = {3, 7, 5, -5}; + + // iterating through the array + for (int number: numbers) { + System.out.println(number); + } + } +} +``` + +#### Output + +```text +3 +7 +5 +-5 +``` + +Here, we have used the **for-each loop** to print each element of the `numbers` array one by one. + +In the first iteration of the loop, `number` will be 3, `number` will be 7 in second iteration and so on. + +To learn more, visit [Java for-each Loop](enhanced-for-loop). + +## Java Infinite for Loop + +If we set the test expression in such a way that it never evaluates to false, the for loop will run forever. This is called infinite for loop. For example, + +#### Input + +```java +// Infinite for Loop + +class Infinite { + public static void main(String[] args) { + + int sum = 0; + + for (int i = 1; i <= 10; --i) { + System.out.println("Hello"); + } + } +} +``` + +Here, the test expression ,`i <= 10`, is never `false` and Hello is printed repeatedly until the memory runs out. From 597d4ca8bf734bf042f61c605b618ead71fd3bc7 Mon Sep 17 00:00:00 2001 From: Arghya Ghosh <71373838+uiuxarghya@users.noreply.github.com> Date: Wed, 9 Feb 2022 13:42:52 +0530 Subject: [PATCH 2/3] Create working-of-for-loop.svg --- src/img/docs/working-of-for-loop.svg | 30 ++++++++++++++++++++++++++++ 1 file changed, 30 insertions(+) create mode 100644 src/img/docs/working-of-for-loop.svg diff --git a/src/img/docs/working-of-for-loop.svg b/src/img/docs/working-of-for-loop.svg new file mode 100644 index 00000000..3e8f9466 --- /dev/null +++ b/src/img/docs/working-of-for-loop.svg @@ -0,0 +1,30 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + \ No newline at end of file From 5359c3f7f515181447d00ac5c21619a29bedfd55 Mon Sep 17 00:00:00 2001 From: Arghya Ghosh <71373838+uiuxarghya@users.noreply.github.com> Date: Wed, 9 Feb 2022 13:42:56 +0530 Subject: [PATCH 3/3] Update documentation.js --- src/navs/documentation.js | 5 +---- 1 file changed, 1 insertion(+), 4 deletions(-) diff --git a/src/navs/documentation.js b/src/navs/documentation.js index c2ad8866..09f8e1f1 100644 --- a/src/navs/documentation.js +++ b/src/navs/documentation.js @@ -18,8 +18,5 @@ export const documentationNav = { pages['expressions-statements-blocks'], pages['comments'], ], - 'Java Flow Control': [ - pages['if-else-statement'], - pages['switch-statement'], - ], + 'Java Flow Control': [pages['if-else-statement'], pages['switch-statement'], pages['for-loop']], }