SlideShare a Scribd company logo
2. Programming with
Java Statements
• Understanding Assignment Statements
• Understanding Conditional Statements
• Understanding Iteration Statements
• Understanding Transfer of Control Statements
By Fernando Gil
Date: Jan 2015
Version: 1.0
Based in the book OCA Java SE 7 Programmer I Study Guide (Examn 1Z0-803)
1. Understanding Assignment
Statements
• An assignment statement sets a value within a variable
• All assignment are considered to be expression statements
• Expressions in Java are anything that has a value
• Typically, expressions evaluate to primitive types
Expression
Statement
Example
Assignment variable = 7
Pre-increment ++variable;
Pre-decrement --variable;
Post-increment variable++;
Post-decrement variable--;
Method invocation variable = performMethod();
Object creation variable = new ClassName();
The Assignment Expression
Statements
• Commonly known simply as assignment statements, are
designed to assign values to variables and must be
terminated with a semicolon
Variable = value;
• On the left is the variable that will be associated with the
memory and type necessary to store the value. On the
right is a literal value.
2. Understanding Conditional
Statements
• Conditional statements are used when there is a need for
determining the direction of flow based on conditions
The if conditional Statement
• The if statement is designed to conditionally execute a
statement or conditionally decide between a choice of
statements
• The if statement will execute only one statement upon the
condition, unless braces are supplied. Braces allow for
multiple enclosed statements to be executed. This group
of statements is also known as a block
• The expression that is evaluated within if statements must
evaluate to a boolean value
• The else clause is optional and may be omitted
The if-then conditional
Statement
• The if-then statement is used when multiple conditions
need to flow through a decision-based scenario
The if-then-else conditional
Statement
• The main difference is that the code will fall through to the
final stand-alone else when the expression fails to return
true for any condition
The switch conditional
Statement
• It is used to match the value from a switch statement
expression against a value associated with a case
keyword. Once matched, the enclosed statements
associated with the matching case value are executed,
unless a break statement is encountered
• The break statements are
optional and will cause the
immediate termination of the
switch conditional statement
• The expression of the switch
statement must evaluate to byte,
short, int, char, string
3. Understanding Iteration
Statements
• Iteration statements are used when there is a need to
iterate through pieces of code
• Iteration statements include for loop, enhanced for loop,
and the while and do-while statements.
• The continue statement is used to terminate the current
iteration and continue with the next iteration
• The break statement is used to exit the body of any
iteration statement
The for loop Iteration
Statement
• It has many parts that include an initialization part, an
expression part, and an iteration part
• The initialization does not need to declare a variable as
long as the variable is declared before the for statement.
So int x=0 and x=0 are both acceptable
• The expression within the for loop must be evaluated to a
boolean value
• The iteration, also known as the update part, provides the
mechanism that will allow the iteration to occur
The enhanced for loop
Iteration Statement
• It is used to iterate through an array, a collection, or an
object that implements the interface iterable.
• It is also commonly known as the for each loop
• Here is the general usage of the for each statement:
for (type variable : collection)
The while Iteration Statement
• This loop evaluates an expression and executes the loop
body only if the expression evaluates to true
• There is typically an expression within the body that will
affect the result of the expression
The do-while Iteration
Statement
• It is very similar to the while loop, except that it always
executes the body at least once
• The do-while loop evaluates an expression and continues
to execute the body only if it evaluates to true
4. Understanding Transfer of
Control Statements
• The transfer of control statements provide a means to stop
or interrupt the normal flow of control
• They are always used within other types of statements
• Transfer of control statements include the break, continue
and return statements
The break Transfer of Control
Statement
• It is used to exit or force an abrupt termination of the body
of the conditional and iteration statements
The continue Transfer of
Control Statement
• It is used to terminate the current iteration of a loop and
continue with the next iteration
The return Transfer of Control
Statement
• It is used to exit a method and optionally return a specified
value as an expression
• A return statement with no return value must have the
keyword void in the method declaration
Knowing your Statement-Related
keywords
3. Programming with
Java Operators
Next Slide:

More Related Content

What's hot

Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
ilakkiya
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
Jomel Penalba
 
Template method pattern example
Template method pattern exampleTemplate method pattern example
Template method pattern example
Guo Albert
 

What's hot (20)

Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Decision statements in vb.net
Decision statements in vb.netDecision statements in vb.net
Decision statements in vb.net
 
Control structures selection
Control structures   selectionControl structures   selection
Control structures selection
 
Control statements
Control statementsControl statements
Control statements
 
Control Structures: Part 1
Control Structures: Part 1Control Structures: Part 1
Control Structures: Part 1
 
Control structures ii
Control structures ii Control structures ii
Control structures ii
 
10. switch case
10. switch case10. switch case
10. switch case
 
Switch Case in C Programming
Switch Case in C ProgrammingSwitch Case in C Programming
Switch Case in C Programming
 
Conditional statement in c
Conditional statement in cConditional statement in c
Conditional statement in c
 
04 control structures 1
04 control structures 104 control structures 1
04 control structures 1
 
Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1Week 1: Getting Your Hands Dirty - Part 1
Week 1: Getting Your Hands Dirty - Part 1
 
Quiz5
Quiz5Quiz5
Quiz5
 
The Switch Statement in java
The Switch Statement in javaThe Switch Statement in java
The Switch Statement in java
 
Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1Bt0067 c programming and data structures 1
Bt0067 c programming and data structures 1
 
Week 2: Getting Your Hands Dirty – Part 2
Week 2: Getting Your Hands Dirty – Part 2Week 2: Getting Your Hands Dirty – Part 2
Week 2: Getting Your Hands Dirty – Part 2
 
Template method pattern example
Template method pattern exampleTemplate method pattern example
Template method pattern example
 
Cis160 Final Review
Cis160 Final ReviewCis160 Final Review
Cis160 Final Review
 
Python operators part3
Python operators part3Python operators part3
Python operators part3
 
Template Method Pattern
Template Method PatternTemplate Method Pattern
Template Method Pattern
 
PL/SQL Example for IF .. ELSIF
PL/SQL Example for IF .. ELSIFPL/SQL Example for IF .. ELSIF
PL/SQL Example for IF .. ELSIF
 

Viewers also liked

how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
Sajid Marwat
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
Gayathri Gaayu
 

Viewers also liked (13)

Java pdf
Java   pdfJava   pdf
Java pdf
 
Ass2 1 (2)
Ass2 1 (2)Ass2 1 (2)
Ass2 1 (2)
 
Java ocjp level_2
Java ocjp level_2Java ocjp level_2
Java ocjp level_2
 
Quiz test JDBC
Quiz test JDBCQuiz test JDBC
Quiz test JDBC
 
OCA JAVA - 3 Programming with Java Operators
 OCA JAVA - 3 Programming with Java Operators OCA JAVA - 3 Programming with Java Operators
OCA JAVA - 3 Programming with Java Operators
 
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building BlocksOCA Java SE 8 Exam Chapter 1 Java Building Blocks
OCA Java SE 8 Exam Chapter 1 Java Building Blocks
 
how to calclute time complexity of algortihm
how to calclute time complexity of algortihmhow to calclute time complexity of algortihm
how to calclute time complexity of algortihm
 
Cracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 ExamsCracking OCA and OCP Java 8 Exams
Cracking OCA and OCP Java 8 Exams
 
Design & Analysis Of Algorithm
Design & Analysis Of AlgorithmDesign & Analysis Of Algorithm
Design & Analysis Of Algorithm
 
DESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMSDESIGN AND ANALYSIS OF ALGORITHMS
DESIGN AND ANALYSIS OF ALGORITHMS
 
Design and Analysis of Algorithms
Design and Analysis of AlgorithmsDesign and Analysis of Algorithms
Design and Analysis of Algorithms
 
Study: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving CarsStudy: The Future of VR, AR and Self-Driving Cars
Study: The Future of VR, AR and Self-Driving Cars
 
TEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of WorkTEDx Manchester: AI & The Future of Work
TEDx Manchester: AI & The Future of Work
 

Similar to OCA JAVA - 2 Programming with Java Statements

Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
AdrianVANTOPINA
 

Similar to OCA JAVA - 2 Programming with Java Statements (20)

05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
 
data types.pdf
data types.pdfdata types.pdf
data types.pdf
 
Lecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdfLecture 7 Control Statements.pdf
Lecture 7 Control Statements.pdf
 
Lecture-13.ppt
Lecture-13.pptLecture-13.ppt
Lecture-13.ppt
 
Java 2.pptx
Java 2.pptxJava 2.pptx
Java 2.pptx
 
Control structure
Control structureControl structure
Control structure
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
presentation on powerpoint template.pptx
presentation on powerpoint template.pptxpresentation on powerpoint template.pptx
presentation on powerpoint template.pptx
 
Presentation on C Switch Case Statements
Presentation on C Switch Case StatementsPresentation on C Switch Case Statements
Presentation on C Switch Case Statements
 
Chapter05-Control Structures.pptx
Chapter05-Control Structures.pptxChapter05-Control Structures.pptx
Chapter05-Control Structures.pptx
 
cpu.pdf
cpu.pdfcpu.pdf
cpu.pdf
 
While , For , Do-While Loop
While , For , Do-While LoopWhile , For , Do-While Loop
While , For , Do-While Loop
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Java Exception Handling
Java Exception HandlingJava Exception Handling
Java Exception Handling
 
03 conditions loops
03   conditions loops03   conditions loops
03 conditions loops
 
System verilog control flow
System verilog control flowSystem verilog control flow
System verilog control flow
 
Flow of control by deepak lakhlan
Flow of control by deepak lakhlanFlow of control by deepak lakhlan
Flow of control by deepak lakhlan
 
C language (Part 2)
C language (Part 2)C language (Part 2)
C language (Part 2)
 
C-Programming Control statements.pptx
C-Programming Control statements.pptxC-Programming Control statements.pptx
C-Programming Control statements.pptx
 

Recently uploaded

How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
Globus
 

Recently uploaded (20)

Using IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New ZealandUsing IESVE for Room Loads Analysis - Australia & New Zealand
Using IESVE for Room Loads Analysis - Australia & New Zealand
 
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERRORTROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
TROUBLESHOOTING 9 TYPES OF OUTOFMEMORYERROR
 
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
Abortion ^Clinic ^%[+971588192166''] Abortion Pill Al Ain (?@?) Abortion Pill...
 
Advanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should KnowAdvanced Flow Concepts Every Developer Should Know
Advanced Flow Concepts Every Developer Should Know
 
Enhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdfEnhancing Research Orchestration Capabilities at ORNL.pdf
Enhancing Research Orchestration Capabilities at ORNL.pdf
 
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
In 2015, I used to write extensions for Joomla, WordPress, phpBB3, etc and I ...
 
Studiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting softwareStudiovity film pre-production and screenwriting software
Studiovity film pre-production and screenwriting software
 
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital TransformationWSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
WSO2Con2024 - WSO2's IAM Vision: Identity-Led Digital Transformation
 
Cyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdfCyaniclab : Software Development Agency Portfolio.pdf
Cyaniclab : Software Development Agency Portfolio.pdf
 
Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...Developing Distributed High-performance Computing Capabilities of an Open Sci...
Developing Distributed High-performance Computing Capabilities of an Open Sci...
 
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
Paketo Buildpacks : la meilleure façon de construire des images OCI? DevopsDa...
 
Into the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdfInto the Box 2024 - Keynote Day 2 Slides.pdf
Into the Box 2024 - Keynote Day 2 Slides.pdf
 
AI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning FrameworkAI/ML Infra Meetup | Perspective on Deep Learning Framework
AI/ML Infra Meetup | Perspective on Deep Learning Framework
 
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
Exploring Innovations in Data Repository Solutions - Insights from the U.S. G...
 
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAGAI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
AI/ML Infra Meetup | Reducing Prefill for LLM Serving in RAG
 
GlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote sessionGlobusWorld 2024 Opening Keynote session
GlobusWorld 2024 Opening Keynote session
 
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.ILBeyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
Beyond Event Sourcing - Embracing CRUD for Wix Platform - Java.IL
 
AI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in MichelangeloAI/ML Infra Meetup | ML explainability in Michelangelo
AI/ML Infra Meetup | ML explainability in Michelangelo
 
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
Facemoji Keyboard released its 2023 State of Emoji report, outlining the most...
 
How to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good PracticesHow to Position Your Globus Data Portal for Success Ten Good Practices
How to Position Your Globus Data Portal for Success Ten Good Practices
 

OCA JAVA - 2 Programming with Java Statements

  • 1. 2. Programming with Java Statements • Understanding Assignment Statements • Understanding Conditional Statements • Understanding Iteration Statements • Understanding Transfer of Control Statements By Fernando Gil Date: Jan 2015 Version: 1.0 Based in the book OCA Java SE 7 Programmer I Study Guide (Examn 1Z0-803)
  • 2. 1. Understanding Assignment Statements • An assignment statement sets a value within a variable • All assignment are considered to be expression statements • Expressions in Java are anything that has a value • Typically, expressions evaluate to primitive types Expression Statement Example Assignment variable = 7 Pre-increment ++variable; Pre-decrement --variable; Post-increment variable++; Post-decrement variable--; Method invocation variable = performMethod(); Object creation variable = new ClassName();
  • 3. The Assignment Expression Statements • Commonly known simply as assignment statements, are designed to assign values to variables and must be terminated with a semicolon Variable = value; • On the left is the variable that will be associated with the memory and type necessary to store the value. On the right is a literal value.
  • 4. 2. Understanding Conditional Statements • Conditional statements are used when there is a need for determining the direction of flow based on conditions
  • 5. The if conditional Statement • The if statement is designed to conditionally execute a statement or conditionally decide between a choice of statements • The if statement will execute only one statement upon the condition, unless braces are supplied. Braces allow for multiple enclosed statements to be executed. This group of statements is also known as a block • The expression that is evaluated within if statements must evaluate to a boolean value • The else clause is optional and may be omitted
  • 6.
  • 7. The if-then conditional Statement • The if-then statement is used when multiple conditions need to flow through a decision-based scenario
  • 8. The if-then-else conditional Statement • The main difference is that the code will fall through to the final stand-alone else when the expression fails to return true for any condition
  • 9. The switch conditional Statement • It is used to match the value from a switch statement expression against a value associated with a case keyword. Once matched, the enclosed statements associated with the matching case value are executed, unless a break statement is encountered • The break statements are optional and will cause the immediate termination of the switch conditional statement • The expression of the switch statement must evaluate to byte, short, int, char, string
  • 10.
  • 11. 3. Understanding Iteration Statements • Iteration statements are used when there is a need to iterate through pieces of code • Iteration statements include for loop, enhanced for loop, and the while and do-while statements. • The continue statement is used to terminate the current iteration and continue with the next iteration • The break statement is used to exit the body of any iteration statement
  • 12. The for loop Iteration Statement • It has many parts that include an initialization part, an expression part, and an iteration part • The initialization does not need to declare a variable as long as the variable is declared before the for statement. So int x=0 and x=0 are both acceptable • The expression within the for loop must be evaluated to a boolean value • The iteration, also known as the update part, provides the mechanism that will allow the iteration to occur
  • 13.
  • 14. The enhanced for loop Iteration Statement • It is used to iterate through an array, a collection, or an object that implements the interface iterable. • It is also commonly known as the for each loop • Here is the general usage of the for each statement: for (type variable : collection)
  • 15. The while Iteration Statement • This loop evaluates an expression and executes the loop body only if the expression evaluates to true • There is typically an expression within the body that will affect the result of the expression
  • 16. The do-while Iteration Statement • It is very similar to the while loop, except that it always executes the body at least once • The do-while loop evaluates an expression and continues to execute the body only if it evaluates to true
  • 17. 4. Understanding Transfer of Control Statements • The transfer of control statements provide a means to stop or interrupt the normal flow of control • They are always used within other types of statements • Transfer of control statements include the break, continue and return statements
  • 18. The break Transfer of Control Statement • It is used to exit or force an abrupt termination of the body of the conditional and iteration statements
  • 19. The continue Transfer of Control Statement • It is used to terminate the current iteration of a loop and continue with the next iteration
  • 20. The return Transfer of Control Statement • It is used to exit a method and optionally return a specified value as an expression • A return statement with no return value must have the keyword void in the method declaration
  • 22. 3. Programming with Java Operators Next Slide: