if/else Statement

The if statement encloses some code which is executed only if a condition is true. The general syntax of the if statement is:
if (condition) { body-code }
The if statement has two parts. The condition is a boolean expression which resolves to either true or false. The body-code is code that will be executed only if the condition is true. Here is an example of the if statement.
// Returns 0 if s is null public int getLength(String s) { if (s == null) { // This line of code is execute only if s is null return 0; } return s.length(); }
The if statement also has an optional else part which encloses some code that is executed only if the condition is false. Here is the general syntax of the if statement with the optional else part:
if (condition) { body-code } else { else-code }
The else-code is executed only if condition resolves to false. Here is an example of the if statement with the optional else part.
// Generates a random number and prints whether it is even or odd if ((int)(Math.random()*100)%2 == 0) { System.out.println("The number is even"); } else { System.out.println("The number is odd"); }
The else-code part can also be another if statement. An if statement written this way sets up a sequence of condition's, each tested one after the other in top-down order. If one of the conditions is found to be true, the body-code for that if statement is executed. Once executed, no other conditions are tested and execution continues after the sequence. Here is an example of a sequence of if/else/if statements
// Converts the time, which is in milliseconds, into a // more readable format with higher units public String getTimeUnit(long time) { String unit = "ms"; if (time >= 365*24*60*60*1000L) { unit = "years"; time /= 365*24*60*60*1000L; } else if (time >= 24*60*60*1000) { unit = "days"; time /= 24*60*60*1000; } else if (time >= 60*60*1000) { unit = "hr"; time /= 60*60*1000; } else if (time >= 60*1000) { unit = "min"; time /= 60*1000; } else if (time >= 1000) { unit = "sec"; time /= 1000; } // Execution continues here if any of the conditions match return time + " " + unit; }
Here's some sample output:
getTimeUnit(123); // 123 ms getTimeUnit(1234)); // 1 sec getTimeUnit(12345)); // 12 sec getTimeUnit(123456)); // 2 min getTimeUnit(1234567)); // 20 min getTimeUnit(12345678)); // 3 hr getTimeUnit(123456789)); // 1 days getTimeUnit(1234567890)); // 14 days getTimeUnit(12345678900L)); // 142 days getTimeUnit(123456789000L)); // 3 years

Comments

21 Feb 2010 - 8:46pm by joi (not verified)

i wish u could help me with my situational problem:
i want you to create a program that
will accept string character only if
numeric the program will prompt us
that invalid data or input something
like that...

1 Mar 2010 - 6:37pm by DWilly92 (not verified)

Im not the poster of this article, but if u explain in more detail maybe i can help you, my email is: graphics {at} letsgetdigital <(d0t)> ca
Sorry about the @ and . but its to avoid spambots, just give me a few more details on what you need and i should be able to help you.

4 Mar 2010 - 12:56pm by Anonymous (not verified)

mejora evidente de las páginas de ayuda

7 Apr 2010 - 7:23am by Anonymous (not verified)

write a program that read an employee name, his total salary , and then calculates his tax,(10%of his total salary of the salary is equal than 1000,15%if more),and prints the result in a tabular

13 Apr 2010 - 11:34am by evance Makopa (not verified)

thas great staff technology is doing with java.

14 May 2010 - 11:47am by dipesh p. chonka (not verified)

Write a program to find the negative positive number and convert into absolute number.
Commands to be used
1. IfElseNested

18 Jun 2010 - 11:13am by Anonymous (not verified)

how to print two values using if-else statements

30 Jul 2010 - 4:29am by rai (not verified)

import java.io.*;

public class Main{
public static void main(String [] args){
BufferedReader getIn=new BufferedReader(new InputStreamReader(System.in));

String a;
int num1=0;
int num2=0;
String ans="";
System.out.println("enter operator(only + and -)");
a=getIn.readLine ();

if(ans == num1 ){

num1 = num1++;

}
else{
num2 =num2--;
}
}
}

what is the wrong of my codes???
sopposed to be the result of that is when i choose + sign it will increment and if - it will decrement..

9 Aug 2010 - 7:38pm by Anonymous (not verified)

it'z so hard subject grabe.....

3 Sep 2010 - 1:53am by Anonymous (not verified)

Women'sugg boots,anxiety coolugg classic,especially during autumnugg boots sale,and winter acclimateugg boots uk,does not assure anxietyugg boots cheap,not aloneugg classic bootsugg classic tall,accessible totall ugg boots,uggs boots,ugg classic short,put the bottom freezing,easier affectingugg classic tall boots,womens ugg boots,leather ugg boots,ugg boots on sale,women's health, fall/winterdiscount ugg boots,ugg boots online, ugg boats,ugg boots outlet,division baddest augg classic short sand,ugg classic cardy,brace of balmyugg classic cardy boots,clothes andugg cardy, is actual important,a acclaimed Ucardy ugg boots,is autumn and winterugg boots for girls,ugg boots uk cheap,ugg cardy boots,knitted ugg boots snow boots in against balmy ,ugg boots london,ugg bailey button,style,the winter a bifold ensures anxietyugg bailey button bootsugg boots sale 2010,ugg classic tall chestnut,adequate and warm,UGG boots ugg classic tall black,ugg classic tall chocolate,ugg classic tall bomber boot, in autumn 2010 new snow is added than in the accomplished appearance with snow UGG Classic Cardy Boots easier!

Post a comment

More information about formatting options

CAPTCHA
This question is for testing whether you are a human visitor and to prevent automated spam submissions.
Image CAPTCHA
Enter the characters shown in the image. Ignore spaces and be careful about upper and lower case.