Header Banner
Null Byte Logo
Null Byte
wonderhowto.mark.png
Cyber Weapons Lab Forum Metasploit Basics Facebook Hacks Password Cracking Top Wi-Fi Adapters Wi-Fi Hacking Linux Basics Mr. Robot Hacks Hack Like a Pro Forensics Recon Social Engineering Networking Basics Antivirus Evasion Spy Tactics MitM Advice from a Hacker

How to Java: E3 (If/Else Statements)

Jan 31, 2016 05:43 AM
Feb 1, 2016 02:22 PM
Java programming language logo

Hello Java-ers,

In the previous two tutorials, we learned about installing the JDK, Installing/CreatingAProject in Eclipse IDE, and data types in Java.

In this tutorial, we will be learning about if/else statements.

--------------------------------------

In Java, an if statement can be interpretted as

if(This condition is true){

doSomething();

}else if(another condition is true){

doSomethingElse();

}else{

otherConditionsAreFalseDoThisOtherThing();

}

So lets write our first if/else statement:

(Assuming that you (the reader) have already created a Java project and file for this)

lets make a program that checks to see if an int variable equals the number "5":

public class numCheck{

public static void main(String[] args){

int num = 12;

if(num == 5){

System.out.println("Num equals 5");

}else{

System.out.println("Num does not equal 5");

}

}

}

(If you prefer organized code, press Ctrl+Shift+O (Windows), Cmd+Shift+O (OSX), Ctrl+Shift+O (Linux))

You can find the code for this on Github HERE

-----------------

You don't need the brackets (its not required, but adding them doesn't harm anything) after the if/else statement, only if the doSomething() statements are only one line, with just one semi-colon at the end.

Its only when you have multi-lined statements within an if statement, you Must have brackets.

You already know how to use your phone. With Gadget Hacks' newsletter, we'll show you how to master it. Each week, we explore features, hidden tools, and advanced settings that give you more control over iOS and Android than most users even know exists.

Sign up for Gadget Hacks Weekly and start unlocking your phone's full potential.

Related Articles

Comments

No Comments Exist

Be the first, drop a comment!