Scala Basics Tutorial (iv): If statement, Loop statement, while statement _scala

Source: Internet
Author: User

Here is a typical decision to make if ... Else the general form of the structure is used in most programming languages:

If statement:

The If statement consists of a Boolean expression followed by one or more statements. Grammar:

Syntax for an IF statement:

if (boolean_expression)
{
Statements'll execute if the Boolean expression is true
}

If the value of the Boolean expression is true, then the code module inside the IF statement is executed. If this is not the case, the first set of code if statement after the end (after the closing curly brace) will be executed. Example:

Object Test {
def main (args:array[string]) {
var x = 10;
if (x < 20) {
println ("This is if statement");
}
}
}

This produces the following output results:

C:/>scalac Test.scala
C:/>scala Test
This is if statement
C:/>
If...else statement:

An If statement can follow an optional else statement, and the Boolean expression condition is false when the else block executes. Grammar:

The syntax for If...else is:

if (boolean_expression) {
Executes when the Boolean expression is true
}else{
Executes when the Boolean expression is false
}
Example:
Object Test {
def main (args:array[string]) {
var x = 30;
if (x < 20) {
println ("This is if statement");
}else{
println ("This is Else statement");
}
}
}

This will produce the following results:

C:/>scalac Test.scala
C:/>scala Test
This is Else statement
C:/>
If...else If...else Statement:

If statements can be followed by an optional else if ... else statement, it is very useful to use If...else if testing various conditional declarations.

When using if, else if, the else statement has several points to keep in mind.

· If there can be 0 or one else, it must be followed by else if.

· An if can have 0 to multiple else if, and they must precede else.

· Once an else if match succeeds, the remaining else if or else is not matched by the test. Grammar:

The syntax for If...else If...else is:

if (Boolean_expression 1) {
Executes when the Boolean expression 1 is True
}else if (Boolean_expression 2) {
Executes when the Boolean expression 2 is True
}else if (boolean_expression 3) {
Executes when the Boolean expression 3 is True
}else {
Executes when the none of the above condition is true.
}
Example:
Object Test {
def main (args:array[string]) {
var x = 30;
if (x = = 10) {
println ("Value of X is 10");
}else if (x = = 20) {
println ("Value of X is 20");
}else if (x = = 30) {
println ("Value of X is 30");
}else{
println ("This is Else statement");
}
}
}

This will produce the following results:

C:/>scalac Test.scala
C:/>scala Test
Value of X is 30
C:/>
If ... else statement nesting:

It is always a valid nested If-else statement, which means that you can use an if or else if in another if or else if statement. Grammar:

The syntax nesting if...else is as follows:

if (Boolean_expression 1) {
Executes when the Boolean expression 1 is True
if (Boolean_expression 2) {
Executes when the Boolean expression 2 is True
}
}

You can nest else if...else in an if statement, or vice versa. Example:

Object Test {
def main (args:array[string]) {
var x = 30;
var y = 10;
if (x = = 30) {
if (y = = 10) {
println ("X = Y = 10");
}
}
}
}

This will produce the following results:

C:/>scalac Test.scala
C:/>scala Test
X = Y = 10
C:/>

There may be a situation where several blocks of code need to be executed multiple times. In general, statement Order execution: In the first statement of a function, first executes, then the second, and so on.

The programming language provides a variety of control structures that allow for more complex execution paths.

A loop statement can execute multiple or multiple groups of statements, following in most programming languages and looping statements generally as follows:

The Scala programming language provides the following loop-type processing requirements. Click on the link below to view its details.

Loop type

Describe

While loop

A repeating statement or group of statements, when the given condition is true. It tests the condition to execute the loop before the body.

Do...while Cycle

Like a while statement, the difference is that it tests the condition at the end of the loop body

For loop

Code that executes a sequence of statements multiple times and manages loop variables in shorthand. Loop Control statement:

The loop control statement changes its normal sequential execution. When execution leaves a range, all objects created within that scope are automatically destroyed. But Scala doesn't support break or continue statements, like Java, but starting with the Scala2.8 version, there's a way to get out of the loop. Click on the link below to view the details.

Control statements

Describe

Break statement

Terminates the Loop statement and executes the following statement that immediately loops. Infinite loop:

A loop becomes an infinite loop if the condition is never false. Using the Scala,while loop is the best way to implement an infinite loop, as follows:

Object Test {
def main (args:array[string]) {
var a = 10;
an infinite loop.
while (true) {
println ("Value of A:" + a);
}
}
}

If you execute the above code, it can be terminated in an infinite loop by pressing CTRL + C.

The While Loop statement executes multiple times, as long as the given condition is true to execute the target statement. Grammar:

The syntax for the Scala while loop is:

while (condition) {
statement (s);
}

Here, the declaration can be a single statement or block of statements. The condition can be any expression, and the truth value is any non 0. Loop iterations when the condition is true. When the condition is Faklse, the program is programmed to enter the line immediately after the loop. Flow chart:

Here, the key point of the while loop is that the loop may not run forever. When the condition test results are false, the loop body skips the first statement after the while loop. Example:

Object Test {
def main (args:array[string]) {
Local variable declaration:
var a = 10;
While loop execution
while (a < 20) {
println ("Value of A:" + a);
A = a + 1;
}
}
}

When the above code is compiled and executed, it produces the following results:

C:/>scalac Test.scala
C:/>scala Test
Value of A:10
Value of A:11
Value of A:12
Value of A:13
Value of A:14
Value of A:15
Value of A:16
Value of A:17
Value of A:18
Value of a:19
C:/>

from:http://www.yiibai.com/scala/scala_basic_syntax.html

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.