![]() ![]() For instance, in the example above we knew we needed to display the word “Awesome!” four times. Note: If you know in advance how many times you want the same action to repeat, then you need to use the "for" loop. Fourth, we again displayed the word “ Awesome! ” on the screen. Third, we displayed the word “ Awesome! ” on the screen.Ĥ. Second, we displayed the word “ Awesome! ” on the screen.ģ. First, we displayed the word “ Awesome! ” on the screen.Ģ. It’s obvious that you need to use a loop, because the same operation is repeated four times:ġ. Let’s say we want to display the following message on the screen:Īs you can see, the word “Awesome!” repeats four times. The "for loop" is also called the loop “with the counter”. The "do…while" loop is a very good and convenient choice when you need to execute some action at least once or, under certain circumstances, when an action needs to be repeated several times. We saw this clearly in the example above. Remember: the "do…while" loop always executes at least once. If you describe this life situation using the while loop (remember, sometimes the while loop is called the “as long as” loop ), it will look as follows: But as soon as I see that I have less than $10, it will all be over we will conclude the “rose and candy stage” and, instead, we will just sit on the couch at home and watch TV and so on." So he tells himself: “I will invite Katie to restaurants and give her flowers as long as I have at least $10 in my wallet. ![]() In short, he's romancing her.īut Charlie has a limited budget. Charlie gives her flowers, invites her to restaurants, and buys her ice cream. They are in the “rose and candy stage” - you know what that means. Let’s say you have a friend called Charlie and he has a girlfriend called Katie. Let’s start with a somewhat funny, fictional everyday example. This loop is sometimes called the "as long as” loop. So it’s high time to learn about the "while" loop. In earlier versions of JDK, it didn’t exist. The "for each" loop only appeared for the first time in JDK5. For now, all you need to know is that the "for each" loop is a variation of the "for" loop. It is the most commonly-used loop especially if the. This operator can be used to connect Arithmetic operator with an Assignment operator. Its called shorthand because it provides a short way to assign an expression to a variable. We will take a look at the "for each" loop a little bit later in another article. For-loop is a shorthand for while-do with fewer lines of code. Java provides some special Compound Assignment Operators, also known as Shorthand Assignment Operators. A for-each loop in java, also known as an enhanced for loop. So now you know what a loop is.Īt this stage, it will be enough for you to know only the first three loop types. Ternary operator is a conditional operator which is a shorthand for the if-else statement. And if the sequence of actions is the same, then we are talking about a loop. ![]() So, at least 5 times a week (Monday-Friday), you perform the same sequence of actions. open the fridge and eat everything at hand.go to the bathroom, brush your teeth, and shave your face or legs.To begin with, let’s figure out what a loop is. ![]()
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |