Benpian scala fast as Title VII series of blog entry, for everyone to bring about cycling related content.
Article Directory
cycle
In the scala can be used for and while, but generally recommended for expression, because for more concise expression syntax.
for expression
grammar
Simple cycle
Use for printing digital expression 1-10
step
1. Generate a digital 1-10 (Note: to use the method)
2. for expression traversal, each digital printing
Reference Code 1
Reference code 2 (shorthand)
Nested loop
Use a for loop, print the following characters
step
- Use for expression print 3 lines, 5 stars
- Each print five stars, wrap
Reference Code
guard
for expression can be added if the judge sentences, the judge if it is called the guards. We can use the expression guard make for more concise.
Syntax
example
Use for expression can print between 1 and 10 numbers divisible by 3
Reference Code
for derivations
- In the future it may be used to derive the formula for generating a new set (a set of data)
- In the for loop, may be used
yield表达式构建出一个集合
, we have referred to the use of an expression derived for the yield
Examples
Generating a set of 10, 20 ... 100
Reference Code
while loop
scala while loop and is consistent java
Examples
Digital printing 1-10
Reference Code
This blog to end here, small partners who are interested can continue to focus on the next one will bring you yo ~ 方法
related content, so stay tuned!