Skip to content

Commit c1bfeff

Browse files
Utkarsh MishraUtkarsh Mishra
Utkarsh Mishra
authored and
Utkarsh Mishra
committed
site updated
1 parent 914eba1 commit c1bfeff

File tree

4 files changed

+97
-25
lines changed

4 files changed

+97
-25
lines changed

.github/pull_request-template.md

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -21,11 +21,11 @@ Fixes #
2121
<!-- Remove items that do not apply. For completed items, change [ ] to [x]. -->
2222

2323
- [ ] Added description of change
24-
- [ ] Added file name matches [File name guidelines](https://github.com/TheAlgorithms/C-Plus-Plus/blob/master/CONTRIBUTING.md#New-File-Name-guidelines)
24+
- [ ] Added file name matches [File name guidelines](https://github.com/Utkarsh1504/DSA-Java/blob/main/CONTRIBUTING.md#requesting-a-lesson)
2525
- [ ] Added code examples, test must pass
2626
- [ ] Added documentation so that the program is self-explanatory and educational.
2727
- [ ] Relevant documentation/comments is changed or added
28-
- [ ] PR title follows semantic [commit guidelines](https://github.com/Utkarsh1504/ds-algo/CONTRIBUTING.md#Commit-Guidelines)
28+
- [ ] PR title follows semantic [commit guidelines](https://github.com/Utkarsh1504/DSA-Java/blob/main/CONTRIBUTING.md#requesting-a-lesson)
2929
- [ ] Search previous suggestions before making a new one, as yours may be a duplicate.
3030
- [ ] I acknowledge that all my contributions will be made under the project's license.
3131

CONTRIBUTING.md

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
# Contributing to DS-Algo
1+
# Contributing to DSA-Java
22

33
First of all, thanks for taking the time to contribute! This project can only grow and live by your countless contributions. To keep this project maintainable, we have developed some guidelines for our contributors.
44

@@ -30,7 +30,7 @@ The crux of this site is are the `lesson`. Provided are two examples. Each lesso
3030
title: "TitleName"
3131
order: "<Number><CapitalLetter>"
3232
section: "SectionName"
33-
description: "Learn DS-Algo/C++"
33+
description: "Learn DSA-Java"
3434
---
3535
```
3636

@@ -55,7 +55,7 @@ for requesting a lesson simply raise a new issue in the `Issues` section.
5555
- Search for other issues already requesting the lesson.
5656
- If an issue doesn't exist, create an issue naming it `new lesson: <name-of-the-lesson>`
5757
- provide description as mentioned in issue template.
58-
- Please create separate issues for each icon
58+
- Please create separate issues for any concept or lesson that you want to add.
5959

6060
## Fixing Typos
6161

lessons/intro-recursion.md

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,3 +6,34 @@ section: "Recursion"
66
description: "learn Recursion from scratch"
77
icon: "redo"
88
---
9+
10+
Recursion is a technique that leads to elegant solutions to problems that are difficult to solve using simple iteration(loops). In some cases, it enables
11+
you to develop a natural, straightforward, simple solution to an otherwise difficult problem.
12+
13+
> Recursion in Computer Science is a method where the solution to a problem depends on the solution to smaller instances of the same problem.
14+
15+
This lesson introduces the concepts and techniques of recursive programming and illustrates with examples of how to **“think recursively”**. Let's understand this by a simple example:
16+
17+
```java
18+
Calculate factorial of a number by using recursion
19+
```
20+
21+
Now, How do you find `n!` of a given number?
22+
23+
- To find `1!` is easy, because you know that `0!` is 1, and 1! is 1 × 0!.
24+
- Assuming that you know `(n - 1)!`, you can obtain n! immediately by using `n × (n - 1)!`. Thus, the problem of computing n! is reduced to computing (n - 1)!. When computing `(n - 1)!`, you can apply the same idea recursively until n is reduced to 0.
25+
- Let factorial(n) be the method for computing n!. If you call the method with n = 0, it immediately returns the result. The method knows how to solve the simplest case, which is referred to as the **base case** or the stopping condition.
26+
- If you call the method with n > 0, it reduces the problem into a subproblem for computing the factorial of n - 1.
27+
- The subproblem is essentially the same as the original problem, but it is simpler or smaller. Because the sub-problem has the same property as the original problem, you can call the method with a different argument, which is referred to as a recursive call.
28+
- The recursive algorithm for computing factorial(n) can be simply described as follows:
29+
30+
```java
31+
if(n == 0){
32+
return 1;
33+
}else{
34+
return n * factorial(n - 1);
35+
}
36+
```
37+
38+
A recursive call can result in many more recursive calls, because the method keeps on dividing a subproblem into new subproblems. For a recursive method to terminate, the problem must eventually be reduced to a stopping case, at which point the method returns a result to its caller. The caller then performs a computation and returns the result to its own caller. This process continues until the result is passed back to the original caller. The original problem
39+
can now be solved by multiplying `n` by the result of **factorial(n - 1)**.

lessons/loops.md

Lines changed: 61 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -6,12 +6,9 @@ section: "Learn Java"
66
description: "Learn java"
77
---
88

9-
Loops are a way to repeat a block of code. It is used to remove repetitive code and to make code more readable. There are two ways for iteration in C++.
9+
Loops are a way to repeat a block of code. It is used to remove repetitive code and to make code more readable. It is a way to write iterative code.
1010

11-
- Loops
12-
- Recursion
13-
14-
There are three types of loops in C++.
11+
There are three types of loops in Java.
1512

1613
- For
1714
- While
@@ -23,7 +20,7 @@ For loops are used to iterate over a range of numbers. In this loop, the range i
2320

2421
#### Semantics
2522

26-
```cpp
23+
```java
2724
// syntax
2825

2926
for (initialization; condition; update){
@@ -52,24 +49,57 @@ Ok so let's see how to use for loops in writing a program.
5249

5350
so let's analyze the code.
5451

55-
- firstly we declare a variable `n` and assign it a value from the user using the `cin` function which is a part of the standard library.
56-
- `cin >> n` is a statement that take input the value of `n` from the user.
57-
- then we declare one more variable `sum` and assign it a value of 0 because we are starting from the first number so at that time we don't have any sum.
58-
- then we use a `for` loop to iterate over the range of numbers from 0 to the value of `n` (in this case `n` is the value of `n`). this loop will run for `n` times ( starting from 0 to `n-1` total n times).
59-
- `i` is initialized with 0 (you can initialize it with any value you want but keep in mind you have to change the condition `i < n` according to that). and then we update the value of `i` by adding 1 each time.
60-
- inside the body of the loop we assigned the value of `i` to the `sum` variable. `sum += i` is another way of writing `sum = sum + i` and it is a way to add the value of `i` to the value of `sum`.
61-
each time value of `i` will get incemented by 1 and then it will be added to the `sum`.
62-
- finally we print the value of `sum` to the screen.
52+
- we have initialized the variable `sum` to 0.
53+
- then we used `for` loop:
54+
55+
- In the initialization, we have initialized the variable `i` to 0(`int i=0;`). This step is completely optional. You can also do the initialization before the loop. In that that would be look like:
56+
57+
```java
58+
int sum = 0;
59+
int i = 0;
60+
for (; i < 10; i++) {
61+
....
62+
```
63+
64+
or something like that.
65+
66+
```java
67+
int i;
68+
for (i = 0; i < 10; i++) {
69+
```
70+
71+
- In the condition, it checks if `i` is less than 10. The condition checks if the loop should continue or not and this is not optional, you have to provide condition. In this case we have given a sigle condition `i < 10`. we can also have multiple conditions like `i < 10 && i > 0`. for multiple conditions, we need to use logical operator to combine them.
72+
73+
```java
74+
int sum = 0;
75+
for(int i=0; i<10 && i%2==0; i++) {
76+
```
77+
78+
- In the update, it updates the value of `i` by 1. This is optional too. you can have the update like: i++, i--, i+=2, i-=2 etc.
79+
80+
```java
81+
int sum = 0;
82+
for(int i=0; i<10;) {
83+
sum += i;
84+
i++;
85+
}
86+
87+
```
88+
89+
- Okay so these are the variation of for loops. let's understand how this for loop works. So `i=0;`, i is assigned to 0. now it checks is `i`(0) is less than `10`, the answer is yes(true). if condition comes true, it executes the block of code. so inside the body, `sum += i;`, expression is evaluated. `sum` is assigned to 0 as sum = sum + i(0). so `sum` is assigned to 0. This is the first iteration.
90+
- Now `i` get updated to 1. now it checks if `i` is less than 10, the answer is yes(true). if condition comes true, it executes the block of code. so inside the body, `sum += i;`, expression is evaluated. `sum` is assigned to 0 as sum = sum + i(1). so `sum` is assigned to 1. This is the second iteration.
91+
- This iteration continues until `i` is less than 10. As `i` reaches 10, the condition becomes false and the loop stops.
92+
- So the final value of `sum` is 10, which will be printed on the console.
6393
64-
And that's all. I'll add some question at the end.
94+
And that's all. You can see that the for loop is very simple and easy to understand. You can practise some questions based on for loops in the [Assignment Section](./assignment.md).
6595

6696
## **While Loops**
6797

68-
While Loops are used to iterate over a range of numbers. In this loop, the range is defined by the `while` keyword. It needs a condition to check if the loop should continue or not and a block of code to be executed for each value in the range of numbers (from the first to the last). The main difference between for and while loops is that the for loop is used to iterate over a range of numbers and the while loop is used to iterate over a range of values.
98+
While Loops are used to iterate over a range of numbers. In this loop, the range is defined by the `while` keyword. It needs a condition to check if the loop should continue or not and a block of code to be executed for each value in the range of numbers (from the first to the last).
6999

70100
#### Semantics
71101

72-
```cpp
102+
```java
73103
// syntax
74104
while(condition) {
75105
// block of code
@@ -111,7 +141,7 @@ Do-While loops are used to iterate over a range of numbers. In this loop, the ra
111141
112142
#### Semantics
113143
114-
```cpp
144+
```java
115145
// syntax
116146
do {
117147
// block of code
@@ -124,7 +154,7 @@ Do-While loops are used to iterate over a range of numbers. In this loop, the ra
124154
125155
Let's see how to apply do-while loop in writing a program.
126156

127-
```cpp
157+
```java
128158
// Program to demonstrate do-while loop
129159
public class <ClassName> {
130160
public static void main(String[] args) {
@@ -139,4 +169,15 @@ Let's see how to apply do-while loop in writing a program.
139169

140170
Lets analyze the code.
141171

142-
- In line six, we have used `do` to iterate over the range of numbers and we have put condition `n > 0` because we want to iterate over the numbers from the first to the last.
172+
- we have used `do` keyword to execute the block of code firstly. firstly this loop will start executing the block of code and print the value of `i` to the console.
173+
- after that we're incrementing the value of `i` by 1 and then we check the condition `i < 10` and if the condition is true then we execute the loop again and print the value of `i` to the console.
174+
- and if the condition is false then we break the loop.
175+
- so the conclusion is that we first, atleast once execute the block of code and then we'll check the condition and according to the condition we'll work further.
176+
177+
> There may be a question when to use which loop?
178+
179+
Okay so it depends on the situation. It completey depends on the problem you are solving.
180+
181+
- **`for` and `while`** - you can use both loop as per your requirement, but there is one thing which recommended by all is you should use `while` loop when you don't know how many times you have to iterate and use a `for` loop when you know how many times you have to iterate. suppose you have to print numbers from 1 to 10 then you know how many times you have to run the loop so use `for` loop. And if you have given that keep taking input from a user till user doesn't press x, here you don't know how many times you have to iterate so use `while` loop.
182+
183+
- **`while` and `do-while`** - so in situations like, when you atleast once want to run the loop, irrespective of the condition, then you should use `do-while` loop and rest in all other cases either use `while` loop or `for` loop.

0 commit comments

Comments
 (0)