If Else-If statement Example

  1. /*
  2.   If Else-If statement Example
  3.   This Java Example shows how to use if else-if statement in Java program.
  4. */
  5.  
  6. public class IfElseIfElseExample {
  7.  
  8. public static void main(String[] args) {
  9.  
  10. /*
  11.   * If Else-if statement is used to execute multiple of actions based upon
  12.   * multiple conditions.
  13.   * Sysntax of If Else-If statement is
  14.   *
  15.   * if(<condition1>)
  16.   * statement1
  17.   * else if(<condition2>)
  18.   * statement2
  19.   * ..
  20.   * else
  21.   * statement3
  22.   *
  23.   * If <condition1> is true, statement1 will be executed, else if <condition2>
  24.   * is true statement2 is executed and so on. If no condition is true, then else
  25.   * statement will be executed.
  26.   */
  27.  
  28. int i = 10;
  29.  
  30. if(i > 100)
  31. System.out.println("i is grater than 100");
  32. else if(i > 50)
  33. System.out.println("i is grater than 50");
  34. else
  35. System.out.println("i is less than 50");
  36. }
  37. }
  38.  
  39. /*
  40. Output would be
  41. i is less than 50
  42. */

Get a number as input from the user

Get a number as input from the user, and output the equivalent of the number in words. The number inputted should range from 1-10. If the user inputs a number that is not in the range, output, "Invalid number".

Use an if-else statement to solve this problem

Use a switch statement to solve this problem

ty

Post new comment

To combat spam, please enter the code in the image.


Suggested Reading

Oracle Magazine
Contains technology strategy articles, sample code, tips, Oracle and partner news, how to articles for developers and DBAs, and more.
Cost: FREE

View/Subscribe
NASA Tech Briefs
Features exclusive reports of innovations developed by NASA and its industry partners/contractors that can be applied to develop new/improved products and solve engineering or manufacturing problems.
Cost: FREE

View/Subscribe
FierceBiotech IT
Is a free, easy to read weekly email service that brings must read biotech IT news to senior biotech, pharma, and IT executives.
Cost: FREE

View/Subscribe
Simply SQL
Simply SQL is a practical step-by-step guide to writing SQL.
Cost: FREE

View/Subscribe
Simply JavaScript
Packed with full-color examples, Simply JavaScript is all you need to start programming in JavaScript the right way.
Cost: FREE

View/Subscribe
PCMag.com's What's New Now
Lance Ulanoff, Editor in Chief of the PC Magazine Network, brings you this twice-weekly roundup of the latest top tech stories, the best new product reviews, plus special offers from Ziff Davis and its partners.
Cost: FREE

View/Subscribe

Could not find what you are looking for? Search Java Examples




Feel Tired? Read Jokes & Inspirational Stories, Play Games!