Page 337 - Computer_Science_F5
P. 337
Computer Science Output:
FOR ONLINE READING ONLY
Figure 5.6: Program in JDK
In Program Example 5.4, you observed that for both integers and floating-point
numbers, the remainder is the same, equals to 1.
Increment and decrement in Java
In Java, the increment operator (++) increases the value of a variable by 1, while the
decrement operator (--) decreases it by 1. These operators can be applied as either
prefix or postfix forms, altering whether the variable is incremented or decremented
before or after its value is used in an expression. They are commonly used in loops
for iterating through collections or controlling program flow based on numeric
conditions. Workout Program Example 5.5 on increment and do extra in decrement
in any editor then compile and run the program in JDK using cmd.exe:
Program Example 5.5:
Java program to demonstrate increment operator
public class Increment {
public static void main(String[] args) {
int x = 10;
// Postfix, Using x++ when x=10
int y1 = x++;
System.out.println(“Answer after x++: “ + y1); // Output: 10
System.out.println(“Incremented value of x: “ + x); // Output: 11
328
for Advanced Secondary Schools
Computer Science Form 5.indd 328 23/07/2024 12:34

