SlideShare ist ein Scribd-Unternehmen logo
1 von 26
Chapter 2:
For Loops
AP Computer Science A
Building Java Programs, 3rd Edition
In this chapter:
Primitive data types
Operators
Variables
Expressions
Primitive
Data Types
Data Types
Type Representation
int Integers from negative to positive 2 -1
double Real numbers from negative to positive 10
char Single text characters in single quotes
boolean True/false values
String A line of text in double quotes
31
308
Primitive data types
Objects
Operators
int
Addition, subtraction, and multiplication
2 + 2 is 4
5 - 3 is -2
7 * 3 is 21
Dividing with ints
4 / 2 is 2
But…
15 / 4 is not 3.75
You need to cut off what is after the decimal point.
15 / 4 is 3
You are NOT rounding--just leaving out digits.
But…
15.0 / 4 is 3.75
This is because 15.0 is a double, not an int.
Mod (modulus)
% finds the remainder after integer division
x % y is x - (x / y) * y
6 % 2 is 0
5 % 3 is 2
3 % 5 is 3
10 % 10 is 0
integer division
Mod Hacks
● If x % y == 0 is true, then x is
divisible by y.
● If x % 2 == 0, then x is even. If it is
1, then x is odd.
● x % 10 finds the last "a" number
of digits of x
● x / 10 % 10 finds the "a"th digit of
x starting from the left
a
a
double
Accurate to 16 places
Roundoff error examples (try them, they actually
work):
10.0 / 3.0 is 3.3333333333333335
3 * 3.2 is 9.600000000000001
char
The space bar ' ' is also valid
A single quote (') as a char is '''
chars, ints, and ASCII Values
int a = ('a' + 'b') / 2; //becomes 97
/*
(97 + 98) / 2
195 / 2
97
*/
'a' - 98 cannot become a char
char c = 'c' - 2; //becomes 'a'
//99 - 2 is 97, which is 'a'
boolean
Symbol Meaning
== equal to
!= not equal to
> greater than
>= greater than or equal to
< less than
<= less than or equal to
Ands && Ors
True/False Table for && True/False Table for ||
true false
true true false
false false false
true false
true true true
false true false
Short-circuited evaluation:
1. If the first thing in an "and" statement is false, the entire "and" statement is
assumed to be false. 3 > 5 && 1 / 0 == 0 is assumed false.
2. If the first thing in an "or" statement is true, the entire "or" statement is
assumed to be true. 3 < 5 || 1 / 0 == 0 is assumed true.
Variables
Initializing Variables
The naming convention for variables isTheSameAsForMethods.
The equal sign means "to get the value of" or "becomes"
int b = 4;
int b = 2; //compile error
int c = 4;
int a = 5;
type
name
value
String Concatenation
"this " + "that" becomes "this that"
"This statement is " + true becomes "This statement is true"
"I am number " + 1 becomes "I am number 1"
"myVariable" ≠ myVariable
'6' ≠ 6
false ≠ "false"
4 + 2 + " is my number" becomes "6 is my number"
"" + 4 + 2 + " is my number" becomes "42 is my number"
"You're number " + 1 + 2 becomes "You're number 12"
"You're number " + (1 + 2) becomes "You're number 3"
Casting
Implicit Casting Explicit Casting
● 5.0 / 2
2 → 2.0
● 'a' * 2
'a' → 97
● 5.0 / (double) 2
2 → 2.0
● (int) 'a' * 2
'a' → 97
● (int) 3.14
3.14 → 3
● (int) 3.5 * 2 + 1
3 * 2 + 1
7
● (int) (3.5 * 2 + 1)
(int) (8)
8
Expressions
Operator Precedence
PrecedenceRank Operator Category Example
1 Parentheses ( )
2 Unary minus sign in front of numbers
3 Casting (int), (char), (double), 5 → 5.0, 'a' → 97
4 Multiplicative *, /, %
5 Additive + for adding, -, + for concatenation
6 Relational <, <=, >, >=
7 Equality ==, !=
8 And &&
9 Or ||
10 Assignment =
Operation Shorthands
Expression Shorthand
x = x + 3; x += 3;
x = x - 4; x -= 4;
x = x * 2; x *= 2;
x = x / 5; x /= 5;
x = x + 1; //incrementing x++; x += 1;
x = x - 1; //decrementing x--; x -= 1;
For Loops
What is a For Loop?
for (int i = 1; i <= 5; i++) {
System.out.print(i);
}
Initialization
(statement)
Test
(boolean
variable)
Update
(statement)
Statement(s)
for (;;) {
System.out.println("Infinite loop!");
}
How a For Loop Runs
for (int i = 1; i <= 5; i++) {
System.out.print(i);
}
Do the
initialization
Is the test
true?
Yes
No
Exit for loop
Do the statements inside
Do the update
Nested For Loops
for (int i = 1; i <= 50; i++) {
for (int j = 1; j <= 50; j++) {
System.out.print("*");
} //prints out one line
System.out.println();
} //repeats the line
i j
1 1
2
3
2 1
2
3
3 1
2
3
Class Constants
public class ClassConstant {
public static final int SIZE = 20;
//final means it cannot be changed after declaring
public static void main(String[] args) {
for (int i = 1; i <= SIZE; i++) {
for (int j = 1; j <= SIZE; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
Class constant naming convention:
ALL_CAPS_WITH_UNDERSCORES

Weitere ähnliche Inhalte

Was ist angesagt?

Was ist angesagt? (19)

Misra c rules
Misra c rulesMisra c rules
Misra c rules
 
Chap 4 c++
Chap 4 c++Chap 4 c++
Chap 4 c++
 
100 c interview questions answers
100 c interview questions answers100 c interview questions answers
100 c interview questions answers
 
C tutorial
C tutorialC tutorial
C tutorial
 
Input processing and output in Python
Input processing and output in PythonInput processing and output in Python
Input processing and output in Python
 
Operator Precedence and Associativity
Operator Precedence and AssociativityOperator Precedence and Associativity
Operator Precedence and Associativity
 
Chap 5 c++
Chap 5 c++Chap 5 c++
Chap 5 c++
 
Lesson 9. Pattern 1. Magic numbers
Lesson 9. Pattern 1. Magic numbersLesson 9. Pattern 1. Magic numbers
Lesson 9. Pattern 1. Magic numbers
 
Pseudocode
PseudocodePseudocode
Pseudocode
 
02b 1 ma0_1f_-_march_2013_mark_scheme
02b 1 ma0_1f_-_march_2013_mark_scheme02b 1 ma0_1f_-_march_2013_mark_scheme
02b 1 ma0_1f_-_march_2013_mark_scheme
 
Introduction to programming - class 3
Introduction to programming - class 3Introduction to programming - class 3
Introduction to programming - class 3
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
 
Micro Blaze C Reference
Micro Blaze C ReferenceMicro Blaze C Reference
Micro Blaze C Reference
 
Data types and operators in vb
Data types and operators  in vbData types and operators  in vb
Data types and operators in vb
 
Unit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in CUnit ii chapter 2 Decision making and Branching in C
Unit ii chapter 2 Decision making and Branching in C
 
Python : Operators
Python : OperatorsPython : Operators
Python : Operators
 
C# conditional branching statement
C# conditional branching statementC# conditional branching statement
C# conditional branching statement
 
Decisions
DecisionsDecisions
Decisions
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 

Andere mochten auch (20)

Core java online training
Core java online trainingCore java online training
Core java online training
 
Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
 
Yaazli International Web Project Workshop
Yaazli International Web Project WorkshopYaazli International Web Project Workshop
Yaazli International Web Project Workshop
 
Yaazli International AngularJS 5 Training
Yaazli International AngularJS 5 TrainingYaazli International AngularJS 5 Training
Yaazli International AngularJS 5 Training
 
09events
09events09events
09events
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Non ieee dot net projects list
Non  ieee dot net projects list Non  ieee dot net projects list
Non ieee dot net projects list
 
02basics
02basics02basics
02basics
 
Java quick reference v2
Java quick reference v2Java quick reference v2
Java quick reference v2
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Savr
SavrSavr
Savr
 
Yaazli International Hibernate Training
Yaazli International Hibernate TrainingYaazli International Hibernate Training
Yaazli International Hibernate Training
 
Yaazli International Spring Training
Yaazli International Spring Training Yaazli International Spring Training
Yaazli International Spring Training
 
Non ieee java projects list
Non  ieee java projects list Non  ieee java projects list
Non ieee java projects list
 
Singleton pattern
Singleton patternSingleton pattern
Singleton pattern
 
Java Basic Operators
Java Basic OperatorsJava Basic Operators
Java Basic Operators
 
DIWE - Using Extensions and Image Manipulation
DIWE - Using Extensions and Image ManipulationDIWE - Using Extensions and Image Manipulation
DIWE - Using Extensions and Image Manipulation
 
DIWE - Multimedia Technologies
DIWE - Multimedia TechnologiesDIWE - Multimedia Technologies
DIWE - Multimedia Technologies
 
Yaazli International Java Training
Yaazli International Java Training Yaazli International Java Training
Yaazli International Java Training
 
java swing
java swingjava swing
java swing
 

Ähnlich wie For Loops and Variables in Java

Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsJames Brotsos
 
03-Primitive-Datatypes.pdf
03-Primitive-Datatypes.pdf03-Primitive-Datatypes.pdf
03-Primitive-Datatypes.pdfKaraBaesh
 
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptMahyuddin8
 
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptghoitsun
 
6th grade math notes
6th grade math notes6th grade math notes
6th grade math noteskonishiki
 
Lecture 3 and 4.pptx
Lecture 3 and 4.pptxLecture 3 and 4.pptx
Lecture 3 and 4.pptxMAHAMASADIK
 
Lab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsLab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsAksharVaish2
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象勇浩 赖
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language Mohamed Loey
 

Ähnlich wie For Loops and Variables in Java (20)

Ch02 primitive-data-definite-loops
Ch02 primitive-data-definite-loopsCh02 primitive-data-definite-loops
Ch02 primitive-data-definite-loops
 
03-Primitive-Datatypes.pdf
03-Primitive-Datatypes.pdf03-Primitive-Datatypes.pdf
03-Primitive-Datatypes.pdf
 
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
 
ch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.pptch02-primitive-data-definite-loops.ppt
ch02-primitive-data-definite-loops.ppt
 
C++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdfC++ in 10 Hours.pdf.pdf
C++ in 10 Hours.pdf.pdf
 
Lecture 3
Lecture 3Lecture 3
Lecture 3
 
Arrays in C++
Arrays in C++Arrays in C++
Arrays in C++
 
Python
PythonPython
Python
 
6th grade math notes
6th grade math notes6th grade math notes
6th grade math notes
 
Lecture 3 and 4.pptx
Lecture 3 and 4.pptxLecture 3 and 4.pptx
Lecture 3 and 4.pptx
 
C SLIDES PREPARED BY M V B REDDY
C SLIDES PREPARED BY  M V B REDDYC SLIDES PREPARED BY  M V B REDDY
C SLIDES PREPARED BY M V B REDDY
 
Lab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressionsLab 4 reading material formatted io and arithmetic expressions
Lab 4 reading material formatted io and arithmetic expressions
 
c_tutorial_2.ppt
c_tutorial_2.pptc_tutorial_2.ppt
c_tutorial_2.ppt
 
Falcon初印象
Falcon初印象Falcon初印象
Falcon初印象
 
901131 examples
901131 examples901131 examples
901131 examples
 
pointers 1
pointers 1pointers 1
pointers 1
 
What is c
What is cWhat is c
What is c
 
Token and operators
Token and operatorsToken and operators
Token and operators
 
ICP - Lecture 7 and 8
ICP - Lecture 7 and 8ICP - Lecture 7 and 8
ICP - Lecture 7 and 8
 
C++ Programming Language
C++ Programming Language C++ Programming Language
C++ Programming Language
 

Kürzlich hochgeladen

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsMiki Katsuragi
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piececharlottematthew16
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Commit University
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxhariprasad279825
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationRidwan Fadjar
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brandgvaughan
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfAddepto
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek SchlawackFwdays
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationSlibray Presentation
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):comworks
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxNavinnSomaal
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfSeasiaInfotech2
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubKalema Edgar
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024Lorenzo Miniero
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsMark Billinghurst
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clashcharlottematthew16
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesZilliz
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Mattias Andersson
 

Kürzlich hochgeladen (20)

Vertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering TipsVertex AI Gemini Prompt Engineering Tips
Vertex AI Gemini Prompt Engineering Tips
 
Story boards and shot lists for my a level piece
Story boards and shot lists for my a level pieceStory boards and shot lists for my a level piece
Story boards and shot lists for my a level piece
 
Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!Nell’iperspazio con Rocket: il Framework Web di Rust!
Nell’iperspazio con Rocket: il Framework Web di Rust!
 
Artificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptxArtificial intelligence in cctv survelliance.pptx
Artificial intelligence in cctv survelliance.pptx
 
My Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 PresentationMy Hashitalk Indonesia April 2024 Presentation
My Hashitalk Indonesia April 2024 Presentation
 
WordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your BrandWordPress Websites for Engineers: Elevate Your Brand
WordPress Websites for Engineers: Elevate Your Brand
 
Gen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdfGen AI in Business - Global Trends Report 2024.pdf
Gen AI in Business - Global Trends Report 2024.pdf
 
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
"Subclassing and Composition – A Pythonic Tour of Trade-Offs", Hynek Schlawack
 
Connect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck PresentationConnect Wave/ connectwave Pitch Deck Presentation
Connect Wave/ connectwave Pitch Deck Presentation
 
DMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special EditionDMCC Future of Trade Web3 - Special Edition
DMCC Future of Trade Web3 - Special Edition
 
CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):CloudStudio User manual (basic edition):
CloudStudio User manual (basic edition):
 
SAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptxSAP Build Work Zone - Overview L2-L3.pptx
SAP Build Work Zone - Overview L2-L3.pptx
 
The Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdfThe Future of Software Development - Devin AI Innovative Approach.pdf
The Future of Software Development - Devin AI Innovative Approach.pdf
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 
Unleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding ClubUnleash Your Potential - Namagunga Girls Coding Club
Unleash Your Potential - Namagunga Girls Coding Club
 
SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024SIP trunking in Janus @ Kamailio World 2024
SIP trunking in Janus @ Kamailio World 2024
 
Human Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR SystemsHuman Factors of XR: Using Human Factors to Design XR Systems
Human Factors of XR: Using Human Factors to Design XR Systems
 
Powerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time ClashPowerpoint exploring the locations used in television show Time Clash
Powerpoint exploring the locations used in television show Time Clash
 
Vector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector DatabasesVector Databases 101 - An introduction to the world of Vector Databases
Vector Databases 101 - An introduction to the world of Vector Databases
 
Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?Are Multi-Cloud and Serverless Good or Bad?
Are Multi-Cloud and Serverless Good or Bad?
 

For Loops and Variables in Java

  • 1. Chapter 2: For Loops AP Computer Science A Building Java Programs, 3rd Edition
  • 2. In this chapter: Primitive data types Operators Variables Expressions
  • 4. Data Types Type Representation int Integers from negative to positive 2 -1 double Real numbers from negative to positive 10 char Single text characters in single quotes boolean True/false values String A line of text in double quotes 31 308 Primitive data types Objects
  • 6. int Addition, subtraction, and multiplication 2 + 2 is 4 5 - 3 is -2 7 * 3 is 21
  • 7. Dividing with ints 4 / 2 is 2 But… 15 / 4 is not 3.75 You need to cut off what is after the decimal point. 15 / 4 is 3 You are NOT rounding--just leaving out digits. But… 15.0 / 4 is 3.75 This is because 15.0 is a double, not an int.
  • 8. Mod (modulus) % finds the remainder after integer division x % y is x - (x / y) * y 6 % 2 is 0 5 % 3 is 2 3 % 5 is 3 10 % 10 is 0 integer division Mod Hacks ● If x % y == 0 is true, then x is divisible by y. ● If x % 2 == 0, then x is even. If it is 1, then x is odd. ● x % 10 finds the last "a" number of digits of x ● x / 10 % 10 finds the "a"th digit of x starting from the left a a
  • 9. double Accurate to 16 places Roundoff error examples (try them, they actually work): 10.0 / 3.0 is 3.3333333333333335 3 * 3.2 is 9.600000000000001
  • 10. char The space bar ' ' is also valid A single quote (') as a char is '''
  • 11.
  • 12. chars, ints, and ASCII Values int a = ('a' + 'b') / 2; //becomes 97 /* (97 + 98) / 2 195 / 2 97 */ 'a' - 98 cannot become a char char c = 'c' - 2; //becomes 'a' //99 - 2 is 97, which is 'a'
  • 13. boolean Symbol Meaning == equal to != not equal to > greater than >= greater than or equal to < less than <= less than or equal to
  • 14. Ands && Ors True/False Table for && True/False Table for || true false true true false false false false true false true true true false true false Short-circuited evaluation: 1. If the first thing in an "and" statement is false, the entire "and" statement is assumed to be false. 3 > 5 && 1 / 0 == 0 is assumed false. 2. If the first thing in an "or" statement is true, the entire "or" statement is assumed to be true. 3 < 5 || 1 / 0 == 0 is assumed true.
  • 16. Initializing Variables The naming convention for variables isTheSameAsForMethods. The equal sign means "to get the value of" or "becomes" int b = 4; int b = 2; //compile error int c = 4; int a = 5; type name value
  • 17. String Concatenation "this " + "that" becomes "this that" "This statement is " + true becomes "This statement is true" "I am number " + 1 becomes "I am number 1" "myVariable" ≠ myVariable '6' ≠ 6 false ≠ "false" 4 + 2 + " is my number" becomes "6 is my number" "" + 4 + 2 + " is my number" becomes "42 is my number" "You're number " + 1 + 2 becomes "You're number 12" "You're number " + (1 + 2) becomes "You're number 3"
  • 18. Casting Implicit Casting Explicit Casting ● 5.0 / 2 2 → 2.0 ● 'a' * 2 'a' → 97 ● 5.0 / (double) 2 2 → 2.0 ● (int) 'a' * 2 'a' → 97 ● (int) 3.14 3.14 → 3 ● (int) 3.5 * 2 + 1 3 * 2 + 1 7 ● (int) (3.5 * 2 + 1) (int) (8) 8
  • 20. Operator Precedence PrecedenceRank Operator Category Example 1 Parentheses ( ) 2 Unary minus sign in front of numbers 3 Casting (int), (char), (double), 5 → 5.0, 'a' → 97 4 Multiplicative *, /, % 5 Additive + for adding, -, + for concatenation 6 Relational <, <=, >, >= 7 Equality ==, != 8 And && 9 Or || 10 Assignment =
  • 21. Operation Shorthands Expression Shorthand x = x + 3; x += 3; x = x - 4; x -= 4; x = x * 2; x *= 2; x = x / 5; x /= 5; x = x + 1; //incrementing x++; x += 1; x = x - 1; //decrementing x--; x -= 1;
  • 23. What is a For Loop? for (int i = 1; i <= 5; i++) { System.out.print(i); } Initialization (statement) Test (boolean variable) Update (statement) Statement(s) for (;;) { System.out.println("Infinite loop!"); }
  • 24. How a For Loop Runs for (int i = 1; i <= 5; i++) { System.out.print(i); } Do the initialization Is the test true? Yes No Exit for loop Do the statements inside Do the update
  • 25. Nested For Loops for (int i = 1; i <= 50; i++) { for (int j = 1; j <= 50; j++) { System.out.print("*"); } //prints out one line System.out.println(); } //repeats the line i j 1 1 2 3 2 1 2 3 3 1 2 3
  • 26. Class Constants public class ClassConstant { public static final int SIZE = 20; //final means it cannot be changed after declaring public static void main(String[] args) { for (int i = 1; i <= SIZE; i++) { for (int j = 1; j <= SIZE; j++) { System.out.print("*"); } System.out.println(); } } } Class constant naming convention: ALL_CAPS_WITH_UNDERSCORES