//SqrtIfApplet.java //demo of if statement import javax.swing.*; public class SqrtIfApplet extends JApplet { public void init() { String input; double num, numSqrt; input = JOptionPane.showInputDialog( "Enter a number" ); num = Double.parseDouble( input ); if (num >= 0) { numSqrt = Math.sqrt(num); JOptionPane.showMessageDialog(null, "Square root = " + numSqrt ); } else JOptionPane.showMessageDialog(null, "No square root of negative number"); } } /* if statement makes the program branch, either the true branch or the false (else) branch is executed, not both and not neither, the other branch is skipped. Question is asked, decision is made, one branch is selected. Based on language/logic/real life: IF it's raining then I'll stay in, otherwise I'll go out. Syntax: if (booleanExpression) { statement(s) //of true branch } else { statement(s) //of false branch } If more than one statement, must be in {} (If only one statement, braces are not required.) Without braces, only single following statement is part of the if or else part, causing either syntax error or bug. else part is optional. Can not have else without matching if. Notice NO ; on if line! booleanExpression is an expression that evaluates to true or false. Exs: num<=0 num==0 num!=0 num>10 num1==num2 num1>=num2 */