The if statement has this form, where condition is true or false.
... // Do these statements before. if (condition) { ... // Do this clause if the condition is true. } ... // Do these statements after.
or
Example - EvaluateScore.java... // Do these statements before. if (condition) { ... // Do this clause if the condition is true } else { ... // Do this clause if the condition is false } ... // Do these statements after.
This displays one of two messages, depending on an input value.
// Description: Evaluate a test score. Illustrates if statement. // File : if/EvalScore.java // Author: Fred Swartz - 2007-04-09 - Placed in public domain. import javax.swing.*; public class EvalScore { public static void main(String[] args) {//... Input a score. String scoreStr = JOptionPane.showInputDialog(null, "Enter your score?"); int score = Integer.parseInt(scoreStr); //... Create a message. String comment; // Message to the user. if (score >= 60) { comment = "Not so bad"; } else { comment = "This is terrible"; } //... Output the message. JOptionPane.showMessageDialog(null, comment); } }
![]() |
| Flowchart If Statement |

No comments:
Post a Comment