conditionals - Conditionals Why a Conditional? As we write...

Info iconThis preview shows pages 1–3. Sign up to view the full content.

View Full Document Right Arrow Icon
Conditionals Why a Conditional? As we write more complicated programs, we don’t always want them to do the same thing in all cases. We might want them to do one thing in one situation, and another thing in a different one. For example, a computer game does one thing while the game is in progress, but behaves differently when the game is over. If-Statements An if-statement allows us to do something only if some condition is true. There are also variants on if-statements that allow us to do one thing in one case, another thing in another case, etc. Simple If-Statement Here is the syntax for a simple if-statement: if (condition) { //statements } Here, condition is either a boolean variable or an expression that evaluates to true or false. The statements inside the if-statement are things that we ONLY want to happen if our condition is true. Finally, the brackets {} around the body are optional if there is only one statement inside. For example, suppose we want to get the user’s name and age as input. In any case, we want to then print out a greeting. We also want to print “You are an adult” if the user is 18 or older. Here is the full program: import java.util.*; public class HelloAdult { public static void main(String[] args) { Scanner s = new Scanner(System.in); System.out.print(“Enter your name: “); String name = s.nextLine(); System.out.print(“Enter your age: “); int age = Integer.parseInt(s.nextLine()); System.out.println(“Hello, “ + name); if (age >= 18) { System.out.println(“You are an adult”); } } }
Background image of page 1

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
When the program runs, it will ask the user for their name and age and read those values into the name and age variables. It will then print Hello and the person’s name. Finally, if the person is 18 or older, the program will print “You are an adult”. If the user is under 18, the program will not print anything else. If/Else-Statement Here is the syntax for an if/else-statement. This allows us to do one thing if something is true, and a different thing if it is not true: if (condition) { //statements } else { //statements } Here, we will check the condition and execute the first set of statement if the condition is true, and the second set of statements if the condition is not true. In any case, exactly one set of statements is executed. For example, suppose we want to print out whether a number is even or odd.
Background image of page 2
Image of page 3
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 03/02/2010 for the course CIS 200 taught by Professor Staff during the Spring '08 term at Kansas State University.

Page1 / 7

conditionals - Conditionals Why a Conditional? As we write...

This preview shows document pages 1 - 3. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online