Download presentation
Presentation is loading. Please wait.
Published byHilary Francis Modified over 9 years ago
1
Week #2 Java Programming
2
Enable Line Numbering in Eclipse Open Eclipse, then go to: Window -> Preferences -> General -> Editors -> Text Editors -> Check “Show Line Numbers”
3
Simple Output For printing words (or Strings) onto the screen, we use this command: System.out.println(“Anything you want printed here”); If we want to print something that contains a variable, we would use this command: String mySentence = “Hey there.”; System.out.println(mySentence);
4
Simple Output String commands: \”This represents a quotation mark \’This represents an apostrophe \nThis represents a new line \\This represents a backslash \tThis represents a tab \sThis represents a space
5
Simple Output Another way to print out text onto your screen: This is used when you have a lot of variables to be included in the print statement, example: String name = “Vincent Tsang”; int age = 60; String school = “Binghamton University”; System.out.format(“My name is %s, I am %s years old, I go to %s.”, name, age, school);
6
Simple Output Printing out variables using the System.out.println(“”); format: String name = “Vincent Tsang”; int age = 60; String school = “Binghamton University”; System.out.println(“My name is ” + name + “. I am “ + age + “years old, I go to “ + school + “.”);
7
If/Else Statements What is an if/else statement? Basically, in programming, we will often have different ways to approach things depending on the values given to it. We tell the program that “If something occurs, do this. If something else occurs, do something else. Else do something else” T his will be clearer when we write out the code
8
If/Else Statements The command for the if/else statement is in this format: if(comparison) { Something happens here; } else { Something else happens here; }
9
If/Else Statements You are allowed to have more than one if statement: if(comparison) { Something happens here; } if(comparison2) { Something else happens here; } else { Something else happens here; }
10
If/Else Statements Nested If/Else Statements: if(comparison) { if(comparison2) {// Basically the same as && symbol Something happens here; } } else { Something else happens here; }
11
If/Else Statements Logical Operators || represents “or” This can be found above your enter key (it’s located with the backslash) && represents “and” This can be found with the 7 key Switch Statements This is used when you have a lot of things to compare and do different type of commands This is to save time so that you don’t have to type out a billion if statements
12
If/Else Statements In a switch statement, we need a variable to compare its value with: Example: int month;// This is our integer variable switch(month) { case 1: System.out.println(“January”); break; // We will always need to break out of the statement case 2: System.out.println(“February”); break; } Do Exercise 1 (Age.java)
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.