SlideShare a Scribd company logo
Core Java Training
Elements of Java programming
Page 1Classification: Restricted
Agenda
• Elements of Java programming language
• Conditional Statements
• Loops
Conditional Statements
Page 3Classification: Restricted
Conditional Statements
• if
• if-else
• if-else ladder
• switch-case
• Ternary operator
Page 4Classification: Restricted
If statement
Page 5Classification: Restricted
if and if-else examples
Page 6Classification: Restricted
if-else ladder
Page 7Classification: Restricted
Ternary Operator
Page 8Classification: Restricted
Switch…case statement
Unlike if-then and if-then-else statements, the switch statement can have a number
of possible execution paths. A switch works with the byte, short, char, and int
primitive data types. It also works with enumerated types (discussed in Enum Types),
the String class, and a few special classes that wrap certain primitive types: Character,
Byte, Short, and Integer (discussed in Numbers and Strings).
* Provide default value – it is a good practice.
* The break is important. Don’t forget it.
Page 9Classification: Restricted
Exercise
Use Math.random() and an if-else statement to print the results of a coin flip.
Page 10Classification: Restricted
Exercise
Use Math.random() and an if-else statement to print the results of a coin flip.
Page 11Classification: Restricted
Exercise
• Write two programs - one using switch case and another using if else ladder
to print the full name of a month provided that the user inputs an integer
between 1 to 12.
• E.g. if the user inputs 1, print January.
Loops
Page 13Classification: Restricted
Loops
• while
• do..while
• for
Page 14Classification: Restricted
while
Page 15Classification: Restricted
do-while
Page 16Classification: Restricted
Exercise – what is output?
Page 17Classification: Restricted
Exercise – what is the output?
Page 18Classification: Restricted
Difference between while and do-while?
Do-while loop is always executed at least once, as the condition is checked at
the end of the loop.
Page 19Classification: Restricted
for statement
Page 20Classification: Restricted
Loop Examples
Page 21Classification: Restricted
Branching Statements: Break, Continue and Return…
Break:
The break statement has two forms: labeled and unlabeled. You saw the
unlabeled form in the previous discussion of the switch statement. You can
also use an unlabeled break to terminate a for, while, or do-while loop
An unlabeled break statement terminates the innermost switch, for, while, or
do-while statement, but a labeled break terminates an outer statement.
Page 22Classification: Restricted
Break statement: Example (Without Label)
Page 23Classification: Restricted
Break statement: Example (With Label)
Page 24Classification: Restricted
Branching Statements: Break, Continue and Return…
Continue:
The continue statement skips the current iteration of a for, while , or do-
while loop.
The unlabeled form skips to the end of the innermost loop's body and
evaluates the boolean expression that controls the loop.
The following program, ContinueDemo , steps through a String, counting the
occurences of the letter "p". If the current character is not a p, the continue
statement skips the rest of the loop and proceeds to the next character. If it
is a "p", the program increments the letter count.
Page 25Classification: Restricted
Continue Statement: Example (Without Label)
Page 26Classification: Restricted
Continue Statement: Example (Without Label)
Page 27Classification: Restricted
Branching Statements: Break, Continue and Return…
Return:
The last of the branching statements is the return statement. The return statement
exits from the current method, and control flow returns to where the method was
invoked. The return statement has two forms: one that returns a value, and one that
doesn't. To return a value, simply put the value (or an expression that calculates the
value) after the return keyword.
return ++count;
The data type of the returned value must match the type of the method's declared
return value. When a method is declared void, use the form of return that doesn't
return a value.
return;
Page 28Classification: Restricted
Exercise: What is the output?
Page 29Classification: Restricted
Exercise
Consider the following code snippet.
if (aNumber >= 0)
if (aNumber == 0)
System.out.println("first string");
else System.out.println("second string");
System.out.println("third string");
• What output do you think the code will produce if aNumber is 3?
• Write a test program containing the previous code snippet; make aNumber 3. What
is the output of the program? Is it what you predicted? Explain why the output is
what it is; in other words, what is the control flow for the code snippet?
• Using only spaces and line breaks, reformat the code snippet to make the control
flow easier to understand.
• Use braces, { and }, to further clarify the code.
Page 30Classification: Restricted
Exercise
• Print 10 HelloWorld using different kinds of loops
Page 31Classification: Restricted
Exercise
• Write a program to print all powers of 2 less than or equal to n.
Page 32Classification: Restricted
Exercise
• Write a program to print all powers of 2 less than or equal to n.
Page 33Classification: Restricted
Exercise
Write a program to print the binary equivalent of a decimal integer input by
the user.
Page 34Classification: Restricted
Exercise
Write a program to print the binary equivalent of a decimal integer input by
the user.
Page 35Classification: Restricted
Exercise
Use conditional operator (ternary operator) to find the least of 3 numbers
input by the user.
Page 36Classification: Restricted
Exercise: What is the value of m and n?
Page 37Classification: Restricted
Exercise: What is the output?
Page 38Classification: Restricted
Exercise: What’s wrong with this?
Page 39Classification: Restricted
Exercise: What is the output?
Page 40Classification: Restricted
Exercise: What is the output?
Page 41Classification: Restricted
Exercise: What is the output?
Answer: a*b
Page 42Classification: Restricted
Exercise: What is the output?
Page 43Classification: Restricted
Topics to be covered in next session
• Autoboxing and Unboxing in Java
• Handling Arrays
Page 44Classification: Restricted
Thank You!

More Related Content

PPSX
Strings in Java
PPSX
Exception Handling - Continued
PPSX
Exception Handling - Part 1
PPSX
OOP with Java - Abstract Classes and Interfaces
PPTX
Session 10 - OOP with Java - Abstract Classes and Interfaces
PPT
Core java concepts
PPTX
OOP with Java - Abstract Classes and Interfaces
PPT
C++ classes
Strings in Java
Exception Handling - Continued
Exception Handling - Part 1
OOP with Java - Abstract Classes and Interfaces
Session 10 - OOP with Java - Abstract Classes and Interfaces
Core java concepts
OOP with Java - Abstract Classes and Interfaces
C++ classes

What's hot (17)

PPTX
class and objects
PDF
Java Programming - 04 object oriented in java
PPT
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
PPTX
‫Object Oriented Programming_Lecture 3
PPTX
Static keyword ppt
PPTX
Java Reflection @KonaTechAdda
PPTX
Oop c++class(final).ppt
PPT
Introduction to-programming
PPTX
Programming Fundamentals With OOPs Concepts (Java Examples Based)
PPT
البرمجة الهدفية بلغة جافا - تعدد الأشكال
PPTX
Static Data Members and Member Functions
PDF
Java 8 best practices - Stephen Colebourne
PPTX
OOP with Java - continued
PPT
البرمجة الهدفية بلغة جافا - الوراثة
PDF
Java SE 8 best practices
PPTX
Object Oriented Programming_Lecture 2
PDF
Programming in c++
class and objects
Java Programming - 04 object oriented in java
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
‫Object Oriented Programming_Lecture 3
Static keyword ppt
Java Reflection @KonaTechAdda
Oop c++class(final).ppt
Introduction to-programming
Programming Fundamentals With OOPs Concepts (Java Examples Based)
البرمجة الهدفية بلغة جافا - تعدد الأشكال
Static Data Members and Member Functions
Java 8 best practices - Stephen Colebourne
OOP with Java - continued
البرمجة الهدفية بلغة جافا - الوراثة
Java SE 8 best practices
Object Oriented Programming_Lecture 2
Programming in c++
Ad

Similar to Elements of Java Language - Continued (20)

PPTX
Session 03 - Elements of Java Language
PPTX
Session 07 - Flow Control Statements
PPTX
Java chapter 3
PDF
java notes.pdf
PPTX
Control statements in java
PDF
Java input Scanner
PPTX
Lecture - 5 Control Statement
PPTX
Java Exception Handling
PPT
Control statements
PPTX
Flow of control by deepak lakhlan
PDF
Android Application Development - Level 3
PDF
Java Programming - 03 java control flow
PPTX
ICSE Class X Conditional Statements in java
PDF
Java chapter 5
PPT
4.CONTROL STATEMENTS_MB.ppt .
PPTX
control statements
PPTX
Control structures
PPT
M C6java5
PPT
Control statements in java programmng
PPT
Introduction to Java Programming Part 2
Session 03 - Elements of Java Language
Session 07 - Flow Control Statements
Java chapter 3
java notes.pdf
Control statements in java
Java input Scanner
Lecture - 5 Control Statement
Java Exception Handling
Control statements
Flow of control by deepak lakhlan
Android Application Development - Level 3
Java Programming - 03 java control flow
ICSE Class X Conditional Statements in java
Java chapter 5
4.CONTROL STATEMENTS_MB.ppt .
control statements
Control structures
M C6java5
Control statements in java programmng
Introduction to Java Programming Part 2
Ad

More from Hitesh-Java (20)

PPSX
Spring - Part 4 - Spring MVC
PPSX
Spring - Part 3 - AOP
PPSX
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
PPSX
Spring - Part 1 - IoC, Di and Beans
PPSX
JSP - Part 2 (Final)
PPSX
JSP - Part 1
PPSX
Struts 2 - Hibernate Integration
PPSX
Struts 2 - Introduction
PPSX
Hibernate - Part 2
PPSX
Hibernate - Part 1
PPSX
JDBC Part - 2
PPSX
PPSX
Java IO, Serialization
PPSX
Inner Classes
PPSX
Collections - Maps
PPSX
Review Session - Part -2
PPSX
Review Session and Attending Java Interviews
PPSX
Collections - Lists, Sets
PPSX
Collections - Sorting, Comparing Basics
PPSX
Collections - Array List
Spring - Part 4 - Spring MVC
Spring - Part 3 - AOP
Spring - Part 2 - Autowiring, Annotations, Java based Configuration - slides
Spring - Part 1 - IoC, Di and Beans
JSP - Part 2 (Final)
JSP - Part 1
Struts 2 - Hibernate Integration
Struts 2 - Introduction
Hibernate - Part 2
Hibernate - Part 1
JDBC Part - 2
Java IO, Serialization
Inner Classes
Collections - Maps
Review Session - Part -2
Review Session and Attending Java Interviews
Collections - Lists, Sets
Collections - Sorting, Comparing Basics
Collections - Array List

Recently uploaded (20)

PPTX
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
PDF
Review of recent advances in non-invasive hemoglobin estimation
DOCX
The AUB Centre for AI in Media Proposal.docx
PDF
The Rise and Fall of 3GPP – Time for a Sabbatical?
PDF
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
PDF
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
PPTX
20250228 LYD VKU AI Blended-Learning.pptx
PDF
Diabetes mellitus diagnosis method based random forest with bat algorithm
PDF
Empathic Computing: Creating Shared Understanding
PDF
KodekX | Application Modernization Development
PDF
Mobile App Security Testing_ A Comprehensive Guide.pdf
PDF
Chapter 3 Spatial Domain Image Processing.pdf
PDF
Encapsulation theory and applications.pdf
PPTX
Digital-Transformation-Roadmap-for-Companies.pptx
PDF
Building Integrated photovoltaic BIPV_UPV.pdf
PPTX
Cloud computing and distributed systems.
PDF
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
PPTX
sap open course for s4hana steps from ECC to s4
PPT
“AI and Expert System Decision Support & Business Intelligence Systems”
PDF
How UI/UX Design Impacts User Retention in Mobile Apps.pdf
VMware vSphere Foundation How to Sell Presentation-Ver1.4-2-14-2024.pptx
Review of recent advances in non-invasive hemoglobin estimation
The AUB Centre for AI in Media Proposal.docx
The Rise and Fall of 3GPP – Time for a Sabbatical?
Blue Purple Modern Animated Computer Science Presentation.pdf.pdf
Build a system with the filesystem maintained by OSTree @ COSCUP 2025
20250228 LYD VKU AI Blended-Learning.pptx
Diabetes mellitus diagnosis method based random forest with bat algorithm
Empathic Computing: Creating Shared Understanding
KodekX | Application Modernization Development
Mobile App Security Testing_ A Comprehensive Guide.pdf
Chapter 3 Spatial Domain Image Processing.pdf
Encapsulation theory and applications.pdf
Digital-Transformation-Roadmap-for-Companies.pptx
Building Integrated photovoltaic BIPV_UPV.pdf
Cloud computing and distributed systems.
Optimiser vos workloads AI/ML sur Amazon EC2 et AWS Graviton
sap open course for s4hana steps from ECC to s4
“AI and Expert System Decision Support & Business Intelligence Systems”
How UI/UX Design Impacts User Retention in Mobile Apps.pdf

Elements of Java Language - Continued