Java/Language Basics/If
If Else
<source lang="java">
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt. public class IfElse {
static int test(int testval, int target) { int result = 0; if(testval > target) result = +1; else if(testval < target) result = -1; else result = 0; // Match return result; } public static void main(String[] args) { System.out.println(test(10, 5)); System.out.println(test(5, 10)); System.out.println(test(5, 5)); }
} ///:~
</source>
if Else 2
<source lang="java">
// From "Thinking in Java, 3rd ed." (c) Bruce Eckel 2002 // www.BruceEckel.ru. See copyright notice in CopyRight.txt.
public class IfElse2 {
static int test(int testval, int target) { if(testval > target) return +1; else if(testval < target) return -1; else return 0; // Match } public static void main(String[] args) { System.out.println(test(10, 5)); System.out.println(test(5, 10)); System.out.println(test(5, 5)); }
} ///:~
</source>
if Else Demo
<source lang="java">
/* From http://java.sun.ru/docs/books/tutorial/index.html */ /*
* Copyright (c) 2006 Sun Microsystems, Inc. All Rights Reserved. * * Redistribution and use in source and binary forms, with or without * modification, are permitted provided that the following conditions are met: * * -Redistribution of source code must retain the above copyright notice, this * list of conditions and the following disclaimer. * * -Redistribution in binary form must reproduce the above copyright notice, * this list of conditions and the following disclaimer in the documentation * and/or other materials provided with the distribution. * * Neither the name of Sun Microsystems, Inc. or the names of contributors may * be used to endorse or promote products derived from this software without * specific prior written permission. * * This software is provided "AS IS," without a warranty of any kind. ALL * EXPRESS OR IMPLIED CONDITIONS, REPRESENTATIONS AND WARRANTIES, INCLUDING * ANY IMPLIED WARRANTY OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE * OR NON-INFRINGEMENT, ARE HEREBY EXCLUDED. SUN MIDROSYSTEMS, INC. ("SUN") * AND ITS LICENSORS SHALL NOT BE LIABLE FOR ANY DAMAGES SUFFERED BY LICENSEE * AS A RESULT OF USING, MODIFYING OR DISTRIBUTING THIS SOFTWARE OR ITS * DERIVATIVES. IN NO EVENT WILL SUN OR ITS LICENSORS BE LIABLE FOR ANY LOST * REVENUE, PROFIT OR DATA, OR FOR DIRECT, INDIRECT, SPECIAL, CONSEQUENTIAL, * INCIDENTAL OR PUNITIVE DAMAGES, HOWEVER CAUSED AND REGARDLESS OF THE THEORY * OF LIABILITY, ARISING OUT OF THE USE OF OR INABILITY TO USE THIS SOFTWARE, * EVEN IF SUN HAS BEEN ADVISED OF THE POSSIBILITY OF SUCH DAMAGES. * * You acknowledge that this software is not designed, licensed or intended * for use in the design, construction, operation or maintenance of any * nuclear facility. */
public class IfElseDemo {
public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = "A"; } else if (testscore >= 80) { grade = "B"; } else if (testscore >= 70) { grade = "C"; } else if (testscore >= 60) { grade = "D"; } else { grade = "F"; } System.out.println("Grade = " + grade); }
}
</source>
if statement
<source lang="java">
public class Retirement {
public static void main(String[] args) { int goal = 100; int payment = 500; if (goal < payment) { System.out.println("Happy."); } }
}
</source>
if statement demo
<source lang="java">
/*
* Copyright (c) 2000 David Flanagan. All rights reserved. * This code is from the book Java Examples in a Nutshell, 2nd Edition. * It is provided AS-IS, WITHOUT ANY WARRANTY either expressed or implied. * You may study, use, and modify it for any non-commercial purpose. * You may distribute it non-commercially as long as you retain this notice. * For a commercial use license, or to purchase the book (recommended), * visit http://www.davidflanagan.ru/javaexamples2. */
/**
* This program plays the game "Fizzbuzz". It counts to 100, replacing each * multiple of 5 with the word "fizz", each multiple of 7 with the word "buzz", * and each multiple of both with the word "fizzbuzz". It uses the modulo * operator (%) to determine if a number is divisible by another. */
public class FizzBuzz { // Everything in Java is a class
public static void main(String[] args) { // Every program must have main() for (int i = 1; i <= 100; i++) { // count from 1 to 100 if (((i % 5) == 0) && ((i % 7) == 0)) // Is it a multiple of 5 & 7? System.out.print("fizzbuzz"); else if ((i % 5) == 0) // Is it a multiple of 5? System.out.print("fizz"); else if ((i % 7) == 0) // Is it a multiple of 7? System.out.print("buzz"); else System.out.print(i); // Not a multiple of 5 or 7 System.out.print(" "); } System.out.println(); }
}
</source>