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".
And if I change y to 4, than it will give me this output
But what if you want to separate the bigger and equal print, then change your code to this
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:
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
But when I change x to 2 and rerun, then it will output this
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
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
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.
Fun Fact:
You can create a infinit-Loop by entering for the boolean a true
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.
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 )
Downvoting a post can decrease pending rewards and make it less visible. Common reasons:
Submit