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.

View RSS Feed

JD1's Personal Development Blog

The If Statement

Rate this Entry
Quote Originally Posted by OA-OD-JD1 View Post
From prior programming experience, I'm well aware of what an If Statement is. I'm just going to quickly go over its syntax so I don't forget!

Single Line If Statement Without Else Clause

if(condition)
   instructions if condition proves true (confined to one line of instructions)...

Single Line If Statement With Else Clause

if(condition)
   instructions if condition proves true (confined to one line of instructions)...
else
   instructions if condition proves false (confined to one line of instructions)...

Multiple Line If Statement Without Else Clause

if(condition){
   instructions if condition proves true...
   instructions if condition proves true...
}

Multiple Line If Statement With Multiple Line Else Clause

if(condition){
   instructions if condition proves true...
   instructions if condition proves true...
}else{
   instructions if condition proves false...
   instructions if condition proves false...
}

Else If Statement

if(condition){
   instructions if condition proves true...
}else if(condition){
   instructions if condition proves true...
}else{
   instructions if condition proves false...
}

Java Example

if(age<13){
   System.out.println("You are not yet a teenager!");
   System.out.println("You are most definitely 12 or younger!");
}else{
   System.out.println("You might be a teenager!");
   System.out.println("You are definitely older than 12!");
}

Important Note

Even though it isn't vital to include braces around single line If Statements, it's still advised. This is because, for example, if you wanted to go back and add code to a particular statement, you will have to then add the braces. Often people can overlook this and it's just a bug waiting to happen. This is the case for all statements, not just If Statements.
Categories
Uncategorized

Comments