SlideShare a Scribd company logo
2
Most read
3
Most read
6
Most read
Conditional Statements
&
Type Conversion in JAVA
Dr. Kuppusamy .P
Associate Professor / SCOPE
Conditional Statements
• Conditional Statements executes one or set of statements based on a
condition exactly once.
• There are four types of Conditional Statements in java
• Simple if
• else..if
• Nested else…if
• Switch case statements
Dr. Kuppusamy P
Simple if statement
• syntax :
if(boolean expression)
{
statement–block;
}
Other statements;
Dr. Kuppusamy P
Simple if statement
/* This is an example of simple if statement */
public class SampleTest
{
public static void main(String args[])
{
int a = 4;
int b = 20;
if( a < b )
{
System.out.println("This is if statement");
}
}
}
Dr. Kuppusamy P
if….else statement
• The is an extension of simple if statement
• syntax :
if (Boolean expression)
{
True -block statements;
}
else
{
False -block statements ;
}
Other statement;
Dr. Kuppusamy P
if….else statements
/* Example of if else statement */
public class SampleTest
{
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
int age = sin.nextInt();
if(age > 40)
{
System.out.println("Eligible to Covid Vaccinate")
}
else
{
System.out.println(" Not Eligible to Covid Vaccinate ");
}
}
}
Dr. Kuppusamy P
Conditional Operator
• Conditional operator is an one line alternative for if else condition.
• The result of conditional statements can be stored in to a variable
• syntax :
condition? true statements : false statements;
• Example:
String result = age>=40 ? ”eligible” : ”not eligible”;
Dr. Kuppusamy P
Cascading (Nested) if….else
Syntax:
if (condition1)
{
statement - 1
}
.
.
.
else if(condition)
{
statement - n
}
else
{
default statement
}
other statement
Dr. Kuppusamy P
Cascading if….else Example
public class CascasdeTest
{
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
int month = sin.nextInt();
if(month == 12 || month == 1 || month == 2)
System.out.println("Winter");
else if(month == 3 || month == 4 || month == 5)
System.out.println("Spring");
else if(month == 6 || month == 7 || month == 8)
System.out.println("Summer");
else if(month == 9 || month == 10 || month == 11)
System.out.println("Autumn");
else
System.out.println("invalid month");
}
} Dr. Kuppusamy P
Switch Case
Syntax:
switch (expression)
{
case value-1:
case-1 block
break;
case value-2:
case-2 block
break;
default:
default block
break;
}
statement-x;
Dr. Kuppusamy P
• Testing for multiple conditions
Switch Case
public class SwitchCaseTest
{
public static void main(String args[])
{
Scanner sin=new Scanner(System.in);
int weekday = sin.nextInt();
switch(weekday) {
case 1: System.out.println(“Sunday");
break;
case 2: System.out.println(“Monday");
break;
case 3: System.out.println(“Tuesday");
break;
case 4: System.out.println(“Wednesday");
break;
case 5: System.out.println(“Thursday");
break;
case 6: System.out.println(“Friday");
break;
case 7: System.out.println(“Saturday");
break;
default:
System.out.println(“Invalid day"); }
}
}
Dr. Kuppusamy P
break statement
• The break statement will terminate the iteration or switch case
block during the execution of program,
• When a break statement is encountered in a loop, the loop exit
and the program continues with the statements immediately
following the loop
• When the loops are nested, the break will only terminate the
corresponding loop body
Dr. Kuppusamy P
Quiz
class QuizExample {
public static void main(String s[]) {
if( 100 > 145 ) {
System.out.println(" 100 is greater than 145 ");
}
else
System.out.println(" 145 is greater than 100 ");
}
}
Dr. Kuppusamy P
Type Casting in JAVA
• Type casting is converting a value of one primitive data type to
another type during any operation.
• Two types of casting:
• Widening Casting (automatic) - converting a smaller size data
type to a larger size data type
byte -> short -> char -> int -> long -> float -> double
• Narrowing Casting (manual) - converting a larger size data type
to a smaller size data type.
double -> float -> long -> int -> char -> short -> byte
Dr. Kuppusamy P
Widening Type Casting (automatic)
public class Typecast1
{
public static void main(String[] args)
{
int myInt =12;
double myDouble = myInt; // Automatic casting: int to double
System.out.println(myInt); // Outputs 12
System.out.println(myDouble); // Outputs 12.0
}
}
Dr. Kuppusamy P
Narrowing or Explicit Type Casting (Manual)
• Assigns a value of larger data type to a smaller data type.
• useful for incompatible data types where automatic conversion cannot be
done.
• Target data type have to be represented in ( ) next to the = sybmbol.
public class Typecast2
{
public static void main(String[] args)
{
double myDouble = 2.35
int myInt = (int) myDouble; // Manual casting: double to int
System.out.println(myDouble); // Outputs 2.35
System.out.println(myInt); // Outputs 2
}
}
Dr. Kuppusamy P
Narrowing or Explicit Type Casting (Manual)
public class Typecast3
{
public static void main(String[] args)
{
double a = 1232.35
long k= (long) a; // Manual casting
int j = (int) k;
System.out.println(a); // Outputs 1232.35
System.out.println(k); // Outputs 1232
System.out.println(j); // Outputs 1232
}
}
Dr. Kuppusamy P
String to integer
//incompatible data type for explicit type conversion
public class Typecast3
{
public static void main(String[] args)
{
String price=“34”;
int num = Integer.parseInt(price);
System.out.println(num);
}
}
Dr. Kuppusamy P
Char to integer conversion
//incompatible data type
public class Typecast3
{
public static void main(String[] args)
{
char ch = “c”;
int num = 88;
ch = num;
System.out.println(num);
}
}
Dr. Kuppusamy P
Type conversion
Dr. Kuppusamy P
References
Dr. Kuppusamy P
Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition,
2017.

More Related Content

PPTX
Java if else condition - powerpoint persentation
PPTX
Control structures in java
PPTX
NABL.pptx
PPTX
Java Methods
PPTX
Data types in java
PDF
Introduction to Java Programming
PPTX
Introduction to Database
PPTX
Online Safety, Security, Ethics, and Netiquette - Empowerment Technologies
Java if else condition - powerpoint persentation
Control structures in java
NABL.pptx
Java Methods
Data types in java
Introduction to Java Programming
Introduction to Database
Online Safety, Security, Ethics, and Netiquette - Empowerment Technologies

What's hot (20)

PPTX
Classes objects in java
PPTX
Method overloading
PPT
Java interfaces
PPTX
Control Statements in Java
PPTX
Operators in java
PPTX
Java Data Types
PPTX
Java exception handling
PPTX
Type casting in java
PPT
FUNCTIONS IN c++ PPT
PPTX
Java abstract class & abstract methods
PPTX
Control statements in java
PPT
standard template library(STL) in C++
PPTX
Packages in java
PPS
Introduction to class in java
PPT
Exception Handling in JAVA
PDF
Methods in Java
PPTX
oops concept in java | object oriented programming in java
PDF
Lesson 02 python keywords and identifiers
PPTX
Methods in java
PPS
Wrapper class
Classes objects in java
Method overloading
Java interfaces
Control Statements in Java
Operators in java
Java Data Types
Java exception handling
Type casting in java
FUNCTIONS IN c++ PPT
Java abstract class & abstract methods
Control statements in java
standard template library(STL) in C++
Packages in java
Introduction to class in java
Exception Handling in JAVA
Methods in Java
oops concept in java | object oriented programming in java
Lesson 02 python keywords and identifiers
Methods in java
Wrapper class
Ad

Similar to Java conditional statements (20)

PPTX
examples of flow controls.pptxexamples of flow controls.pptx
PPTX
JPC#8 Introduction to Java Programming
PPT
ch04-conditional-execution.ppt
PDF
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
PPTX
Decision_Makoiyuhrttouehgouesygytiuojth0ueyutwaeing.pptx
PPT
Flow of Control
PPT
conditional statements
PPTX
02 - Prepcode
PPTX
Stop that!
PPTX
Control Structures.pptx
PDF
GeeCON 2012 Bad Tests, Good Tests
PPT
3 j unit
PDF
Confitura 2012 Bad Tests, Good Tests
PPTX
Data structures
PPT
2012 JDays Bad Tests Good Tests
PDF
SOLID Java Code
PPTX
Java Flow Controls.pptxJava Flow Controls.pptxJava Flow Controls.pptx
PPTX
05. Conditional Statements
PPTX
3-Decision making and Control structures-28-04-2023.pptx
PPT
4b C switch structure .ppt
examples of flow controls.pptxexamples of flow controls.pptx
JPC#8 Introduction to Java Programming
ch04-conditional-execution.ppt
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
Decision_Makoiyuhrttouehgouesygytiuojth0ueyutwaeing.pptx
Flow of Control
conditional statements
02 - Prepcode
Stop that!
Control Structures.pptx
GeeCON 2012 Bad Tests, Good Tests
3 j unit
Confitura 2012 Bad Tests, Good Tests
Data structures
2012 JDays Bad Tests Good Tests
SOLID Java Code
Java Flow Controls.pptxJava Flow Controls.pptxJava Flow Controls.pptx
05. Conditional Statements
3-Decision making and Control structures-28-04-2023.pptx
4b C switch structure .ppt
Ad

More from Kuppusamy P (20)

PDF
Recurrent neural networks rnn
PDF
Deep learning
PDF
Image segmentation
PDF
Image enhancement
PDF
Feature detection and matching
PDF
Image processing, Noise, Noise Removal filters
PDF
Flowchart design for algorithms
PDF
Algorithm basics
PDF
Problem solving using Programming
PDF
Parts of Computer, Hardware and Software
PDF
Strings in java
PDF
Java methods or Subroutines or Functions
PDF
Java arrays
PDF
Java iterative statements
PDF
Java data types
PDF
Java introduction
PDF
Logistic regression in Machine Learning
PDF
Anomaly detection (Unsupervised Learning) in Machine Learning
PDF
Machine Learning Performance metrics for classification
PDF
Machine learning Introduction
Recurrent neural networks rnn
Deep learning
Image segmentation
Image enhancement
Feature detection and matching
Image processing, Noise, Noise Removal filters
Flowchart design for algorithms
Algorithm basics
Problem solving using Programming
Parts of Computer, Hardware and Software
Strings in java
Java methods or Subroutines or Functions
Java arrays
Java iterative statements
Java data types
Java introduction
Logistic regression in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine Learning
Machine Learning Performance metrics for classification
Machine learning Introduction

Recently uploaded (20)

PPTX
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
PPTX
202450812 BayCHI UCSC-SV 20250812 v17.pptx
PPTX
Cell Types and Its function , kingdom of life
PPTX
Institutional Correction lecture only . . .
PDF
FourierSeries-QuestionsWithAnswers(Part-A).pdf
PDF
O5-L3 Freight Transport Ops (International) V1.pdf
PDF
Microbial disease of the cardiovascular and lymphatic systems
PPTX
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
PDF
Computing-Curriculum for Schools in Ghana
PPTX
Microbial diseases, their pathogenesis and prophylaxis
PDF
Module 4: Burden of Disease Tutorial Slides S2 2025
PPTX
Cell Structure & Organelles in detailed.
PDF
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
PPTX
Pharmacology of Heart Failure /Pharmacotherapy of CHF
PDF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
PDF
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
PDF
01-Introduction-to-Information-Management.pdf
PPTX
human mycosis Human fungal infections are called human mycosis..pptx
PPTX
Presentation on HIE in infants and its manifestations
PPTX
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...
school management -TNTEU- B.Ed., Semester II Unit 1.pptx
202450812 BayCHI UCSC-SV 20250812 v17.pptx
Cell Types and Its function , kingdom of life
Institutional Correction lecture only . . .
FourierSeries-QuestionsWithAnswers(Part-A).pdf
O5-L3 Freight Transport Ops (International) V1.pdf
Microbial disease of the cardiovascular and lymphatic systems
PPT- ENG7_QUARTER1_LESSON1_WEEK1. IMAGERY -DESCRIPTIONS pptx.pptx
Computing-Curriculum for Schools in Ghana
Microbial diseases, their pathogenesis and prophylaxis
Module 4: Burden of Disease Tutorial Slides S2 2025
Cell Structure & Organelles in detailed.
ANTIBIOTICS.pptx.pdf………………… xxxxxxxxxxxxx
Pharmacology of Heart Failure /Pharmacotherapy of CHF
Black Hat USA 2025 - Micro ICS Summit - ICS/OT Threat Landscape
A GUIDE TO GENETICS FOR UNDERGRADUATE MEDICAL STUDENTS
01-Introduction-to-Information-Management.pdf
human mycosis Human fungal infections are called human mycosis..pptx
Presentation on HIE in infants and its manifestations
IMMUNITY IMMUNITY refers to protection against infection, and the immune syst...

Java conditional statements

  • 1. Conditional Statements & Type Conversion in JAVA Dr. Kuppusamy .P Associate Professor / SCOPE
  • 2. Conditional Statements • Conditional Statements executes one or set of statements based on a condition exactly once. • There are four types of Conditional Statements in java • Simple if • else..if • Nested else…if • Switch case statements Dr. Kuppusamy P
  • 3. Simple if statement • syntax : if(boolean expression) { statement–block; } Other statements; Dr. Kuppusamy P
  • 4. Simple if statement /* This is an example of simple if statement */ public class SampleTest { public static void main(String args[]) { int a = 4; int b = 20; if( a < b ) { System.out.println("This is if statement"); } } } Dr. Kuppusamy P
  • 5. if….else statement • The is an extension of simple if statement • syntax : if (Boolean expression) { True -block statements; } else { False -block statements ; } Other statement; Dr. Kuppusamy P
  • 6. if….else statements /* Example of if else statement */ public class SampleTest { public static void main(String args[]) { Scanner sin=new Scanner(System.in); int age = sin.nextInt(); if(age > 40) { System.out.println("Eligible to Covid Vaccinate") } else { System.out.println(" Not Eligible to Covid Vaccinate "); } } } Dr. Kuppusamy P
  • 7. Conditional Operator • Conditional operator is an one line alternative for if else condition. • The result of conditional statements can be stored in to a variable • syntax : condition? true statements : false statements; • Example: String result = age>=40 ? ”eligible” : ”not eligible”; Dr. Kuppusamy P
  • 8. Cascading (Nested) if….else Syntax: if (condition1) { statement - 1 } . . . else if(condition) { statement - n } else { default statement } other statement Dr. Kuppusamy P
  • 9. Cascading if….else Example public class CascasdeTest { public static void main(String args[]) { Scanner sin=new Scanner(System.in); int month = sin.nextInt(); if(month == 12 || month == 1 || month == 2) System.out.println("Winter"); else if(month == 3 || month == 4 || month == 5) System.out.println("Spring"); else if(month == 6 || month == 7 || month == 8) System.out.println("Summer"); else if(month == 9 || month == 10 || month == 11) System.out.println("Autumn"); else System.out.println("invalid month"); } } Dr. Kuppusamy P
  • 10. Switch Case Syntax: switch (expression) { case value-1: case-1 block break; case value-2: case-2 block break; default: default block break; } statement-x; Dr. Kuppusamy P • Testing for multiple conditions
  • 11. Switch Case public class SwitchCaseTest { public static void main(String args[]) { Scanner sin=new Scanner(System.in); int weekday = sin.nextInt(); switch(weekday) { case 1: System.out.println(“Sunday"); break; case 2: System.out.println(“Monday"); break; case 3: System.out.println(“Tuesday"); break; case 4: System.out.println(“Wednesday"); break; case 5: System.out.println(“Thursday"); break; case 6: System.out.println(“Friday"); break; case 7: System.out.println(“Saturday"); break; default: System.out.println(“Invalid day"); } } } Dr. Kuppusamy P
  • 12. break statement • The break statement will terminate the iteration or switch case block during the execution of program, • When a break statement is encountered in a loop, the loop exit and the program continues with the statements immediately following the loop • When the loops are nested, the break will only terminate the corresponding loop body Dr. Kuppusamy P
  • 13. Quiz class QuizExample { public static void main(String s[]) { if( 100 > 145 ) { System.out.println(" 100 is greater than 145 "); } else System.out.println(" 145 is greater than 100 "); } } Dr. Kuppusamy P
  • 14. Type Casting in JAVA • Type casting is converting a value of one primitive data type to another type during any operation. • Two types of casting: • Widening Casting (automatic) - converting a smaller size data type to a larger size data type byte -> short -> char -> int -> long -> float -> double • Narrowing Casting (manual) - converting a larger size data type to a smaller size data type. double -> float -> long -> int -> char -> short -> byte Dr. Kuppusamy P
  • 15. Widening Type Casting (automatic) public class Typecast1 { public static void main(String[] args) { int myInt =12; double myDouble = myInt; // Automatic casting: int to double System.out.println(myInt); // Outputs 12 System.out.println(myDouble); // Outputs 12.0 } } Dr. Kuppusamy P
  • 16. Narrowing or Explicit Type Casting (Manual) • Assigns a value of larger data type to a smaller data type. • useful for incompatible data types where automatic conversion cannot be done. • Target data type have to be represented in ( ) next to the = sybmbol. public class Typecast2 { public static void main(String[] args) { double myDouble = 2.35 int myInt = (int) myDouble; // Manual casting: double to int System.out.println(myDouble); // Outputs 2.35 System.out.println(myInt); // Outputs 2 } } Dr. Kuppusamy P
  • 17. Narrowing or Explicit Type Casting (Manual) public class Typecast3 { public static void main(String[] args) { double a = 1232.35 long k= (long) a; // Manual casting int j = (int) k; System.out.println(a); // Outputs 1232.35 System.out.println(k); // Outputs 1232 System.out.println(j); // Outputs 1232 } } Dr. Kuppusamy P
  • 18. String to integer //incompatible data type for explicit type conversion public class Typecast3 { public static void main(String[] args) { String price=“34”; int num = Integer.parseInt(price); System.out.println(num); } } Dr. Kuppusamy P
  • 19. Char to integer conversion //incompatible data type public class Typecast3 { public static void main(String[] args) { char ch = “c”; int num = 88; ch = num; System.out.println(num); } } Dr. Kuppusamy P
  • 21. References Dr. Kuppusamy P Herbert Schildt, “Java: The Complete Reference”, McGraw-Hill Education, Tenth edition, 2017.