1
2
3
4
5
|
public class HelloWorld{
public static void main(String[] args) {
System.out.println( "Hello, World!" );
}
}
|
That wasn't very interesting! Let's try writing an if statement. Also known as conditional statements, if statements control which path your program executes.
1
2
3
4
5
6
7
8
9
|
public class LargerVal{
public static void main(String[] args) {
if ( 38 * 278 > 78 * 78 )
System.out.println( 38 * 278 );
else
System.out.println( 78 * 78 );
}
}
|
Try running the program yourself. If 38*278 is larger than 78*78, whatever 38*278′s result is will be printed out. Else if 78*78 is larger than 38*278, you will see 78*78′s value in the console.
Now that you know some basics, try solving these problems:
1) Print the biggest product out of 3*4, 2*7, and 1*10 using if statements.
2) A number is "cold" if it's less than 32. Write a program that prints "BRRR" if a number is "cold".
Feel free to leave any questions in the comments below!