You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
Copy file name to clipboardExpand all lines: chapter-05-loops.md
+23-23Lines changed: 23 additions & 23 deletions
Original file line number
Diff line number
Diff line change
@@ -1,6 +1,6 @@
1
1
# Chapter 5.1. Repetitions (Loops)
2
2
3
-
In the present chapter, we will get familiar with how to **repeat blocks of commands**, known in software development as "**loops**". We will write several loops using the **`for`** operator in its simplest form. Finally, we will solve some practical problems that require repeating series of actions, using loops.
3
+
In the present chapter, we will get familiar with how to **repeat blocks of commands**, known in software development as "**loops**". We will write several loops using the **`for`** operator in its simplest form. Finally, we will solve some practical problems that require repeating a series of actions, using loops.
4
4
5
5
## Video
6
6
@@ -44,13 +44,13 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/655#
44
44
45
45
You should get **100 points** (fully accurate solution).
46
46
47
-
## Code Snippet for **`for`**loop in IntelliJ IDEA
47
+
## Code Snippet for The **`for`**Loop in IntelliJ IDEA
48
48
49
-
In software development, we regularly need to write loops dozens of times a day. That is why in most integrated development environments (IDE), there are **code snippets** for writing loops. One such example is the **snippet for `for` loop in IntelliJ IDEA**. Write down **`fori`** in the Java code editor in IntelliJ IDEA and **press**[**Enter**]. IntelliJ IDEA will run the snippet, and you will see the entire **`for` loop** written down. All you have to do now is to add the values.
49
+
In software development, we regularly need to write loops dozens of times a day. That is why in most integrated development environments (IDE), there are **code snippets** for writing loops. One such example is the **snippet for the `for` loop in IntelliJ IDEA**. Write down **`fori`** in the Java code editor in IntelliJ IDEA and **press**[**Enter**]. IntelliJ IDEA will run the snippet, and you will see the entire **`for` loop** written down. All you have to do now is to add the values.
**Try it yourself**in order to master using the code snippet for **`for` loop** in IntelliJ IDEA.
53
+
**Try it yourself** to master using the code snippet for the**`for` loop** in IntelliJ IDEA.
54
54
55
55
### Problem: Numbers Ending in 7
56
56
@@ -119,7 +119,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/655#
119
119
120
120
### Problem: Max Number
121
121
122
-
Write a program that inputs **n integers** (**n** > 0) and finds the **the Max Number** (the largest number) among them. The first line of the input specifies the number of integers **n**. The following **`n`** lines consist of one integer. Examples:
122
+
Write a program that inputs **n integers** (**n** > 0) and finds the **Max Number** (the largest number) among them. The first line of the input specifies the number of integers **n**. The following **`n`** lines consist of one integer. Examples:
123
123
124
124
#### Sample Input and Output
125
125
@@ -204,15 +204,15 @@ We input the numbers one by one and calculate the two **sums** (the numbers on *
**`+=`** is **combined assigment operator** that is used frequently. It adds the value of the right to the left operand and then assignes the result to the left operand.
207
+
**`+=`** is a **combined assignment operator** that is used frequently. It adds the value of the right to the left operand and then assigns the result to the left operand.
208
208
**C += A** is equivalent to **C = C + A**.
209
209
210
210
#### Testing in The Judge System
211
211
212
212
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/655#7](https://judge.softuni.org/Contests/Practice/Index/655#7).
213
213
214
214
215
-
### Exapmle: Vowels Sum
215
+
### Problem: Vowels Sum
216
216
217
217
Write a program that inputs **text** (string), calculates and prints **the sum of the values of vowels** according to the table below:
218
218
@@ -229,7 +229,7 @@ Write a program that inputs **text** (string), calculates and prints **the sum o
229
229
230
230
#### Hints and Guidelines
231
231
232
-
We read the input text **`s`**, after that we null the sum and run a loop from **0** to **`s.length() - 1`** (text lenght -1). We check every letter **`s.charAt(i)`** if it is a vowel and add its value to the sum.
232
+
We read the input text **`s`**, after that, we null the sum and run a loop from **0** to **`s.length() - 1`** (text length -1). We check every letter **`s.charAt(i)`** if it is a vowel and add its value to the sum.
@@ -244,15 +244,15 @@ We can repeat a block of code using **`for` loop**:
244
244
245
245

246
246
247
-
We can read series of **`n`** numbers from the console:
247
+
We can read a series of **`n`** numbers from the console:
248
248
249
249

250
250
251
-
## Problems Repetitions (Loops)
251
+
## Problems: Loops
252
252
253
253
After we had acquainted with the loops, it is time to **solidify what we have learned by practicing it**, by writing a lot of code. Let us solve some problems for exercise.
254
254
255
-
### Creating new project in IntelliJ IDEA
255
+
### Creating a new project in IntelliJ IDEA
256
256
257
257
We start by creating a new project in IntelliJ IDEA to better organize our solutions for the exercise. For each problem, we will create a separate class.
258
258
@@ -272,19 +272,19 @@ We name the project (Project name:) **Loops** and select the directory where the
We created a new project. If we click on the triangle in front of **Loops** on the left side of the window, the structure of the project itself will open.
To create **Package** in our project, which contains all the classes with the solutions from the exercise, right-click with the mouse on **src**, then select **New** -> **Package** and name it **problems** (with a small first letter).
283
+
To create a **Package** in our project, which contains all the classes with the solutions from the exercise, right-click with the mouse on **src**, then select **New** -> **Package** and name it **problems** (with a small first letter).
To create new class for the first problem, right-click with the mouse on **problems**, select **New** -> **Java Class** and name it **problem_01** (with a capital letter).
287
+
To create a new class for the first problem, right-click with the mouse on **problems**, select **New** -> **Java Class**, and name it **problem_01** (with a capital letter).
@@ -296,7 +296,7 @@ After these changes, the structure of the project should look like this:
296
296
297
297
For each subsequent exercise, we will create a new class in the way described above.
298
298
299
-
### Problem: An element equal to the sum of the others
299
+
### Problem: Half Sum Element
300
300
301
301
Write a program that inputs **n integers** and checks if there is a number among them that is equal to the sum of all others. If there is such an element, print **"Yes" + its value**. Otherwise, print **"No" + the difference between the largest element and the sum of the rest**, as an absolute value.
302
302
@@ -321,7 +321,7 @@ Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/655#
321
321
322
322
### Problem: Odd \/ Even position
323
323
324
-
Write a program that reads **n numbers** and calculates **the sum**, the **min** and **max** values of the numbers on **even** and **odd** positions (counted from 1). If there is no min / max element, print **"No"**.
324
+
Write a program that reads **n numbers** and calculates **the sum**, the **min**, and **max** values of the numbers on **even** and **odd** positions (counted from 1). If there is no min / max element, print **"No"**.
325
325
326
326
#### Sample Input and Output
327
327
@@ -336,7 +336,7 @@ Write a program that reads **n numbers** and calculates **the sum**, the **min**
336
336
337
337
The problem combines several previous problems: finding the **min**, **max**, and the **sum**, as well as processing elements on **even and odd positions**. Check your solutions to the previous problems again.
338
338
339
-
In the current problem, it is better to work with **fractions** (not integers). The sum, the min, and the max value will also be fractions. We have to use **neutral starting value** in finding the min / max value, for example **1000000000.0** and **-1000000000.0**. If the result is equal to the neutral value, we will print **“No”**.
339
+
In the current problem, it is better to work with **fractions** (not integers). The sum, the min, and the max value will also be fractions. We have to use a **neutral starting value** in finding the min / max value, for example **10000.0** and **-10000.0**. If the result is equal to the neutral value, we will print **“No”**.
340
340
341
341
#### Testing in The Judge System
342
342
@@ -364,16 +364,16 @@ The input consists of number **n**, followed by **2\*n integers**, one per line.
364
364
365
365
#### Hints and Guidelines
366
366
367
-
We read the input numbers **in pairs**. For each pair, we calculate the **sum**. While reading the input pairs, for each pair, except the first one, we have to calculate **the difference with the previous one**. To do that, we need to store the sum of the previous pair in a separate variable. Finally, we find the **largest difference** between two pairs. If it is **0**, print **“Yes”** + the value, otherwise - **“No”** + the difference.
367
+
We read the input numbers **in pairs**. For each pair, we calculate the **sum**. While reading the input pairs, for each pair, except the first one, we have to calculate **the difference with the previous one**. To do that, we need to store the sum of the previous pair in a separate variable. Finally, we find the **largest difference** between the two pairs. If it is **0**, print **“Yes”** + the value, otherwise - **“No”** + the difference.
368
368
369
369
#### Testing in The Judge System
370
370
371
371
Test your solution here: [https://judge.softuni.org/Contests/Practice/Index/655#11](https://judge.softuni.org/Contests/Practice/Index/655#11).
372
372
373
373
374
-
## Problem: Graphical and web applications
374
+
## Problem: Graphical and Web Applications
375
375
376
-
In the current chapter, we learned about **loops** as a construction in programming that allows us to repeat a given action or a group of actions many times. Now let us play with them. To do that, we will draw some figures that will consist of many repeating graphical elements. This time, we will not do it in the console but in a graphical environment using "**turtle graphics**". It will be interesting. And it is not at all complicated. Try it!
376
+
In the current chapter, we learned about **loops** as construction in programming that allows us to repeat a given action or a group of actions many times. Now let us play with them. To do that, we will draw some figures that will consist of many repeating graphical elements. This time, we will not do it in the console but in a graphical environment using "**turtle graphics**". It will be interesting. And it is not at all complicated. Try it!
377
377
378
378
### Problem: Turtle Graphics GUI Application
379
379
@@ -386,7 +386,7 @@ Let us get familiar with **the concept of drawing "Turtle Graphics"**. Take a lo
386
386
* Article on "turtle graphics" in Wikipedia – [https://en.wikipedia.org/wiki/Turtle_graphics](https://en.wikipedia.org/wiki/Turtle_graphics)
387
387
* Interactive online tool for drawing with a turtle – [https://blockly-games.appspot.com/turtle](https://blockly-games.appspot.com/turtle)
388
388
389
-
We will start by creating a new **Java project** in **IntelliJ IDEA**. Add new package **`app`** (earlier in this chapter we described how to do it). Download **`MyTurtle.java`** and **`jturtle-0.1.1.jar`** from [https://github.com/SoftUni/Programming-Basics-Book-Java-EN/tree/master/assets/chapter-5-1-assets](https://github.com/SoftUni/Programming-Basics-Book-Java-EN/tree/master/assets/chapter-5-1-assets). **`MyTurtle.java`** is a pre-written class, which will help us get acquainted faster with the library **`jturtle-0.1.1.jar`**, which runs **the turtle**. Using file explore, we copy **`MyTurtle.java`** into the directory **`app`** of the project. Then we need to add the external library **`jturtle-0.1.1.jar`** in our project. This is done as follows:
389
+
We will start by creating a new **Java project** in **IntelliJ IDEA**. Add a new package **`app`** (earlier in this chapter we described how to do it). Download **`MyTurtle.java`** and **`jturtle-0.1.1.jar`** from [https://github.com/SoftUni/Programming-Basics-Book-Java-EN/tree/master/assets/chapter-5-1-assets](https://github.com/SoftUni/Programming-Basics-Book-Java-EN/tree/master/assets/chapter-5-1-assets). **`MyTurtle.java`** is a pre-written class, which will help us get acquainted faster with the library **`jturtle-0.1.1.jar`**, which runs **the turtle**. Using file explore, we copy **`MyTurtle.java`** into the directory **`app`** of the project. Then we need to add the external library **`jturtle-0.1.1.jar`** in our project. This is done as follows:
390
390
* Select **Project Structure** from the **File** menu (CTRL + SHIFT + ALT + S).
391
391
* Click on **Select Modules** in the left pane.
392
392
* Click on the **Dependencies** tab.
@@ -406,7 +406,7 @@ In the next window, click [**OK**], and we are ready to open **`MyTurtle.java`**
These are the main actions in `Turtle` class with which we draw:
412
412
@@ -543,4 +543,4 @@ Add [**Triangle**] button, which draws three triangles with 22 vertices each, as
543
543
544
544
Draw in a loop by moving forward and rotating. In each step, increase the length of the forward step by 10 and rotate 120 degrees. Repeat 3 times for the three triangles.
545
545
546
-
If you have a problem with the exercises above, ask for help in the official **SoftUni Reddit Forum**: https://www.reddit.com/r/softuni/.
546
+
If you have a problem with the exercises above, ask for help in the official **SoftUni Reddit Forum**: https://www.reddit.com/r/softuni/.
0 commit comments