1. 自增自减
int c=a++
int d=++a
1. 自增自减
int c=a++
int d=++a
自增和自减运算符
++两种用法:放在前面或者放在后面
--两种用法:放在前面或者放在后面
int a=9; int b=5;
int resl=a++;//resl=a;a++;
int res2=++b;//b++; res2=b;
System. out. printin(res1);//9
System. out. printin(res2);//6
int resl=a++ +1;//10
int res2=++b +2;//8
System.out.printin(a++);//9
System.out.println(++b);//6
自增在前,先增在运算;
自增在后,先运算在最后再自增
自增和自减运算符
++
两种用法:放在前面或者放在后面
--
两种用法:放在前面或者放在后面
public class ZiZeng{
public static void main(String[] args){
//int a=9 b=5;
int a = 9;
int b = 5;
//a++;
//b--;
//++a;
//--b;
//int res1 = a++;
//int res2 = ++b;
int res1 = a++ + 1;
int res2 = ++b + 2;
System.out.println(a);
System.out.println(b);
System.out.println(res1);// res1 = a; a++;
System.out.println(res2);// b++; res2 = b;
System.out.println(a++);
System.out.println(++b);
}
}
public class ZiZeng{
public static void main (String [] args){
int a=9;
int b =5;
//++a;
//--b;
int res1 = a++ + 1;
int res2 = ++b + 2;
int res3 = --a;
int res4 = --b;
System.out.println(a);
System.out.println(b);
System.out.println(res1);
System.out.println(res2);
System.out.println(res3);
System.out.println(res4);
}
}
自增运算符号
放前面是先自增,在赋值
放后面是先赋值,在自增
自增运算符号在操作数后面时,最后运算
public class MathOperator{
public stadtic void main(String[]args){
int a=9;
int b=5;
a++;
b--;
int rest1=a++;
int rest2=++b;
System.out.println(a);//
System.out.println(b);//
}
}
自增符号 在单独使用的情况下放置在前在后都是一样的 放在语句
a++ : res1 = a; a++
++a : a++; res2 = a
自增自减运算符
++
--
int res1 = a++ +1;//res1 = a+1
int res2 = ++b +2;//b++;res2 = b+2;
System.out.println(a);//10
System.out.println(b);//6
System.out.println(res1);//10
System.out.println(res2);//8
int c=6;
System.out.println(c++);//6
System.out.println(++c);//8
/*
自增运算符放在变量前面,则先进行自增,再在公式中进行运算。
自增运算符放在变量后面,则先在公式中进行运算,再进行自增。
从左至右运算