Welcome to the Java Programming Forums


The professional, friendly Java community. 21,500 members and growing!


The Java Programming Forums are a community of Java programmers from all around the World. Our members have a wide range of skills and they all have one thing in common: A passion to learn and code Java. We invite beginner Java programmers right through to Java professionals to post here and share your knowledge. Become a part of the community, help others, expand your knowledge of Java and enjoy talking with like minded people. Registration is quick and best of all free. We look forward to meeting you.


>> REGISTER NOW TO START POSTING


Members have full access to the forums. Advertisements are removed for registered users.

Results 1 to 4 of 4

Thread: Increment operator

  1. #1
    Junior Member
    Join Date
    Sep 2021
    Location
    Ireland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Increment operator

    public class Stack1 {
     
    	int stack_elements[] = new int[10];
    	int top_element;
     
    	Stack1() {
    		top_element = -1;
    	}
     
    	void push (int i){
    		if (top_element == 9) 
    			System.out.println("Stack is full");
    		else
    			stack_elements[++top_element] = i; (Please tell me why if I write top_element + 1 won't give me the same result as ++top_element)
    	}
     
    	int pop() {
    		if (top_element < 0) {
    			System.out.println("Stack is empty");
    			return 2;
    		}
    		else 
    			return stack_elements[top_element--];
     
    	}
     
    }
    Last edited by Norm; September 23rd, 2021 at 05:59 AM. Reason: Added code tags

  2. #2
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Increment operator

    Do you have a question about the posted code?

    I don't like to look at unformatted code.

    Please edit your post and wrap your code with code tags:

    [code]
    **YOUR CODE GOES HERE**
    [/code]

    to get highlighting and preserve formatting.

    The expression: variable++ // changes the value of variable
    The expression: variable + 1 // does NOT change the value of variable
    The statement: variable = variable + 1; // changes the value of variable
    If you don't understand my answer, don't ignore it, ask a question.

  3. #3
    Junior Member
    Join Date
    Sep 2021
    Location
    Ireland
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Post Re: Increment operator

     
    public class Stack1 {
    int stack_elements[] = new int[10];
    int top_element;
    Stack1() {
    top_element = -1;
    }
    void push (int i){
    if (top_element == 9)
    System.out.println("Stack is full");
    else
    stack_elements[++top_element] = i; 
    }
    int pop() {
    if (top_element < 0) {
    System.out.println("Stack is empty");
    return 0;
    }
    else
    return stack_elements[top_element--];
    }
    }

    The code is working fine, but I have doubt about increment operator, which is as follows.
    In line 11 of the java code, if I write top_element + 1, why is the result is not same as ++top_element? Usually if I write ++top_element it is same as top_element + 1. Here the code works only if I use ++top_element.

  4. #4
    Super Moderator Norm's Avatar
    Join Date
    May 2010
    Location
    Eastern Florida
    Posts
    25,042
    Thanks
    63
    Thanked 2,708 Times in 2,658 Posts

    Default Re: Increment operator

    if I write top_element + 1, why is the result is not same as ++top_element?
    Yes those two expressions do not give the same result:
    top_element + 1 does NOT change the value in top_element
    ++ top_element does change the value in top_element


    What happened to the indentations for the posted code? Logically nested statements should have indentations.

    If you want to use top_element + 1 Try this:
        stack_elements[top_element = top_element + 1]  // change value and use results
    If you don't understand my answer, don't ignore it, ask a question.

Similar Threads

  1. Urgent! - How To Increment Value
    By stevenroberts in forum What's Wrong With My Code?
    Replies: 4
    Last Post: May 7th, 2014, 05:18 AM
  2. post/pre increment operator speed
    By tayloralina in forum Loops & Control Statements
    Replies: 3
    Last Post: December 17th, 2012, 02:40 PM
  3. post increment
    By deependeroracle in forum Java Theory & Questions
    Replies: 1
    Last Post: November 12th, 2012, 11:31 AM
  4. boolean for increment method
    By daitobu in forum Object Oriented Programming
    Replies: 6
    Last Post: August 28th, 2012, 09:09 PM
  5. Increment a date
    By colerelm in forum Java Theory & Questions
    Replies: 1
    Last Post: September 30th, 2011, 05:57 AM