DEV Community

Rory-lhj
Rory-lhj

Posted on

HarmonyOSNext — — Cangjie Basic Syntax and Structure

This article will delve into the basic syntax and structure of the Cangjie language. These fundamental knowledge lay a solid foundation for writing efficient and maintainable code. By understanding statement structures, expressions, comments, and data types, you can use Cangjie for programming with greater confidence.
Keywords
Cangjie basic syntax
Statement structure
Expression
Comment
Data type
Control structure

I. Basic Syntax
1.1 Statement Structure
The statement structure in Cangjie forms the fundamental building blocks of code, including assignment statements, conditional statements, and loop statements.

Assignment Statements: Used to assign values to variables.

let x: Int64 = 10
println(x)
Enter fullscreen mode Exit fullscreen mode

output:10

Conditional Statements: Used to control the execution flow of a program.

if (x > 0) {
    println("x is +")  
}
output: x is +

for (i in 1..=5) {
    println(i) 
}
Enter fullscreen mode Exit fullscreen mode

output: 1 2 3 4 5

Please translate the following text into English.

1.2 Expressions
Cangjie supports various expressions, including arithmetic operations and logical operations.

Arithmetic Operations: Such as addition, subtraction, etc.

let a: Int64 = 1
let b: Int64 = 2
let sum: Int64 = a + b
println(sum)
Enter fullscreen mode Exit fullscreen mode

output:3

Logical Operations: Such as AND, OR, etc.

if (a > 0 && b < 10) {
    println("a > 0 && b < 10") 
}
Enter fullscreen mode Exit fullscreen mode

output:a > 0 && b < 10

II. Variable Naming and Identifiers Overview
In the Cangjie programming language, developers assign names to program elements, which are called identifiers. Identifiers are categorized into ordinary identifiers and raw identifiers, each adhering to distinct naming rules.

III. Comments
Comments are used to improve code readability and maintainability. In Cangjie, there are two types of comments:

3.1 Single-Line Comments
Single-line comments use // and can be used to explain code or temporarily block code lines.

println("Welcome to Cangjie!")
output:Welcome to Cangjie!
Enter fullscreen mode Exit fullscreen mode

3.2 Multi-Line Comments
Multi-line comments are enclosed within /* */ and are suitable for longer explanations or blocking multiple lines of code.


func factorial(n: Int): Int {
}
Enter fullscreen mode Exit fullscreen mode

IV. Data Types
Cangjie supports several data types, including:

Integer Types、Floating-Point Types、String Type、Boolean Type

let age: Int64 = 25
let height: Float64 = 1.75
let name: String = "Cangjie" 
let isAdult: Bool = true
Enter fullscreen mode Exit fullscreen mode

V. Control Structures
Control structures are used to manage the execution flow of a program, including conditional logic and loop constructs.

Use if, else if, and else for conditional judgment.

The for loop and while loop are used to repeatedly execute code blocks.

Summary
This article provides a comprehensive introduction to the basic syntax and structure of the Cangjie language, including statement structures, expressions, comments, data types, and control structures. Mastering these fundamental concepts will lay a solid foundation for subsequent programming learning and serve as the basis for creating efficient and maintainable code.

Top comments (0)