scala Quick Start series [cycle]

        Benpian scala fast as Title VII series of blog entry, for everyone to bring about cycling related content.
        


        

cycle

        In the scala can be used for and while, but generally recommended for expression, because for more concise expression syntax.

for expression

grammar

Here Insert Picture Description

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
Here Insert Picture Description

Reference code 2 (shorthand)

Here Insert Picture Description


Nested loop

Use a for loop, print the following characters
Here Insert Picture Description
step

  1. Use for expression print 3 lines, 5 stars
  2. Each print five stars, wrap

Reference Code
Here Insert Picture Description


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
Here Insert Picture Description
example

Use for expression can print between 1 and 10 numbers divisible by 3

Reference Code

Here Insert Picture Description

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
Here Insert Picture Description


while loop

scala while loop and is consistent java

Examples

Digital printing 1-10

Reference Code
Here Insert Picture Description

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!
Here Insert Picture Description

Published 148 original articles · won praise 1348 · Views 350,000 +

Guess you like

Origin blog.csdn.net/weixin_44318830/article/details/103962535