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 5 of 5

Thread: The IF control statement

  1. #1
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default The IF control statement

    Hello,

    I'm new and a beginner and i have a question.
    Can an "if" control statement behave as a "for" that produces an interval of numbers from 0 to 9? With "for" you would write the following:

    for(int =0;i<10;i++)

    Could you also write simple code for the if statement in order to get the same interval?

    if(condition){
    // body} <-- this body does loop right? it only produces one instance of what i asked for if the condition is true(excluding the else ending)... right?

    Thank you.


  2. #2
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The IF control statement

    Welcome to the forum! Please read this topic to learn how to post code in code or highlight tags and other useful info for new members.

    There are fundamental tools common to most programming languages. One is the loop, another is the branching or program control statement. The for() statement is a loop, the if() is a branching or program control tool. An if() statement cannot be used to loop, because there is no repeating mechanism as there is built into a looping tool like for, do/while, and while.

    Rather than thinking of every "body" or program clause as looping - even if just once - think of it as executing. The clauses following for() and if() statements execute according to the preceding for() and if() statements.

  3. #3
    Junior Member
    Join Date
    Aug 2014
    Posts
    2
    Thanks
    0
    Thanked 0 Times in 0 Posts

    Default Re: The IF control statement

    Thx Greg ,
    1. so the loops are for() , while() , do() while() yes? These 3 in Java. if() and switch() are branching or executing tools.

    2. Can you tell me how you chose a loop or a branching tool when you have to slove a problem? Is there a trick?

    3. Can anyone explain is super plain words what a object is with its atributes and methods? I already know what it is but it help me cement stuff when its explained plainly by people who used them for a longer time.

    Thank you , i can learn a lot better by interacting with other people, not just books.

  4. #4
    Super Moderator
    Join Date
    Jun 2013
    Location
    So. Maryland, USA
    Posts
    5,517
    My Mood
    Mellow
    Thanks
    215
    Thanked 698 Times in 680 Posts

    Default Re: The IF control statement

    i can learn a lot better by interacting with other people, not just books.
    Whether obtained from a book or through discussions with other people, that's how one learns theory. Programming in any language is a skill. One cannot learn a skill by reading or talking (except the skills reading and talking). Can you learn to play the guitar by reading the Dummy's book on guitar playing without picking up a guitar? Of course not. Can you pick up a guitar and learn to play it without knowing ANY theory? Yes, many do and make a decent living at it.

    Getting started in Programming requires knowing some theory, but after that it's mostly practice, practice, practice, and more practice. Quit reading, quit talking with people about programming, and get programming.

    As for your questions, 1) Yes, 2) Any of the 3 can be used interchangeably (I'm sure you've read this), but there's a general guide: for() if the number of loops is known; while() if the number of loops is not known; and do()/while() if the number of loops is not known, but at least one trip through the loop is required, and 3) If you know what it is in theory, then you need to practice using it to really know it. Adding more words to what you already "know" won't help you in any way.

    Keep (start?) coding!

  5. #5
    Member jdv's Avatar
    Join Date
    Jul 2014
    Location
    This Land
    Posts
    73
    Thanks
    0
    Thanked 5 Times in 5 Posts

    Default Re: The IF control statement

    Quote Originally Posted by Kytres View Post
    3. Can anyone explain is super plain words what a object is with its atributes and methods? I already know what it is but it help me cement stuff when its explained plainly by people who used them for a longer time.
    Typically, an object is a black box that contains something you want. The box has no obvious ways to get inside it, but it has 0 or more buttons on it that you can press to get information about the contents, or the actual contents, or to put more things in the box.

    For example, consider a CandyBox class you see at the Dollar Class Mart. It is wrapped in plastic, but you can see from the picture on the wrapping that it has various buttons for finding out the type and number of various sweet candy it has inside. There are buttons for loading more candy, for dumping out all candy, for getting a specific candy, for displaying how many candies are inside, and so on.

    You buy the CandyBox class and take it home. You unwrap it and plug it into an electrical outlet (that is, you "instantiate" a candyBox object).

    (Because the manufacturers are not stupid, they have made the CandyBox a "singleton"; don't worry about this for now, but assume you can have only one candyBox object.)

    Now you see that you have a working candyBox (an object) that has various buttons and sliders and switches (methods). By pressing buttons, moving sliders, and hitting switches (invoking methods), sometimes you get candy (data), or information about the candyBox or the specific contents (properties). You can even pour your own candy into the candyBox and press a button to load it up with your favourite candy!

    Oops, but not _any_ candy! Because the candy company is a little evil, you have to use candy that is a type of the Candy made by World Domination Candy Corp (inherits or implements from com.domination.Candy). This requires buying from the same or different vendor some pre-created candy bags (objects) that you can load into the candyBox before pressing the "loadCandy" switch [invoke the loadCandy() method with a Candy parameter].

    Or you can buy the CandyFactory product from another store made by another company that allows you to make your own myTastyCandy (an object derived from your own MyTastyCandy class that conforms to the com.domination.Candy interface, probably) and use that in the same way.

    Oh, I should mention that World Domination Candy Corp workers know how to open up the box even though it is impossible for you. Inside are _more_ buttons and sliders and switches that they know how to use. These might do things like limit the number of candies, hide or disable some of the "public" buttons, or generally maintain specifics and details about the operation of a candyBox you, as the candyBox user (client) don't care about.

    Then the analogy breaks down quickly and I find I'm terribly hungry.

Similar Threads

  1. How to turn my If statement into a case/switch statement?
    By blobby404 in forum What's Wrong With My Code?
    Replies: 23
    Last Post: June 19th, 2014, 03:11 PM
  2. [SOLVED] A Loop statement and a switch statement issue
    By sternfox in forum Loops & Control Statements
    Replies: 13
    Last Post: March 7th, 2013, 04:19 PM
  3. timer control
    By amber_hell in forum What's Wrong With My Code?
    Replies: 1
    Last Post: February 26th, 2013, 07:16 AM
  4. Replacing an If statement with a Switch statement
    By logi in forum Loops & Control Statements
    Replies: 9
    Last Post: February 4th, 2013, 12:21 AM
  5. [SOLVED] DoublyLinkedList out of control!
    By javapenguin in forum What's Wrong With My Code?
    Replies: 3
    Last Post: October 13th, 2010, 07:06 AM