• Post Reply Bookmark Topic Watch Topic
  • New Topic

if and while statements and meanings.  RSS feed

 
Wilson Oluwasegun
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Hello, I'm new to java and still don't understand the true functions of if and while statements and why the greater than sign(>) and less than signs(<) are introduced as well as this types : beerNum = beeNum - 1;
i'm so eager to understand java. Would really appreciate your help.
 
Henry Wong
author
Sheriff
Posts: 23295
125
C++ Chrome Eclipse IDE Firefox Browser Java jQuery Linux VI Editor Windows
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wilson Oluwasegun wrote:Hello, I'm new to java and still don't understand the true functions of if and while statements and why the greater than sign(>) and less than signs(<) are introduced as well as this types : beerNum = beeNum - 1;
i'm so eager to understand java. Would really appreciate your help.



One possible place to start the learning, is with the Oracle Java tutorials....

http://docs.oracle.com/javase/tutorial/

Henry
 
William P O'Sullivan
Ranch Hand
Posts: 859
Chrome IBM DB2 Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Java can most definitely be used to learn software programming.

In your case, I would strongly advise learning the basic concepts first.

If you really do not understand what "if" means, or what the greater than or less than signs mean,
then you have to start at the very beginning.

http://www.dummies.com/store/product/Beginning-Programming-For-Dummies-4th-Edition.productCd-0470088702.html

WP
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wilson Oluwasegun wrote:Hello, I'm new to java and still don't understand the true functions of if and while statements and why the greater than sign(>) and less than signs(<) are introduced as well as this types : beerNum = beeNum - 1;
i'm so eager to understand java. Would really appreciate your help.


believe it or not, an if statement in java is just like and English if-statement. for example, if i said:



that's pretty obvious what to do, right? Java works the same way



so this says "If the nightLight.isOn variable is equal to true, call the nightLight.turnOff() method. Else, call the nightLight.turnOn() method".
 
Jeff Verdegan
Bartender
Posts: 6109
6
Android IntelliJ IDE Java
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator


@Wilson: I don't know how good that particular Dummies book is, but I wouldn't expect it to give you any more than the very basic fundamentals of programming in general (and maybe it has some Java-specific examples, I don't know). If you just want to learn what those things mean in Java, then Oracle's Java tutorial is fine.
 
H Biz
Greenhorn
Posts: 4
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
Wilson Oluwasegun wrote:Hello, I'm new to java and still don't understand the true functions of if and while statements and why the greater than sign(>) and less than signs(<) are introduced as well as this types : beerNum = beeNum - 1;
i'm so eager to understand java. Would really appreciate your help.


If statements are just like real life if statements.

If somethings is <insert outcome: true/false/higher/lower>
then do X
else do Y

So a real world example:

If day is a week day
then: go to work
else: stay at home

If phone rings
then: answer call
else: leave alone

> greater than
and
< less than
signs, are used to compare items - especially useful in if conditions.

if chocolate bar price is less than change in my pocket
then: buy
else; dont buy

if chocolateBarPrice < pocketChange
then: buy
else: null - i think thats correct


A while loop is used when something needs to continually happen until a condition is met to stop/break the loop.

For example:

While bank balance is greater than $50
buy beer
else: go home

becomes

While bankBalance > 50
buy beer
bank balance - 5
else: go home

Im a noob to so just read up on what I say - I may be wrong too
 
fred rosenberger
lowercase baba
Bartender
Posts: 12565
49
Chrome Java Linux
  • Mark post as helpful
  • send pies
  • Quote
  • Report post to moderator
H Biz wrote:

if chocolateBarPrice < pocketChange
then: buy
else: null - i think thats correct

not sure what you mean by "else: null". Java does not require you to have an else, so you could just do this:



H Biz wrote:
For example:

While bank balance is greater than $50
buy beer
else: go home

becomes

While bankBalance > 50
buy beer
bank balance - 5
else: go home

"else" statements don't work with "while" statements. so this should really be more like


 
  • Post Reply Bookmark Topic Watch Topic
  • New Topic
Boost this thread!