Kotlin Tutorial Learning-Control Flow

Source: Internet
Author: User
Tags case statement

Kotlin's control flow is the same as in other languages, order, branching, looping

There are two types of branching, if-else and when (similar to switch in Java).

If statement:


Operation Result:

The case statement in Java not only supports integer values, the latest JVM also supports strings, kotlin when statements are more powerful, switch variables can even be various types such as Double,array, in other words when statements are more powerful than switch statements, The ability to if-else statements is already comparable:


Operation Result:

While and Do-while statements

Not much different from Java

Operation Result:

For loop, use range as the For loop control rule:


[Email protected] and [email protected] similar to the label labels in Java, can be used for break and continue statements, the label can appear almost anywhere

Operation Result:

Simplified flow of control;

There are other uses in Kotlin:


The If-else statement and the When statement can appear in a separate statement to initialize a variable.

Operation Result:

This usage can be used instead of ternary expressions,

Kotlin also has ternary expressions, but unlike in Java, ternary expressions in Kotlin are used in null control and are now not discussed.

Kotlin Tutorial Learning-Control Flow

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.