常量
Java
final修饰符标识终态的,
用它修饰的字段标识“常量”
希望某个常量可以在一个或多个类中的多个方法中使用,
这类常量通常叫做“类常量”。使用关键字static final修饰。
Js
Javascript中没有常量,
可以通过创建只能取值,不能赋值的私有变量来模仿常量.
Python
可以import const 通过外部包实现
Go
const variable type = value;
const LENGTH int = 10
const WIDTH int = 5
Scala
val myVal : String = “Foo”
PHP
下例创建了一个对大小写不敏感的常量,值为 “Welcome!”:
实例
<?php
define("GREETING", "Welcome!", true);
echo greeting;
?>
条件控制
Java
if (flag1) {
System.out.println("黄文强在");
} else if (flag2) {
System.out.println("刘克强在");
} else {
System.out.println("他们不在");
}
switch(expression){
case value :
//语句
break; //可选
case value :
//语句
break; //可选
//你可以有任意数量的case语句
default : //可选
//语句
}
while( 布尔表达式 ) {
//循环内容
}
do {
//代码语句
}while(布尔表达式);
for(int x = 10; x < 20; x = x+1)
System.out.print("value of x : " + x );
Js
for (var i=0;i<cars.length;i++)
{
document.write(cars[i] + "<br>");
}
var person={fname:"John",lname:"Doe",age:25};
for (x in person)
{
txt=txt + person[x];
}
其它和Java相似
Python
1.
if 判断条件1:
执行语句1……
elif 判断条件2:
执行语句2……
else:
执行语句3……
不支持switch,自己实现
3.
count = 0
while count < 5:
print count, " is less than 5"
count = count + 1
else:
print count, " is not less than 5"
1)使用range函数
for i in range(beigin,end):
#stadements
这个语句就是和通用的C语言for循环功能一样了
2)使用列表或元组
for i in tList:
#stadements
或者
for i in tList[n:]:
#statements
Python pass是空语句,是为了保持程序结构的完整性。
pass 不做任何事情,一般用做占位语句。
Go
//if ,goto, for ,switch
if x := computedValue(); x > 10 {
fmt.Println("x is greater than 10")
} else {
fmt.Println("x is less than 10")
}
if integer == 3 {
fmt.Println("The integer is equal to 3")
} else if integer < 3 {
fmt.Println("The integer is less than 3")
} else {
fmt.Println("The integer is greater than 3")
}
func myFunc() {
i := 0
Here: //这行的第一个词,以冒号结束作为标签
println(i)
i++
goto Here //跳转到Here去
}
sum := 0;
for index:=0; index < 10 ; index++ {
sum += index
}
for _, v := range map{
fmt.Println("map's val:", v)
}
switch i {
case 1:
fmt.Println("i is equal to 1")
case 2, 3, 4:
fmt.Println("i is equal to 2, 3 or 4")
case 10:
fmt.Println("i is equal to 10")
default:
fmt.Println("All I know is that i is an integer")
}
Scala
//1.
if( x == 10 ){
println("X 的值为 10");
}else if( x == 20 ){
println("X 的值为 20");
}else{
println("无法判断 X 的值");
}
//2.
object PatternMatching extends App{
var list=new ArrayBuffer[Int]()
var x=0
for(i<- 1 to 100){
i match {
//后面可以跟表达式
case 10 => x=10
case 80 => println(80)
case _ if(i%4==0)=> list.append(i)
case _ if(i%3==0)=> println(i+":能被3整除")
case _ =>
}
}
println(x)
}//抽象类Person
//3.
abstract class Person
case class Student(name:String,age:Int,studentNo:Int) extends Person
case class Teacher(name:String,age:Int,teacherNo:Int) extends Person
case class Nobody(name:String) extends Person
object CaseClassDemo{
def main(args: Array[String]): Unit = {
//case class 会自动生成apply方法,从而省去new操作
val p:Person=Student("john",18,1024)
//match case 匹配语法
p match {
case Student(name,age,studentNo)=>println(name+":"+age+":"+studentNo)
case Teacher(name,age,teacherNo)=>println(name+":"+age+":"+teacherNo)
case Nobody(name)=>println(name)
}
}
}
//4.
while(condition)
{
statement(s);
}
//5.
do {
statement(s);
} while( condition );
//6.
for( a <- 1 to 10){
println( "Value of a: " + a );
}
for( a <- 1 until 10){
println( "Value of a: " + a );
}
val numList = List(1,2,3,4,5,6,7,8,9,10);
// for 循环
for( a <- numList
if a != 3; if a < 8 ){
println( "Value of a: " + a );
}
var retVal = for{ a <- numList
if a != 3; if a < 8 }yield a
PHP
#1.
if ($t<"10")
{
echo "Have a good morning!";
}
elseif ($t<"20")
{
echo "Have a good day!";
}
else
{
echo "Have a good night!";
}
#2.
$favcolor="red";
switch ($favcolor)
{
case "red":
echo "你喜欢的颜色是红色!";
break;
case "blue":
echo "你喜欢的颜色是蓝色!";
break;
case "green":
echo "你喜欢的颜色是绿色!";
break;
default:
echo "你喜欢的颜色不是 红, 蓝, 或绿色!";
}
#3.
while($i<=5)
{
echo "The number is " . $i . "<br>";
$i++;
}
#4.
do
{
要执行的代码;
}
while (条件);
#5.
for ($i=1; $i<=5; $i++)
{
echo "The number is " . $i . "<br>";
}
#6.
$x=array("one","two","three");
foreach ($x as $value)
{
echo $value . "<br>";
}