Adding (incrementing) or subtracting (decrementing) the value one from an intege
ID: 3788786 • Letter: A
Question
Adding (incrementing) or subtracting (decrementing) the value one from an integer variable is a common, everyday operation. To increment an int variable x, we could code x = x + 1; As an alternative, we could use the special operators ++ and -- to increment and decrement a variable. Use the first method to increment x in the program below. Print the value of x after incrementing. Use the ++ operator to increment in the program below. Print the value of y after incrementing. public class Increment Demo public static void main(String[] args) {int x = 10: int y = 3;//Put your code here}}Explanation / Answer
/**
*
* The java progam IncrementDemo that demonstrates
* the increment oeprator ++ for x and y values and
* print results to console.*/
//IncrementDemo.java
public class IncrementDemo {
public static void main(String[] args) {
//initialize x and y values
int x=10;
int y=-3;
System.out.println("Before incrementing");
//print before incrementing x and y values
System.out.println("x= "+x+" y ="+y);
System.out.println("After incrementing");
//increment of x
x++;//x=x+1=10+1=11
System.out.println("x ="+x);
y++;//y=y+1=-3+1=-2
System.out.println("y ="+y);
}
}
----------------------------
Sample output:
Before incrementing
x= 10 y =-3
After incrementing
x =11
y =-2
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.