Custom Search

Friday, May 20, 2011

Intro to if else statements

In this post we are covering the if else statement.

public class Main{
public static void main(String[] args){
int x = Math.random()*20;
if(x ==1){
System.out.println("X = 1");
}else{System.out.println(x);

}
}
}

In this code we are generating a random number(x) and writing a simple if else statement saying, if x = 1 print x =1. Else(if x is anything but 1) print the value of x.   We need the two = signs so the computer knows you are comparing and checking the value of x and not assigning the value of x to 1. If you have any questions or ideas for future posts feel free to leave them below! The purpose of an if else statement is so if something is true do this, but we dont want to write a bunch of if statements for each posibility so we use the else statement to catch all other posibilities.

Well, that wraps it up for this post, see you in the next!

No comments:

Post a Comment