Lucky Cat Tutoring Services


CIT111: Lets Add and Subtract
September 17, 2009, 3:17 pm
Filed under: Uncategorized | Tags: , , , ,

With incrementing and decrementing, things can be in many different orders. You can prefix, or postfix and write it in different ways.

Example: “++number” is the same as “number++” or “number +=1”.

What happens with prefixing:
Say you have a number 4 and ++number. When it runs the program it will starting 5, not 4. If you put it after, then it will start with 4. Here’s a small example program.

/**
This program demonstrates the ++ and — operators.
*/

public class IncrementDecrement
{
public static void main(String[] args)
{
int number = 4; // number starts out with 4

// Display the value in number.
System.out.println(“number is ” + number);
System.out.println(“I will increment number.”);

// Increment number.
number++; //number=number + 1; number += 1 Can put ++ or — infront of variable

// Display the value in number again.
System.out.println(“Now, number is ” + number);
System.out.println(“I will decrement number.”);

// Decrement number.
number–; //number=number-1; number -=1

// Display the value in number once more.
System.out.println(“Now, number is ” + number);
}
}

The result of this program shows up as:
number is 4
I will increment number.
Now, number is 5
I will decrement number.
Now, number is 4

Process completed.

If PREfixed, the program spits out
number is 5
I will increment number.
Now, number is 6
I will decrement number.
Now, number is 5

Process completed.

Advertisements

1 Comment so far
Leave a comment

Great site…keep up the good work. 🙂 I read a lot of blogs on a daily basis and for the most part, people lack substance but, I just wanted to make a quick comment to say I’m glad I found your blog. Thanks, 🙂

A definite great read.. ..

-Bill-Bartmann

Comment by Bill Bartmann




Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s



%d bloggers like this: