编程中常用的代码有很多种,以下是一些常见的编程语言和它们的常用代码示例:
Python
- 打印输出
print("Hello, World!")
- 条件判断
if x > 10:
print("x is greater than 10")
else:
print("x is not greater than 10")
- 循环
for i in range(5):
print(i)
- 函数定义
def greet(name):
return "Hello, " + name
Java
- 打印输出
System.out.println("Hello, World!");
- 条件判断
if (x > 10) {
System.out.println("x is greater than 10");
} else {
System.out.println("x is not greater than 10");
}
- 循环
for (int i = 0; i < 5; i++) {
System.out.println(i);
}
- 方法定义
public static int add(int a, int b) {
return a + b;
}
JavaScript
- 打印输出
console.log("Hello, World!");
- 条件判断
if (x > 10) {
console.log("x is greater than 10");
} else {
console.log("x is not greater than 10");
}
- 循环
for (let i = 0; i < 5; i++) {
console.log(i);
}
- 函数定义
function add(a, b) {
return a + b;
}
C++
- 打印输出
#include <iostream>
using namespace std;
int main() {
cout << "Hello, World!" << endl;
return 0;
}
- 条件判断
if (x > 10) {
cout << "x is greater than 10" << endl;
} else {
cout << "x is not greater than 10" << endl;
}
- 循环
for (int i = 0; i < 5; i++) {
cout<< i << endl;
}
- 函数定义
int add(int a, int b) {
return a + b;
}
C
- 打印输出
using System;
class Program {
static void Main() {
Console.WriteLine("Hello, World!");
}
}
- 条件判断
if (x > 10) {
Console.WriteLine("x is greater than 10");
} else {
Console.WriteLine("x is not greater than 10");
}
- 循环
for (int i = 0; i < 5; i++) {
Console.WriteLine(i);
}
- 方法定义
public static int Add(int a, int b) {
return a + b;
}
这些只是编程中常用代码的一部分,每种编程语言都有其特定的语法和常用代码结构。掌握这些基础代码可以帮助你快速编写简单的程序。