We're a community of 1076K IT Pros here for help, advice, solutions, professional growth and fun. Join us!
1,075,624 Members — Technology Publication meets Social Media
Username:
Password:
Lost login information?
Start New Discussion Reply to this Discussion

Seeking Understanding of "if"

Hi i came up with the following code for my Assignment.It works.What i dont understand was the following code, the rest of the code i did myself.

if ( value == 1 )
    smallest = randomNum;

Why do i need to assign value to 1?
Rest of my codes

System.out.println("How many integers shall we compare? (Enter a positive integer):");
            ii = input.nextInt();

            for (value = 1;value<=ii; value++)   
            {
                System.out.print("Enter value " + value + ":");
                randomNum = input.nextInt();  

            if ( value == 1 )
                smallest = randomNum;


            else if (randomNum < smallest)
                smallest = randomNum;           

            }
            System.out.println("The smallest number entered was: " + smallest);
            }

}

.

2
Contributors
2
Replies
16 Minutes
Discussion Span
1 Year Ago
Last Updated
3
Views
Question
Answered
Valiantangel
Junior Poster in Training
89 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

First, '==' operator is not an assignment operator. It is comparison. Assignment operator is '='.

Second, the if statement is used to set the first value as your smallest value since it has nothing to compare to, it will always be the smallest value in your random number.

Have fun! = )

wen_cai
Junior Poster in Training
64 posts since May 2012
Reputation Points: -3
Solved Threads: 17
Skill Endorsements: 2
Question Answered as of 1 Year Ago by wen_cai

ok got it:)

Valiantangel
Junior Poster in Training
89 posts since Jul 2010
Reputation Points: 10
Solved Threads: 0
Skill Endorsements: 0

This question has already been solved: Start a new discussion instead

Post: Markdown Syntax: Formatting Help
 
You
 
© 2013 DaniWeb® LLC
Page rendered in 0.0596 seconds using 2.67MB