SlideShare a Scribd company logo
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.
Ad

More Related Content

What's hot (20)

Operators in java
Operators in javaOperators in java
Operators in java
Then Murugeshwari
 
Control structures in java
Control structures in javaControl structures in java
Control structures in java
VINOTH R
 
Data Types & Variables in JAVA
Data Types & Variables in JAVAData Types & Variables in JAVA
Data Types & Variables in JAVA
Ankita Totala
 
Switch statements in Java
Switch statements  in JavaSwitch statements  in Java
Switch statements in Java
Jin Castor
 
Strings in Java
Strings in JavaStrings in Java
Strings in Java
Abhilash Nair
 
Operators in java
Operators in javaOperators in java
Operators in java
AbhishekMondal42
 
Classes objects in java
Classes objects in javaClasses objects in java
Classes objects in java
Madishetty Prathibha
 
constructors in java ppt
constructors in java pptconstructors in java ppt
constructors in java ppt
kunal kishore
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Abhilash Nair
 
Java Tokens
Java  TokensJava  Tokens
Java Tokens
Madishetty Prathibha
 
Constructor in java
Constructor in javaConstructor in java
Constructor in java
SIVASHANKARIRAJAN
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
Abhilash Nair
 
Java input
Java inputJava input
Java input
Jin Castor
 
Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)Basic Concepts of OOPs (Object Oriented Programming in Java)
Basic Concepts of OOPs (Object Oriented Programming in Java)
Michelle Anne Meralpis
 
Method overloading
Method overloadingMethod overloading
Method overloading
Lovely Professional University
 
Control statements in java
Control statements in javaControl statements in java
Control statements in java
Madishetty Prathibha
 
OOP java
OOP javaOOP java
OOP java
xball977
 
Arrays in Java
Arrays in JavaArrays in Java
Arrays in Java
Naz Abdalla
 
Basic concept of OOP's
Basic concept of OOP'sBasic concept of OOP's
Basic concept of OOP's
Prof. Dr. K. Adisesha
 
Methods in java
Methods in javaMethods in java
Methods in java
chauhankapil
 

Similar to Java conditional statements (20)

JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
Mahyuddin8
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
Praveen M Jigajinni
 
conditional statements
conditional statementsconditional statements
conditional statements
James Brotsos
 
02 - Prepcode
02 - Prepcode02 - Prepcode
02 - Prepcode
thewhiteafrican
 
Stop that!
Stop that!Stop that!
Stop that!
Doug Sparling
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
ssuserfb3c3e
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
3 j unit
3 j unit3 j unit
3 j unit
kishoregali
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
Data structures
Data structuresData structures
Data structures
Khalid Bana
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
SOLID Java Code
SOLID Java CodeSOLID Java Code
SOLID Java Code
Omar Bashir
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
Intro C# Book
 
3-Decision making and Control structures-28-04-2023.pptx
3-Decision making and Control structures-28-04-2023.pptx3-Decision making and Control structures-28-04-2023.pptx
3-Decision making and Control structures-28-04-2023.pptx
SrikarPrasadDonavall
 
4b C switch structure .ppt
4b C switch structure .ppt4b C switch structure .ppt
4b C switch structure .ppt
GowthamiRangaraj
 
Core java
Core javaCore java
Core java
Uday Sharma
 
02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB
Hari Christian
 
Java Programming - 03 java control flow
Java Programming - 03 java control flowJava Programming - 03 java control flow
Java Programming - 03 java control flow
Danairat Thanabodithammachari
 
JPC#8 Introduction to Java Programming
JPC#8 Introduction to Java ProgrammingJPC#8 Introduction to Java Programming
JPC#8 Introduction to Java Programming
Pathomchon Sriwilairit
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
Mahyuddin8
 
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdfLECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
LECTURE 2 MORE TYPES, METHODS, CONDITIONALS.pdf
ShashikantSathe3
 
conditional statements
conditional statementsconditional statements
conditional statements
James Brotsos
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
ssuserfb3c3e
 
GeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good TestsGeeCON 2012 Bad Tests, Good Tests
GeeCON 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
Confitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good TestsConfitura 2012 Bad Tests, Good Tests
Confitura 2012 Bad Tests, Good Tests
Tomek Kaczanowski
 
2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests2012 JDays Bad Tests Good Tests
2012 JDays Bad Tests Good Tests
Tomek Kaczanowski
 
05. Conditional Statements
05. Conditional Statements05. Conditional Statements
05. Conditional Statements
Intro C# Book
 
3-Decision making and Control structures-28-04-2023.pptx
3-Decision making and Control structures-28-04-2023.pptx3-Decision making and Control structures-28-04-2023.pptx
3-Decision making and Control structures-28-04-2023.pptx
SrikarPrasadDonavall
 
4b C switch structure .ppt
4b C switch structure .ppt4b C switch structure .ppt
4b C switch structure .ppt
GowthamiRangaraj
 
02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB02 Java Language And OOP Part II LAB
02 Java Language And OOP Part II LAB
Hari Christian
 
Ad

More from Kuppusamy P (20)

Recurrent neural networks rnn
Recurrent neural networks   rnnRecurrent neural networks   rnn
Recurrent neural networks rnn
Kuppusamy P
 
Deep learning
Deep learningDeep learning
Deep learning
Kuppusamy P
 
Image segmentation
Image segmentationImage segmentation
Image segmentation
Kuppusamy P
 
Image enhancement
Image enhancementImage enhancement
Image enhancement
Kuppusamy P
 
Feature detection and matching
Feature detection and matchingFeature detection and matching
Feature detection and matching
Kuppusamy P
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
Flowchart design for algorithms
Flowchart design for algorithmsFlowchart design for algorithms
Flowchart design for algorithms
Kuppusamy P
 
Algorithm basics
Algorithm basicsAlgorithm basics
Algorithm basics
Kuppusamy P
 
Problem solving using Programming
Problem solving using ProgrammingProblem solving using Programming
Problem solving using Programming
Kuppusamy P
 
Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software
Kuppusamy P
 
Strings in java
Strings in javaStrings in java
Strings in java
Kuppusamy P
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
Kuppusamy P
 
Java arrays
Java arraysJava arrays
Java arrays
Kuppusamy P
 
Java iterative statements
Java iterative statementsJava iterative statements
Java iterative statements
Kuppusamy P
 
Java data types
Java data typesJava data types
Java data types
Kuppusamy P
 
Java introduction
Java introductionJava introduction
Java introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
Kuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine LearningAnomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
Machine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationMachine Learning Performance metrics for classification
Machine Learning Performance metrics for classification
Kuppusamy P
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
Kuppusamy P
 
Recurrent neural networks rnn
Recurrent neural networks   rnnRecurrent neural networks   rnn
Recurrent neural networks rnn
Kuppusamy P
 
Image segmentation
Image segmentationImage segmentation
Image segmentation
Kuppusamy P
 
Image enhancement
Image enhancementImage enhancement
Image enhancement
Kuppusamy P
 
Feature detection and matching
Feature detection and matchingFeature detection and matching
Feature detection and matching
Kuppusamy P
 
Image processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filtersImage processing, Noise, Noise Removal filters
Image processing, Noise, Noise Removal filters
Kuppusamy P
 
Flowchart design for algorithms
Flowchart design for algorithmsFlowchart design for algorithms
Flowchart design for algorithms
Kuppusamy P
 
Algorithm basics
Algorithm basicsAlgorithm basics
Algorithm basics
Kuppusamy P
 
Problem solving using Programming
Problem solving using ProgrammingProblem solving using Programming
Problem solving using Programming
Kuppusamy P
 
Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software Parts of Computer, Hardware and Software
Parts of Computer, Hardware and Software
Kuppusamy P
 
Java methods or Subroutines or Functions
Java methods or Subroutines or FunctionsJava methods or Subroutines or Functions
Java methods or Subroutines or Functions
Kuppusamy P
 
Java iterative statements
Java iterative statementsJava iterative statements
Java iterative statements
Kuppusamy P
 
Java introduction
Java introductionJava introduction
Java introduction
Kuppusamy P
 
Logistic regression in Machine Learning
Logistic regression in Machine LearningLogistic regression in Machine Learning
Logistic regression in Machine Learning
Kuppusamy P
 
Anomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine LearningAnomaly detection (Unsupervised Learning) in Machine Learning
Anomaly detection (Unsupervised Learning) in Machine Learning
Kuppusamy P
 
Machine Learning Performance metrics for classification
Machine Learning Performance metrics for classificationMachine Learning Performance metrics for classification
Machine Learning Performance metrics for classification
Kuppusamy P
 
Machine learning Introduction
Machine learning IntroductionMachine learning Introduction
Machine learning Introduction
Kuppusamy P
 
Ad

Recently uploaded (20)

YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 4-30-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 4-30-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
Political History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptxPolitical History of Pala dynasty Pala Rulers NEP.pptx
Political History of Pala dynasty Pala Rulers NEP.pptx
Arya Mahila P. G. College, Banaras Hindu University, Varanasi, India.
 
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptxYSPH VMOC Special Report - Measles Outbreak  Southwest US 5-3-2025.pptx
YSPH VMOC Special Report - Measles Outbreak Southwest US 5-3-2025.pptx
Yale School of Public Health - The Virtual Medical Operations Center (VMOC)
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
Operations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdfOperations Management (Dr. Abdulfatah Salem).pdf
Operations Management (Dr. Abdulfatah Salem).pdf
Arab Academy for Science, Technology and Maritime Transport
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
SPRING FESTIVITIES - UK AND USA -
SPRING FESTIVITIES - UK AND USA            -SPRING FESTIVITIES - UK AND USA            -
SPRING FESTIVITIES - UK AND USA -
Colégio Santa Teresinha
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
World war-1(Causes & impacts at a glance) PPT by Simanchala Sarab(BABed,sem-4...
larencebapu132
 
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Niamh Lucey, Mary Dunne. Health Sciences Libraries Group (LAI). Lighting the ...
Library Association of Ireland
 
One Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learningOne Hot encoding a revolution in Machine learning
One Hot encoding a revolution in Machine learning
momer9505
 
apa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdfapa-style-referencing-visual-guide-2025.pdf
apa-style-referencing-visual-guide-2025.pdf
Ishika Ghosh
 
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
How to track Cost and Revenue using Analytic Accounts in odoo Accounting, App...
Celine George
 
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Marie Boran Special Collections Librarian Hardiman Library, University of Gal...
Library Association of Ireland
 
Geography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjectsGeography Sem II Unit 1C Correlation of Geography with other school subjects
Geography Sem II Unit 1C Correlation of Geography with other school subjects
ProfDrShaikhImran
 
How to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POSHow to Manage Opening & Closing Controls in Odoo 17 POS
How to Manage Opening & Closing Controls in Odoo 17 POS
Celine George
 
Odoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo SlidesOdoo Inventory Rules and Routes v17 - Odoo Slides
Odoo Inventory Rules and Routes v17 - Odoo Slides
Celine George
 
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulsepulse  ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
pulse ppt.pptx Types of pulse , characteristics of pulse , Alteration of pulse
sushreesangita003
 
GDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptxGDGLSPGCOER - Git and GitHub Workshop.pptx
GDGLSPGCOER - Git and GitHub Workshop.pptx
azeenhodekar
 
Social Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy StudentsSocial Problem-Unemployment .pptx notes for Physiotherapy Students
Social Problem-Unemployment .pptx notes for Physiotherapy Students
DrNidhiAgarwal
 
Quality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdfQuality Contril Analysis of Containers.pdf
Quality Contril Analysis of Containers.pdf
Dr. Bindiya Chauhan
 
Anti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptxAnti-Depressants pharmacology 1slide.pptx
Anti-Depressants pharmacology 1slide.pptx
Mayuri Chavan
 
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Phoenix – A Collaborative Renewal of Children’s and Young People’s Services C...
Library Association of Ireland
 

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.