Java Tutorial [#04] - All Loops + more Math

in programming โ€ขย  7 years agoย  (edited)

Header.jpg

Disclaimer!
If you find any mistakes in this post or you don't know what to do at a specific point? Feel free to write me a comment! ๐Ÿ‘
Greets from Germany โœŒ๏ธ

Last Part Next part
<- #03 Escape Sequences and better print instructions #05 First coding challanges ->

1. if-Loop


This loop is the most simple and basic one. If something is true, then do this, anything else? Then do this. It is written like this if (argument) { } else { }

In this example, I ask the program, if x (5) is smaller than y (8). If x is smaller than y, then print "x is smaller than y". Else print "x is bigger or equal y".

Image 2.PNG

And if I change y to 4, than it will give me this output

Image 4.PNG

But what if you want to separate the bigger and equal print, then change your code to this

Image 5.PNG

The else if (argument) if the same as the normal if instruction, but built in, in a if-Loop.

2. switch-Loop


Here you give the instruction a Integer variable and he will work with him. This should look like this:

Image 7.PNG

Note: The break; statement means, that it will jump out of the loop, otherwise it would completely run through.

He will read the number of the variable and go through the loop. In case the number is 2, then he prints, what's standing inside of it. But if the number doesn't attach to anything, than he will enter the default block. In my case, x is 5

Image 8.PNG

But when I change x to 2 and rerun, then it will output this

Image 9.PNG

3. for-Loop


This might be a bit hard to understand at the beginning, but I will do my best to explain it to you

Image 10.PNG

Image 11.PNG

for (create variable + initialization; as long as true; add 1 to variable) { }
I have really problems to explain this one ^^ Please try to understand this with the information, I gave you.

4. while-Loop


A while-Loop is this

Image 12.PNG

This is the syntax: while(boolean) { }
As long as the boolean is true, the loop will continue to run. When it's false, then it will stop.

Image 13.PNG

Fun Fact:
You can create a infinit-Loop by entering for the boolean a true

Image 14.PNG

Animation 1.gif

That's it for the loop tutorial. Thanks for reading!

Support this project by upvote this post and commenting ^^ (and resteem)

Last Part Next part
<- #03 Escape Sequences and better print instructions #05 First coding challanges ->

Remember: I'm not a genius too! I'm 16 years old but I know quite a lot, because Java or programming in general is fascinating me so much (and yes I have friends and other hobbies :)). So don't give up! Follow you dreams and in some day, they will get true.
Authors get paid when people like you upvote their post.
If you enjoyed what you read here, create your account today and start earning FREE STEEM!
Sort Order: ย 
ย  ยท ย 7 years agoย 

if and switch are not "loops".

a "loop" is something that repeats a block of code as long as a condition is true.

may i suggest using the title "control structures" for this tutorial. (see https://en.wikiversity.org/wiki/Control_structures )