A comparative introduction of several statements of C++, Java, JavaScript and python

A comparative introduction of several statements of C++, Java, JavaScript and python

for statements in C++ , Java , JavaScript , and python

The syntax of the for statement in C++, Java, and JavaScript is similar to the following:

for (initial condition; loop condition; post-loop operation) {

    // loop body code

}

The initial condition is the statement executed before entering the loop, which initializes the value of the loop variable; the loop condition is the condition judged before each loop starts, and controls whether the loop continues to execute; the post-loop operation is the operation performed after each loop ends. The loop body code is the action to be performed each time through the loop.

Python's for statement generally traverses a sequence, and its syntax is as follows:

for variable in sequence:

    # Loop body code

In Python, the for loop is mainly used to iterate through a sequence. In the for statement, you can use a variable to represent the elements in the sequence in turn, and execute the loop body code each time the loop iterates.

Neither C++ nor Java has a for-in statement similar to Python and JavaScript

Python's for-in statement

The syntax is as follows:

for variable in dictionary:

    # Loop body code

The following code uses Python's for-in statement to iterate over the keys and values ​​of a dictionary:

my_dict = {"name": "Alice", "age": 25, "city": "New York"}

# iterate over the keys of the dictionary

for key in my_dict:

    print(key)

# Loop through the dictionary's values

for value in my_dict.values():

    print(value)

# Loop through the key-value pairs of the dictionary

for key, value in my_dict.items():

    print(key, value)

It should be noted that in Python, the for-in statement traverses according to the order of the keys of the dictionary, but the order of the key-value pairs of the dictionary cannot be guaranteed.

JavaScript for-in statement

In JavaScript, the for-in statement can be used to iterate over an object's enumerable properties (both own and inherited). The syntax is as follows:

for (var variable in object) {

    // loop body code

}

The following code uses JavaScript's for-in statement to iterate over the properties of an object:

var person = {name: "Alice", age: 25, city: "New York"};

// Loop through the properties of the object

for (var key in person) {

    console.log(key + ": " + person[key]);

}

It should be noted that when using the for-in statement to traverse object properties, some unnecessary properties may be traversed, such as properties on the object prototype chain. In order to avoid this situation, you can use the Object.hasOwnProperty() method to determine whether the property is an object's own property. For example:

for (var key in person) {

    if (person.hasOwnProperty(key)) {

        console.log(key + ": " + person[key]);

    }

}

This will only traverse the object's own properties.

switch statement in C++ , Java , and JavaScript

The basic structure of the switch statement in C++, Java, and JavaScript is very similar, and the syntax is as follows:

switch(expression)

{

    case value1:

        // code block

        break;

    case value2:

        // code block

        break;

    ...

    default:

        // code block

}

where expression is a conditional expression that determines which branch to execute. Each branch consists of a case label and corresponding code block, representing the case that matches a specific value. If the match is successful, the corresponding code block is executed, and the break keyword can be used to terminate the execution of the switch statement to avoid entering the next branch. If none of the branches match successfully, you can use the default tag to define a default branch to handle the case of no match.

There is no switch statement in Python.

Guess you like

Origin blog.csdn.net/cnds123/article/details/132084512