spark's basic Scala syntax tutorial II, operators and branch statements (idea version)

Posted by liljim on Wed, 26 Jan 2022 20:14:12 +0100

πŸ“‹ preface πŸ“‹

πŸ’ Blog home page: Red eye Aromatherapy_ CSDN blog - big data, computer theory, MySQL BloggerπŸ’

✍ This article was originally written by Yu Xia [Hongmu aromatherapy] and was first launched in CSDN ✍

πŸ€— Biggest wish in 2022: [serve millions of technical people] πŸ€—

πŸ’ Initial environment address:[ spark environment construction (idea version)_ Red eye Aromatherapy - CSDN blog]πŸ’

Environmental requirements

Environment: win10

Development tool: IntelliJ IDEA 2021.2

maven version: 3.6.3

catalogue

πŸ“‹ preface πŸ“‹

Environmental requirements

Scala operator

Arithmetic operator

Relational operator

Logical operator

Assignment Operators

Operator priority

Scala if else branch statement

if statement

grammar

if...else statement

grammar

if...else if...else statement

grammar

summary

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

Scala operator

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

Scala is rich in built-in operators, including the following types:

  • Arithmetic operator

  • Relational operator

  • Logical operator

  • Bitwise Operators

  • Assignment Operators

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

Arithmetic operator

The following table lists the arithmetic operators supported by Scala.

Suppose variable A is 10 and B is 20:

operatordescribeexample
+plusThe result of A + B operation is 30
-minus signThe result of A - B operation is - 10
*multiplication signThe result of A * B operation is 200
/division signThe result of B / A operation is 2
%SurplusB% 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) );

  }
}

Β 

Relational operator

The following table lists the relational operators supported by Scala.

Suppose variable A is 10 and B is 20:

operatordescribeexample
==be equal to(A == B) the operation result is false
!=Not equal to(a! = b) the operation result is true
>greater than(a > b) the operation result is false
<less than(a < b) the operation result is true
>=Greater than or equal to(a > = b) the operation result is false
<=Less than or equal to(a < = b) the operation result is 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) );

  }
}

Logical operator

The following table lists the logical operators supported by Scala.

Suppose variable A is 1 and B is 0:

operatordescribeexample
&&Logic and(A & & B) operation result is false
||Logical or(a | b) the operation result is true
!Logical non! (A & & B) operation result is 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) );

  }
}

Assignment Operators

The following lists the assignment operators supported by the Scala language:

operatordescribeexample
=A simple assignment operation that assigns the right operand to the left operand.C = A + B assigns the operation result of A + B to C
+=The left and right operands are added and then assigned to the left operands.C += A is equivalent to C = C + A
-=Subtract and then assign values. Subtract the left and right operands and assign values to the left operands.C -= A is equivalent to C = C - A
*=The left and right operands are multiplied and then assigned to the left operand.C *= A is equivalent to C = C * A
/=Divide and assign values. Divide the left and right operands and assign values to the left operands.C /= A is equivalent to C = C / A
%=The left and right operands are summed and then assigned to the left operands.C %= A is equivalent to C = C % A
<<=Shift the bit to the left and then assign the valueC < < = 2 is equivalent to C = C < < 2
>>=Shift the bit to the right and then assign the valueC > > = 2 is equivalent to C = C > > 2
&=Assignment after bitwise and operationC & = 2 is equivalent to C = C & 2
^=Assign value after bitwise exclusive or operatorC ^= 2 is equivalent to C = C ^ 2
|=Assign value after bitwise OR operationC | 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 );
  }
}

Operator priority

Example: multiply and divide first, then add and subtract. Brackets look first.

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

categoryoperatorRelevance
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

if else statement is a code block that determines the execution result (True or False) of one or more statements.

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

if statement

if statements are composed of Boolean expressions and subsequent statement blocks.

grammar

The syntax format of if statement is as follows:

package day1

object demo2 {
  def main(args: Array[String]) {
    var isf=true;
    if(isf)
    {
      // If the Boolean expression is true, the statement block is executed
      print("is true");
    }
  }
}

Β 

if...else statement

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

grammar

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

package day1

object demo2 {
  def main(args: Array[String]) {
    var isf=true;
    if(isf)
    {
      // If the Boolean expression is true, the statement block is executed
      print("is true");
    }else {
      print("is false");
    }
  }
}

if...else if...else statement

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

grammar

if...else if...else syntax format 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("commonly");
    }else if(i>=60){
      print("pass");
    }else{
      print("fail,");
    }
  }
}

summary

Here is the basic Scala syntax tutorial about spark. 2. Operators and branch statements (idea version) are over

I hope it can help you.

Welcome to triple click, thank you.

Topics: Java Big Data Maven intellij-idea