eHow launches Android app: Get the best of eHow on the go.

How To

How to write an if statement in Java

Member
By Steph D
User-Submitted Article
(0 Ratings)

So you want to learn how to write an if statement for Java. Great! If statements are one of the basic building blocks for the Java programming language. This guide will quickly show you what you need to know to get started.

Difficulty: Moderate
Instructions

Things You'll Need:

  • A computer with Java set up on it
  1. Step 1

    Think about the condition or expression you are trying to check. Often it helps to think about this in a sentence form.

    For example:
    If it is raining outside, then I will wear a rain jacket. In this case, the condition we are trying to check is if it is raining outside.

    Conditions must have a boolean value - meaning they either are true or false.

  2. Step 2

    Now think about the result or what you want to happen if the conditional (or expression) you thought about above is true.
    In the example above that would be wearing a rain jacket.

  3. Step 3

    Put this in a form that java can understand:

    if (expression)
    resulting-statement;

    In english, this says, if the expression is true, then do the resulting statement.

  4. Step 4

    If you have multiple statements that you want to happen if the condition is true, then you can surround them with parenthesis.

    if (expression)
    {resulting-statement;
    resulting-statement2;
    }

    Notice that now we are grouping the statements with braces. So in this case, if the expression is true, then it would execute all the statements within the braces.

Tips & Warnings
  • You can use the ! operator to negate the expression it is checking
  • you can use the else keyword to ad an alternative statement to do if the expression is false.
  • Make sure you use braces to group statements. it's often easy to make mistakes by incorrect usage of the braces.
Subscribe

Post a Comment

Post a Comment

Related Ads

  • Have you done this? Click here to let us know.
I Did This
Get Free Computers Newsletters

Copyright © 1999-2009 eHow, Inc. Use of this web site constitutes acceptance of the eHow Terms of Use and Privacy Policy .   en-US Portions of this page are modifications based on work created and shared by Google and used according to terms described in the Creative Commons 3.0 Attribution License. † requires javascript

eHow Computers
eHow_eHow Technology and Electronics