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

What's hot (17)

PPTX
class and objects
Payel Guria
 
PDF
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
PPT
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
Mahmoud Alfarra
 
PPTX
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
PPTX
Static keyword ppt
Vinod Kumar
 
PPTX
Java Reflection @KonaTechAdda
Md Imran Hasan Hira
 
PPTX
Oop c++class(final).ppt
Alok Kumar
 
PPT
Introduction to-programming
BG Java EE Course
 
PPTX
Programming Fundamentals With OOPs Concepts (Java Examples Based)
indiangarg
 
PPT
البرمجة الهدفية بلغة جافا - تعدد الأشكال
Mahmoud Alfarra
 
PPTX
Static Data Members and Member Functions
MOHIT AGARWAL
 
PDF
Java 8 best practices - Stephen Colebourne
JAXLondon_Conference
 
PPTX
OOP with Java - continued
RatnaJava
 
PPT
البرمجة الهدفية بلغة جافا - الوراثة
Mahmoud Alfarra
 
PDF
Java SE 8 best practices
Stephen Colebourne
 
PPTX
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
PDF
Programming in c++
Baljit Saini
 
class and objects
Payel Guria
 
Java Programming - 04 object oriented in java
Danairat Thanabodithammachari
 
البرمجة الهدفية بلغة جافا - مفاهيم أساسية
Mahmoud Alfarra
 
‫Object Oriented Programming_Lecture 3
Mahmoud Alfarra
 
Static keyword ppt
Vinod Kumar
 
Java Reflection @KonaTechAdda
Md Imran Hasan Hira
 
Oop c++class(final).ppt
Alok Kumar
 
Introduction to-programming
BG Java EE Course
 
Programming Fundamentals With OOPs Concepts (Java Examples Based)
indiangarg
 
البرمجة الهدفية بلغة جافا - تعدد الأشكال
Mahmoud Alfarra
 
Static Data Members and Member Functions
MOHIT AGARWAL
 
Java 8 best practices - Stephen Colebourne
JAXLondon_Conference
 
OOP with Java - continued
RatnaJava
 
البرمجة الهدفية بلغة جافا - الوراثة
Mahmoud Alfarra
 
Java SE 8 best practices
Stephen Colebourne
 
Object Oriented Programming_Lecture 2
Mahmoud Alfarra
 
Programming in c++
Baljit Saini
 

Similar to Elements of Java Language - Continued (20)

PPTX
Session 03 - Elements of Java Language
PawanMM
 
PDF
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
PPS
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
PPTX
Templates and Exception Handling in C++
Nimrita Koul
 
PPTX
Full CSE 310 Unit 1 PPT.pptx for java language
ssuser2963071
 
PPTX
Introduction to Java
Ashita Agrawal
 
DOCX
C# language basics (Visual Studio)
rnkhan
 
DOCX
C# language basics (Visual studio)
rnkhan
 
PDF
Control structures functions and modules in python programming
Srinivas Narasegouda
 
PPTX
Unit - 2 CAP.pptx
malekaanjum1
 
PPT
Java Concepts with object oriented programming
KalpeshM7
 
PPT
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
PPT
Programming with Java - Essentials to program
leaderHilali1
 
PPTX
MODULE hdsfsf gefegsfs wfefwfwfg etegeg.pptx
prasathg214
 
PPT
Chap04
Terry Yoast
 
PPTX
Survelaine murillo ppt
Survelaine Murillo
 
PDF
GE3151_PSPP_All unit _Notes
Guru Nanak Technical Institutions
 
PPT
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
PPT
Programming with Java by Faizan Ahmed & Team
FaizanAhmed272398
 
Session 03 - Elements of Java Language
PawanMM
 
Unit 1- Part-2-Control and Loop Statements.pdf
Harsha Patil
 
Programming in Arduino (Part 2)
Niket Chandrawanshi
 
Templates and Exception Handling in C++
Nimrita Koul
 
Full CSE 310 Unit 1 PPT.pptx for java language
ssuser2963071
 
Introduction to Java
Ashita Agrawal
 
C# language basics (Visual Studio)
rnkhan
 
C# language basics (Visual studio)
rnkhan
 
Control structures functions and modules in python programming
Srinivas Narasegouda
 
Unit - 2 CAP.pptx
malekaanjum1
 
Java Concepts with object oriented programming
KalpeshM7
 
Mobile computing for Bsc Computer Science
ReshmiGopinath4
 
Programming with Java - Essentials to program
leaderHilali1
 
MODULE hdsfsf gefegsfs wfefwfwfg etegeg.pptx
prasathg214
 
Chap04
Terry Yoast
 
Survelaine murillo ppt
Survelaine Murillo
 
GE3151_PSPP_All unit _Notes
Guru Nanak Technical Institutions
 
CSL101_Ch1.ppt Computer Science
kavitamittal18
 
Programming with Java by Faizan Ahmed & Team
FaizanAhmed272398
 
Ad

More from Hitesh-Java (20)

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

Recently uploaded (20)

PDF
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
PDF
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
PDF
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
PDF
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
PDF
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
PDF
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
PPTX
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
PDF
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
PDF
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
PPT
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
PPTX
Top Managed Service Providers in Los Angeles
Captain IT
 
PDF
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
PDF
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
PPTX
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
PPTX
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
PDF
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
PDF
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
PPTX
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
PDF
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
PDF
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 
Log-Based Anomaly Detection: Enhancing System Reliability with Machine Learning
Mohammed BEKKOUCHE
 
SWEBOK Guide and Software Services Engineering Education
Hironori Washizaki
 
Building Resilience with Digital Twins : Lessons from Korea
SANGHEE SHIN
 
NewMind AI - Journal 100 Insights After The 100th Issue
NewMind AI
 
Apache CloudStack 201: Let's Design & Build an IaaS Cloud
ShapeBlue
 
Windsurf Meetup Ottawa 2025-07-12 - Planning Mode at Reliza.pdf
Pavel Shukhman
 
Building and Operating a Private Cloud with CloudStack and LINBIT CloudStack ...
ShapeBlue
 
Complete JavaScript Notes: From Basics to Advanced Concepts.pdf
haydendavispro
 
HCIP-Data Center Facility Deployment V2.0 Training Material (Without Remarks ...
mcastillo49
 
Interview paper part 3, It is based on Interview Prep
SoumyadeepGhosh39
 
Top Managed Service Providers in Los Angeles
Captain IT
 
Human-centred design in online workplace learning and relationship to engagem...
Tracy Tang
 
Meetup Kickoff & Welcome - Rohit Yadav, CSIUG Chairman
ShapeBlue
 
WooCommerce Workshop: Bring Your Laptop
Laura Hartwig
 
UiPath Academic Alliance Educator Panels: Session 2 - Business Analyst Content
DianaGray10
 
Women in Automation Presents: Reinventing Yourself — Bold Career Pivots That ...
DianaGray10
 
Smart Air Quality Monitoring with Serrax AQM190 LITE
SERRAX TECHNOLOGIES LLP
 
MSP360 Backup Scheduling and Retention Best Practices.pptx
MSP360
 
CloudStack GPU Integration - Rohit Yadav
ShapeBlue
 
CIFDAQ Token Spotlight for 9th July 2025
CIFDAQ
 

Elements of Java Language - Continued