Upload
others
View
33
Download
0
Embed Size (px)
Citation preview
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Variables, Types, Conditionals, Loops and Arrays.
Jemar Jones
Department of Computing and SoftwareMcMaster University
Tutorial 2
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Outline1 Variables and Types
Variable DeclarationVariable Assignment
2 ConditionalsThe if statementThe conditionOthers
3 Excersize 14 Loops
The While LoopThe For Loop
5 ArraysArray DeclarationAccesing an arrayIteration
6 Excersize 2Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Variable DeclarationVariable Assignment
Variable Declaration
int︸︷︷︸type
answer︸ ︷︷ ︸name
= 42︸︷︷︸value
;
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Variable DeclarationVariable Assignment
Primitive Types
Type Description Operators
byte Whole numbers from -128 to 127 + - * /
short Whole numbers from -32,768 to 32,767 + - * /
int Whole numbers from −231 to 231 - 1 + - * /
long Whole numbers from −263 to 263 - 1 + - * /
float Floating point numbers (decimals) + - * /
double Floating point numbers with greater range + - * /
boolean true or false value and, or, not
char Single characters compare
*String Sequences of characters concatenation
*Technically String is not a Java primitive.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Variable DeclarationVariable Assignment
Variable names
Case sensitive
Must start with a letter, dollar sign, or underscore.Subsequent characters may be letters, digits, dollar signs, orunderscores.
Can’t be certain reserved words.
Should be meaningful and clear.
Convention is to use camel casing (ie. myVar).
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Variable DeclarationVariable Assignment
Variable Assignment
Quick Example:
int myNum = 10; //Declaration and initialization
myNum = 3; //Overwriting via assignment
int myNum2; //Declaration with no initialization
myNum2 = (myNum * 5) - (4/2) + 10; //initialization after
Declaration
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The if statementThe conditionOthers
The if statement
The if statement is one way of controlling the flow of your program.
if (condition){
//Some statements
}else if (condition2){
//Some alternative statements
//As many else if’s as you want...
}else{
//Some other alternative statements
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The if statementThe conditionOthers
The condition
The condition must evaluate to a boolean value.
This means the condition must ultimately be either true orfalse
Typically this means the condition will be made up ofrelational and boolean operators.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The if statementThe conditionOthers
Relational Operators
Syntax Description
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
*Note that the equality operator ”==” is different from theassignment operator ”=”
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The if statementThe conditionOthers
Boolean Operators
Syntax Description
&& Logical AND
|| Logical OR
! Logical NOT
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The if statementThe conditionOthers
Some possible conditions
true
1 == 1
!{1 == 1}
a > 5
(a > 5) !! (b != a)
(answer == 7*6 && name == "Arthur") || p >= 9001
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The if statementThe conditionOthers
...
if ((answer == 7*6 && name == "Arthur") || p >= 9001){
System.out.println("Some sort of cultural reference");
}else{
System.out.println("Something uninteresting");
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The if statementThe conditionOthers
Others
Shorter form:
if (condition)
statement;
Ternary operator for very common case:
a = condition? firstOption : secondOption;
//This is the same as:
if (condition){
a = firstOption;
}else{
a = secondOption;
}Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Try it!
Write a program that takes in a usercode and a passcode (*asint’s) and checks them against some hard coded values. If thecredentials are correct, indicate that the user now has access. Ifonly the usercode is correct, indicate that the user is an imposter.Otherwise, simply indicate that access to the system is declined.
*Hint: To turn a String into an int use: Integer.parseInt(myStr)
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
public static void main(String[] args) {
int usercode = Integer.parseInt(args[0]);
int passcode = Integer.parseInt(args[1]);
if (usercode == 123 && passcode == 456){
System.out.println("Welcome.");
}else if (usercode == 123){
System.out.println("You’re an imposter..");
}else{
System.out.println("You don’t have access, sorry.");
}
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
Loops
A loop is used to repeat some sequence of statement(s) accordingto some condition.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
The While Loop
while (condition){
//Some statement(s)
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
What’s going on here:
1 Evaluate boolean condition
2 If the condition is true, run some statements.
3 Jump back to 1.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
Quick Example:
int n = 0;
while (n < 6){
System.out.println(n);
n = n + 1;
}
System.out.println("Final n value: " + n);
Output:
0
1
2
3
4
5
Final n value: 6Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
The infinite loop
while (true){
System.out.println("Still running");
}
This loop doesnt terminate!It will run either forever, or more likely until it causes a crash.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
The For Loop
for (statement; condition; statement){
//Some statement(s)
}
Almost universally you will see the following structure:
for (init; condition; increment){
//Some statement(s)
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
What’s going on here:
1 Run initialization statement
2 Check boolean condition. If it’s false jump out of for loop.
3 Run increment statement.
4 Run statement(s) in the loops body.
5 Jump back up to step 2
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The While LoopThe For Loop
For example:
for (int i = 0; i < 10; i++){
System.out.println(i);
}
Or with a different increment:
for (int i = 0; i < 10; i += 3){
System.out.println(i);
}
Or maybe in reverse:
for (int i = 10; i >= 0; i--){
System.out.println(i);
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Array DeclarationAccesing an arrayIteration
Arrays
An array is used to store an indexed collection of same type values.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Array DeclarationAccesing an arrayIteration
Array Declaration
If we wanted an array of int’s for example:
int[] nums = new int[5];
The type of this variable is ”int[]”, or an array of ints.
The ”new int[5]” creates and initializes an array of 5 ints.
The indices of arrays will always start at 0.
These arrays are fixed length after initialization.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Array DeclarationAccesing an arrayIteration
Accesing an array
int[] myNums = new int[3];
System.out.println(myNums[0]);
System.out.println(myNums[1]);
System.out.println(myNums[2]);
System.out.println(myNums[3]);
Output:
0
0
0
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 3
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Array DeclarationAccesing an arrayIteration
Exception in thread "main"
java.lang.ArrayIndexOutOfBoundsException: 3
This is a very common error.Remember, indices start at 0, size != (upper) index.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Array DeclarationAccesing an arrayIteration
Iteration
Accesing each index manually is tedius.. is there a better way?
Yes, loops and arrays go together very well..
int[] myNums = new int[3];
for (int i = 0; i < myNums.length; i++){
System.out.println(myNums[i]);
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Array DeclarationAccesing an arrayIteration
This is extremely useful to remember at scale..
int[] myNums = new int[3000000];
for (int i = 0; i < myNums.length; i++){
System.out.println(myNums[i]);
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Try it!
Write a program that takes in a number n and creates an arraycorresponding to the Fibonacci series from the 0th term up to thenth term.
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
import java.util.Arrays;//This library is useful for
dealing with arrays.
public class Fib{
public static void main(String[] args) {
int n = Integer.parseInt(args[0]);
int[] fibs = new int[n + 1];//We specifically need
to include the nth term
if (n > 0)
fibs[1] = 1;
for (int i = 2; i < n + 1; i++){
fibs[i] = fibs[i-2] + fibs[i-1];
}
System.out.println(Arrays.toString(fibs));
}
}
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Is our solution correct though?
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Is our solution correct though?
java Foo 50
[0, 1, 1, 2, 3, 5, 8, 13, 21, 34, 55, 89, 144, 233, 377,
610, 987, 1597, 2584, 4181, 6765, 10946, 17711,
28657, 46368, 75025, 121393, 196418, 317811, 514229,
832040, 1346269, 2178309, 3524578, 5702887, 9227465,
14930352, 24157817, 39088169, 63245986, 102334155,
165580141, 267914296, 433494437, 701408733,
1134903170, 1836311903, -1323752223, 512559680,
-811192543, -298632863]
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
Discussion
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.
Variables and TypesConditionalsExcersize 1
LoopsArrays
Excersize 2
The End
References:Beamer Presentation template - http://www.LaTeXTemplates.com
Jemar Jones Variables, Types, Conditionals, Loops and Arrays.