One day to learn spark's Scala basic grammar tutorial 2. Operators and branch statements (idea version)

📋Preface📋 💝Blog homepage: Red eye aromatherapy_CSDN blog-Big data, computer theory, MySQL field blogger💝 ✍This article was originally created by Zai Xia [Red Eye Aromatherapy], first published on CSDN✍ 🤗The biggest wish in 2022: [Serving millions of technical people]🤗 💝Initial environment address:[ spark environment construction (idea version) - Hongmu aromatherapy - CSDN blog]💝

Environmental requirements

Environment: win10 Development tools: IntelliJ IDEA 2021.2 maven version: 3.6.3

Table of contents

📋Preface📋

Environmental requirements

Scala operators

arithmetic operator

relational operator

Logical Operators

assignment operator

operator precedence

Scala if else branch statement

if statement

grammar

if...else statement

grammar

if...else if...else statement

grammar

Summarize

Create a test class [day1/demo2.scalc], select Object as the type

Scala operators

An operator is a symbol that tells the compiler to perform specified mathematical and logical operations.

Scala has a rich set of built-in operators, including the following types:

  • arithmetic operator
  • relational operator
  • Logical Operators
  • bitwise operator
  • assignment operator

Next, we will introduce the application of the above operators in detail.

arithmetic operator

The following table lists the arithmetic operators supported by Scala.

Assume variable A is 10 and B is 20:

operator

describe

example

+

plus

The result of A + B operation is 30

-

minus sign

A - B operation result is -10

*

Multiplication sign

The result of A * B operation is 200

/

division sign

The result of B / A operation is 2

%

Take the remainder

B % A operation result is 0

example

package day1

object demo2 {
  def main(args: Array[String]) {
    var a = 5;
    var b = 6;
    var c = 7;
    var d = 8;
    println("a + b = " + (a + b) );
    println("a - b = " + (a - b) );
    println("a * b = " + (a * b) );
    println("b / a = " + (b / a) );
    println("b % a = " + (b % a) );
    println("c % a = " + (c % a) );

  }
}
copy

relational operator

The following table lists the relational operators supported by Scala.

Assume variable A is 10 and B is 20:

operator

describe

example

==

equal

(A == B) evaluates to false

!=

not equal to

(A != B) evaluates to true

>

more than the

(A > B) evaluates to false

<

less than

(A < B) evaluates to true

>=

greater or equal to

(A >= B) evaluates to false

<=

less than or equal to

(A <= B) evaluates to true

example

package day1

object demo2 {
  def main(args: Array[String]) {
    var a = 5;
    var b = 6;
    println("a == b = " + (a == b) );
    println("a != b = " + (a != b) );
    println("a > b = " + (a > b) );
    println("a < b = " + (a < b) );
    println("b >= a = " + (b >= a) );
    println("b <= a = " + (b <= a) );

  }
}
copy

Logical Operators

The following table lists the logical operators supported by Scala.

Assume variable A is 1 and B is 0:

operator

describe

example

&&

logic and

(A && B) evaluates to false

||

logical or

(A || B) evaluates to true

!

logical NOT

!(A && B) evaluates to true

example

package day1

object demo2 {
  def main(args: Array[String]) {
    var a = true;
    var b = false;

    println("a && b = " + (a&&b) );

    println("a || b = " + (a||b) );

    println("!(a && b) = " + !(a && b) );

  }
}
copy

assignment operator

The assignment operators supported by the Scala language are listed below:

operator

describe

example

=

A simple assignment operation, specifying that the right operand is assigned to the left operand.

C = A + B Assign the operation result of A + B to C

+=

Add and then assign, add the left and right operands together and assign to the left operand.

C += A is equivalent to C = C + A

-=

Subtract and then assign, subtract the left and right operands and then assign to the left operand.

C -= A is equivalent to C = C - A

*=

Multiply and then assign, the left and right operands are multiplied and then assigned to the left operand.

C *= A is equivalent to C = C * A

/=

Assign after division, divide the left and right operands and then assign to the left operand.

C /= A is equivalent to C = C / A

%=

After the remainder is assigned, the remainder of the left and right operands is assigned to the left operand.

C %= A is equivalent to C = C % A

<<=

Bitwise left shift and then assign

C <<= 2 is equivalent to C = C << 2

>>=

Bitwise right shift and then assign

C >>= 2 is equivalent to C = C >> 2

&=

Assignment after bitwise AND operation

C &= 2 is equivalent to C = C & 2

^=

Bitwise XOR operator and then assignment

C ^= 2 is equivalent to C = C ^ 2

|=

Bitwise OR operation and then assignment

C |= 2 is equivalent to C = C | 2

example

package day1

object demo2 {
  def main(args: Array[String]) {
    var a = 5;
    var b = 6;
    var c = 0;

    c = a + b;
    println("c = a + b  = " + c );

    c += a ;
    println("c += a  = " + c );

    c -= a ;
    println("c -= a = " + c );

    c *= a ;
    println("c *= a = " + c );

    a = 10;
    c = 15;
    c /= a ;
    println("c /= a  = " + c );

    a = 10;
    c = 15;
    c %= a ;
    println("c %= a  = " + c );

    c <<= 2 ;
    println("c <<= 2  = " + c );

    c >>= 2 ;
    println("c >>= 2  = " + c );

    c >>= a ;
    println("c >>= a  = " + c );

    c &= a ;
    println("c &= 2  = " + c );

    c ^= a ;
    println("c ^= a  = " + c );

    c |= a ;
    println("c |= a  = " + c );
  }
}
copy

operator precedence

Example: Calculate multiplication and division first, then addition and subtraction. Parentheses look first.

Check the following table, the priority is descending from top to bottom, the top has the highest priority, and the comma operator has the lowest priority.

category

operator

relevance

1

() []

left to right

2

! ~

right to left

3

* / %

left to right

4

+ -

left to right

5

>> >>> <<

left to right

6

> >= < <=

left to right

7

== !=

left to right

8

&

left to right

9

^

left to right

10

|

left to right

11

&&

left to right

12

||

left to right

13

= += -= *= /= %= >>= <<= &= ^= |=

right to left

14

,

left to right

Scala if else branch statement

The if else statement is a code block that is determined to be executed by the execution result (True or False) of one or more statements.

You can simply understand the execution process of the conditional statement through the following figure:

if statement

An if statement consists of a Boolean expression followed by a block of statements.

grammar

The syntax of the if statement is as follows:

package day1

object demo2 {
  def main(args: Array[String]) {
    var isf=true;
    if(isf)
    {
      // Executes the block of statements if the Boolean expression is true
      print("is true");
    }
  }
}
copy

if...else statement

The if statement can be followed by an else statement, and the statement block inside the else can be executed when the Boolean expression is false.

grammar

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

package day1

object demo2 {
  def main(args: Array[String]) {
    var isf=true;
    if(isf)
    {
      // Executes the block of statements if the Boolean expression is true
      print("is true");
    }else {
      print("is false");
    }
  }
}
copy

if...else if...else statement

The if statement can be followed by an else if...else statement, which is useful in the case of multiple conditional judgment statements.

grammar

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

package day1

object demo2 {
  def main(args: Array[String]) {
   var i=100;
    if(i>90&i<=100){
      print("excellent");
    }else if(i>=80){
      print("good");
    }else if(i>=70){
      print("generally");
    }else if(i>=60){
      print("pass");
    }else{
      print("failed");
    }
  }
}
copy

Summarize

This is the end of the Scala Basic Grammar Tutorial about spark II. Operators and Branch Statements (idea version) hope that it can help us.

Tags: Scala

Posted by PurpleMonkey on Tue, 29 Nov 2022 20:02:02 +0530