SlideShare a Scribd company logo
1 of 39
Control Statements
Language Fundamental
Command Line Arguments
1
www.infoviaan.com
Command Line Argument
Animal
Human
public class CommandLineArgument
{
public static void main(String args[])
{
System.out.println(“Hello ” +args[0]);
}
}
Compile : - javac CommandLineArgument.java
Run:- java CommandLineArgument info viaan Lecture
class args[0] args[1] args[2]
Output:- Hello info
Run:- java CommandLineArgument “info viaan” Lecture
class args[0] args[1]
2
www.infoviaan.com
Command Line Argument
Animal
Human
public class NoOfArg
{
public static void main(String ar[])
{
System.out.println(“You passed total no. of
arguments: ”+ar.length);
}
}
Compile : - javac NoOfArg.java
Run:- java NoOfArg Hello I am good
1 2 3 4
Output:- You passed total no. of arguments: 4
3
www.infoviaan.com
Control Statements
Animal
• Control the flow of program
Control
Statements
Decision Making
Statements
Looping Statements
if else
Ladder if else
Nested if else
Switch case
do...while
for
while
for each
4
www.infoviaan.com
Program - if else
• An if statement can be followed by an optional else
statement, which executes when the Boolean expression is
false.
public class TestIfElse {
public static void main(String[] args) {
int age = Integer.parseInt(args[0]);
if (age > 17)
System.out.println("You are Eligibe
for Vote :)");
else
System.out.println("Sorry! You Are
not eligible :(");
}
}
5
www.infoviaan.com
Program - Nested if..else
public class Nested {
public static void main(String[] args) {
int a, b, c;
a = 113; b = 88; c = 95;
if (a > b) {
if (a > c) {
System.out.println("A is biggest " + a);
} else {
System.out.println("C is boggest " + c);
}
} else {
if (b > c)
System.out.println("B is biggest " + b);
else
System.out.println("C is Biggest " + c);
}
}
} 6
www.infoviaan.com
Switch statement
• A switch statement allows a variable to be tested for
equality against a list of values.
• Each value is called a case, and the variable being
switched on is checked for each case.
7
www.infoviaan.com
Program - Switch
public class Switch {
public static void main(String[] args) {
int i = 2;
switch (i) {
case 1: System.out.println(“Addition");
break;
case 2:System.out.println(“Subtraction");
break;
case 3:System.out.println(“Mulplication");
break;
default: System.out.println(“Not
Available");
}
}
}
8
www.infoviaan.com
Program - Switch with
String (Java 5)public class Switch {
public static void main(String[] args) {
String c = “Ram";
switch (c) {
case “Ram":System.out.println(“Lord");
break;
case "Shani": System.out.println(“Dev");
break;
case “RamDev“: System.out.println(“BaBa");
break;
default: System.out.println(“People");
}
}
}
9
www.infoviaan.com
Loop
• while - allows code to be executed repeatedly based on
a given Boolean condition
• do...while - executes a block of code at least once, and
then repeatedly executes the block, or not, depending
on a given boolean condition at the end of the block
• for – it specifying iteration, which allows code to be
executed repeatedly
• for each (java 5) - used to access each successive value
in a collection of values. Arrays and Collections
10
www.infoviaan.com
While Loop
11
www.infoviaan.com
Program - While Loop
public class TestWhile {
public static void main(String[] args) {
int i = 1;
while (i <= 5) {
System.out.println("Hello "+i);
i++;
}
}
}
12
www.infoviaan.com
do While Loop
13
www.infoviaan.com
Program – do While Loop
public class TestDo {
public static void main(String[] args) {
int i = 1;
do {
System.out.println("Hello " +
i);
i++;
} while (i <= 5);
}
}
14
www.infoviaan.com
for Loop
15
www.infoviaan.com
Program - for Loop
public class TestForLoop {
public static void main(String[] args) {
int n = 4;
int fact = 1;
for (int i = 1; i <= n; i++) {
fact = fact * i;
}
System.out.println("Factorial Value is = "
+ fact);
}
}
16
www.infoviaan.com
Loop
While(condition)
{
}
For( ; conditionalstatement;
)
{
}
17
www.infoviaan.com
Loop
18
www.infoviaan.com
Nested Loop
• A nested loop is a (inner) loop that appears in the
loop body of another (outer) loop.
• The inner or outerloop can be any type: while, do
while, or for.
• For example, the inner loop can be a while loop while
an outer loop can be a for loop. Of course, they can
be the same kind of loops too.
19
www.infoviaan.com
Programs - Nested Loop
public class TestNestedLoop {
public static void main(String[] args) {
for (int i = 1; i <= 5; i++) {
for (int j = 1; j <= i; j++) {
System.out.print("*");
}
System.out.println();
}
}
}
20
www.infoviaan.com
For each Loop(java 5)
• Access value directly from array or collection.
for(type variable : array){
//statement
}
Program : -
public class TestForEach{
public static void main(String[] args) {
int ar[]= {10,20,30,40,50};
for (int i : ar) {
System.out.println(i);
}
}
} 21
www.infoviaan.com
Array
Animal
Human
• An array is a container object that holds a fixed
number of values of a single type.
• It’s reference data type, which contain 2 byte
memory for holding address
• Advantages:
• Contiguous memory allocation
• Collection of similar data types
• Limitations:
• Can’t store dissimilar data types
• Not resizable
22
www.infoviaan.com
Declare an Array
Animal
Human
1. int x[] = {10,20,30,40,50}; //memory allocate at
compile time
2. int x[] = new int[5];
3. int x[] = new int[]{10,20,30,40,50}; //memory
allocate at runtime
• Which one is better to use?
Knows the elements - 3
Don't know the elements -2
23
www.infoviaan.com
One-D Array
Animal
Human
int a[] = new int[9];
a[0] = 10;
a[1] = 20;
…..
a[8] = 90;
or
int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90};
int size = a.length;
a[0]
a[1]
a[2]
a[3]
a[4]
a[5]
a[6]
a[7]
a[8]
1000
1000
10
20
30
40
50
60
70
80
90
4 B
2 B
24
www.infoviaan.com
One-D Array
Animal
char c[] = new char[7];
c[0] = ‘A’;
c[1] = ‘V’;
…
Or
char c[] = {‘A’, ’V’, ’R’, ’T’, ’E’, ’T’, ’P’};
String names[] = new String[5];
name[0] = “Ram”;
name[1] = “Lakhan”;
…
Or
String names[] = {“Ram”, ”Lakhan”, ”Sita”, “Hanuman”,
“Ramayana”};
25
www.infoviaan.com
Programs – One_D_Array
public class TestOneDArray {
public static void main(String[] args) {
char ar[]= {'X','Y','Z','A','@'};
for (char c : ar) {
System.out.println(c);
}
}
}
26
www.infoviaan.com
Copy an Array
Animal
• To copy an arrays value to another use System.ArrayCopy()
method, which take 5 arguments.
System.ArrayCopy(copyFrom, 2, copyTo, 0, 5);
Start index
Start index
No. of elements
27
www.infoviaan.com
Programs – Copy Array
public class TestArrayCopy {
public static void main(String[] args) {
char cf[] = { 'X', 'Y', 'Z', 'A', 'W' };
char ct[] = new char[4];
System.arraycopy(cf, 1, ct, 0, 4);
System.out.println(ct);
}
}
28
www.infoviaan.com
2D array
Animal
Human
29
www.infoviaan.com
Array of Array/ 2D array
Animal
Human
Two types
• Square Array
• Zagged Array
1. int x[][]={
{10,20,30},
{10},
{10,20}
};
2. int x[][]=new int[3][];
3. int x[][]=new int[][] {
{10,20,30},
{10},
{10,20}
}; 30
www.infoviaan.com
2D array
Animal
Human
31
www.infoviaan.com
2D array
Animal
Human
32
www.infoviaan.com
Programs - TwoDArray
public class TestTwoD {
public static void main(String[] args) {
int x[][] = { { 12, 23, 67, 89 },
{ 89, 78, 67 },
{ 12, 34, 78 } };
int i=0;
while(i<x.length) {
int j=0;
while(j<x[i].length) {
System.out.print(x[i][j]+" ");
j++;
}
System.out.println();
i++;
}
}
}
33
www.infoviaan.com
3D array
Animal
Human
34
www.infoviaan.com
QA
1. What is difference between while and do..while loop?
2. Switch case work with which of the followings.
Byte,short,int,long,float,double,String
3. Difference between for in and for loop.
4. What happens when we forget to put a break statement in a
case clause of a switch?
5. Which method is given parameter via command line
arguments?
6. How many arguments can be passed to main()?
7. Which loop will execute the body of loop even when condition
controlling the loop is initially false?
8. Which of the jump statements can skip processing remainder
of code in its body for a particular iteration?
9. Why java uses String array as command Line Argument?
35
www.infoviaan.com
QA
1. What is the index of Brighton in the following array?
String[] skiResorts = { "Whistler Blackcomb", "Squaw Valley",
"Brighton", "Snowmass", "Sun Valley", "Taos" };
2. Write an expression that refers to the string Brighton within
the array.
3. What is the value of the expression skiResorts.length?
4. What is the index of the last item in the array?
5. What is the value of the expression skiResorts[4]?
36
www.infoviaan.com
Module Project 1
Ross is an event organizer. He has received data regarding the
participation of employees in two different events. Some employees
have participated in only one event and others have participated in
both events. Ross now needs to count the number of employees who
have taken part in both events. The records received by Ross consist
of employee ids, which are unique. Write a program that accepts the
employee ids participating in each event (the first line relates to the
first event and the second line relates to the second event). The
program should print the number of common employee ids in both
the events.
Suppose the following input is given to the program, where each line
represents a different event:
1001,1002,1003,1004,1005
1106,1008,1005,1003,1016,1017,1112
Now the common employee ids are 1003 and 1005, so the program
should give the output as:
2
www.infoviaan.com
Module Project 2
Write a java code to find the distance from Mumbai to major
cities of India.
Hint: Create an String array of major cities and integer array of
distances. User gives the city name and the same is searched in
the respective array and displays result.
www.infoviaan.com
Get in Touch
Thank You
www.infoviaan.com
39
www.infoviaan.com

More Related Content

What's hot

Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread SynchronizationBenj Del Mundo
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and OperatorsSunil OS
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java languageHareem Naz
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentationkunal kishore
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statementsKuppusamy P
 
Exception Handling
Exception HandlingException Handling
Exception HandlingSunil OS
 
Python functions
Python functionsPython functions
Python functionsAliyamanasa
 
Java package
Java packageJava package
Java packageCS_GDRCST
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaJorge Vásquez
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variablesPushpendra Tyagi
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionSvetlin Nakov
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling pptJavabynataraJ
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesPrabu U
 

What's hot (20)

Java Fundamentals
Java FundamentalsJava Fundamentals
Java Fundamentals
 
Java Thread Synchronization
Java Thread SynchronizationJava Thread Synchronization
Java Thread Synchronization
 
JAVA Variables and Operators
JAVA Variables and OperatorsJAVA Variables and Operators
JAVA Variables and Operators
 
Arrays in java language
Arrays in java languageArrays in java language
Arrays in java language
 
Operators in java presentation
Operators in java presentationOperators in java presentation
Operators in java presentation
 
Java conditional statements
Java conditional statementsJava conditional statements
Java conditional statements
 
07 java collection
07 java collection07 java collection
07 java collection
 
Exception Handling
Exception HandlingException Handling
Exception Handling
 
What's new in Java 11
What's new in Java 11What's new in Java 11
What's new in Java 11
 
Java Arrays
Java ArraysJava Arrays
Java Arrays
 
Python functions
Python functionsPython functions
Python functions
 
Java package
Java packageJava package
Java package
 
Exploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in ScalaExploring ZIO Prelude: The game changer for typeclasses in Scala
Exploring ZIO Prelude: The game changer for typeclasses in Scala
 
Files in java
Files in javaFiles in java
Files in java
 
Vectors in Java
Vectors in JavaVectors in Java
Vectors in Java
 
Wrapper class
Wrapper classWrapper class
Wrapper class
 
Java basics and java variables
Java basics and java variablesJava basics and java variables
Java basics and java variables
 
Java Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type ConversionJava Foundations: Data Types and Type Conversion
Java Foundations: Data Types and Type Conversion
 
Java exception handling ppt
Java exception handling pptJava exception handling ppt
Java exception handling ppt
 
String Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and InterfacesString Handling, Inheritance, Packages and Interfaces
String Handling, Inheritance, Packages and Interfaces
 

Similar to Java Language fundamental

Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Ayes Chinmay
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic SyntaxAdil Jafri
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentalsHCMUTE
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iiiNiraj Bharambe
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and ClassesIntro C# Book
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)Chhom Karath
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECSreedhar Chowdam
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lecturesMSohaib24
 
Computer java programs
Computer java programsComputer java programs
Computer java programsADITYA BHARTI
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introductioncaswenson
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIjagriti srivastava
 

Similar to Java Language fundamental (20)

Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
Internet and Web Technology (CLASS-16) [Basic Elements of Java Program] | NIC...
 
Java 5 and 6 New Features
Java 5 and 6 New FeaturesJava 5 and 6 New Features
Java 5 and 6 New Features
 
4java Basic Syntax
4java Basic Syntax4java Basic Syntax
4java Basic Syntax
 
Pattern Matching in Java 14
Pattern Matching in Java 14Pattern Matching in Java 14
Pattern Matching in Java 14
 
Java fundamentals
Java fundamentalsJava fundamentals
Java fundamentals
 
Core java pract_sem iii
Core java pract_sem iiiCore java pract_sem iii
Core java pract_sem iii
 
05. Java Loops Methods and Classes
05. Java Loops Methods and Classes05. Java Loops Methods and Classes
05. Java Loops Methods and Classes
 
Introduction to Java Programming Part 2
Introduction to Java Programming Part 2Introduction to Java Programming Part 2
Introduction to Java Programming Part 2
 
CH1 ARRAY (1).pptx
CH1 ARRAY (1).pptxCH1 ARRAY (1).pptx
CH1 ARRAY (1).pptx
 
Chapter i(introduction to java)
Chapter i(introduction to java)Chapter i(introduction to java)
Chapter i(introduction to java)
 
What is new in Java 8
What is new in Java 8What is new in Java 8
What is new in Java 8
 
Java
JavaJava
Java
 
Jvm a brief introduction
Jvm  a brief introductionJvm  a brief introduction
Jvm a brief introduction
 
Java Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPRECJava Notes by C. Sreedhar, GPREC
Java Notes by C. Sreedhar, GPREC
 
Java Notes
Java Notes Java Notes
Java Notes
 
Core java
Core javaCore java
Core java
 
object oriented programming java lectures
object oriented programming java lecturesobject oriented programming java lectures
object oriented programming java lectures
 
Computer java programs
Computer java programsComputer java programs
Computer java programs
 
Learning Java 1 – Introduction
Learning Java 1 – IntroductionLearning Java 1 – Introduction
Learning Java 1 – Introduction
 
Basic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time APIBasic java, java collection Framework and Date Time API
Basic java, java collection Framework and Date Time API
 

Recently uploaded

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsTechSoup
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxJisc
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdfQucHHunhnh
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Association for Project Management
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the ClassroomPooky Knightsmith
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxVishalSingh1417
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfciinovamais
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...Poonam Aher Patil
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsMebane Rash
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfAdmir Softic
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701bronxfugly43
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxRamakrishna Reddy Bijjam
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and ModificationsMJDuyan
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024Elizabeth Walsh
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdfQucHHunhnh
 

Recently uploaded (20)

Introduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The BasicsIntroduction to Nonprofit Accounting: The Basics
Introduction to Nonprofit Accounting: The Basics
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
1029 - Danh muc Sach Giao Khoa 10 . pdf
1029 -  Danh muc Sach Giao Khoa 10 . pdf1029 -  Danh muc Sach Giao Khoa 10 . pdf
1029 - Danh muc Sach Giao Khoa 10 . pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...Making communications land - Are they received and understood as intended? we...
Making communications land - Are they received and understood as intended? we...
 
Fostering Friendships - Enhancing Social Bonds in the Classroom
Fostering Friendships - Enhancing Social Bonds  in the ClassroomFostering Friendships - Enhancing Social Bonds  in the Classroom
Fostering Friendships - Enhancing Social Bonds in the Classroom
 
Unit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptxUnit-IV- Pharma. Marketing Channels.pptx
Unit-IV- Pharma. Marketing Channels.pptx
 
Activity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdfActivity 01 - Artificial Culture (1).pdf
Activity 01 - Artificial Culture (1).pdf
 
General Principles of Intellectual Property: Concepts of Intellectual Proper...
General Principles of Intellectual Property: Concepts of Intellectual  Proper...General Principles of Intellectual Property: Concepts of Intellectual  Proper...
General Principles of Intellectual Property: Concepts of Intellectual Proper...
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
On National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan FellowsOn National Teacher Day, meet the 2024-25 Kenan Fellows
On National Teacher Day, meet the 2024-25 Kenan Fellows
 
Key note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdfKey note speaker Neum_Admir Softic_ENG.pdf
Key note speaker Neum_Admir Softic_ENG.pdf
 
ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701ComPTIA Overview | Comptia Security+ Book SY0-701
ComPTIA Overview | Comptia Security+ Book SY0-701
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Python Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docxPython Notes for mca i year students osmania university.docx
Python Notes for mca i year students osmania university.docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Understanding Accommodations and Modifications
Understanding  Accommodations and ModificationsUnderstanding  Accommodations and Modifications
Understanding Accommodations and Modifications
 
FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024FSB Advising Checklist - Orientation 2024
FSB Advising Checklist - Orientation 2024
 
1029-Danh muc Sach Giao Khoa khoi 6.pdf
1029-Danh muc Sach Giao Khoa khoi  6.pdf1029-Danh muc Sach Giao Khoa khoi  6.pdf
1029-Danh muc Sach Giao Khoa khoi 6.pdf
 

Java Language fundamental

  • 1. Control Statements Language Fundamental Command Line Arguments 1 www.infoviaan.com
  • 2. Command Line Argument Animal Human public class CommandLineArgument { public static void main(String args[]) { System.out.println(“Hello ” +args[0]); } } Compile : - javac CommandLineArgument.java Run:- java CommandLineArgument info viaan Lecture class args[0] args[1] args[2] Output:- Hello info Run:- java CommandLineArgument “info viaan” Lecture class args[0] args[1] 2 www.infoviaan.com
  • 3. Command Line Argument Animal Human public class NoOfArg { public static void main(String ar[]) { System.out.println(“You passed total no. of arguments: ”+ar.length); } } Compile : - javac NoOfArg.java Run:- java NoOfArg Hello I am good 1 2 3 4 Output:- You passed total no. of arguments: 4 3 www.infoviaan.com
  • 4. Control Statements Animal • Control the flow of program Control Statements Decision Making Statements Looping Statements if else Ladder if else Nested if else Switch case do...while for while for each 4 www.infoviaan.com
  • 5. Program - if else • An if statement can be followed by an optional else statement, which executes when the Boolean expression is false. public class TestIfElse { public static void main(String[] args) { int age = Integer.parseInt(args[0]); if (age > 17) System.out.println("You are Eligibe for Vote :)"); else System.out.println("Sorry! You Are not eligible :("); } } 5 www.infoviaan.com
  • 6. Program - Nested if..else public class Nested { public static void main(String[] args) { int a, b, c; a = 113; b = 88; c = 95; if (a > b) { if (a > c) { System.out.println("A is biggest " + a); } else { System.out.println("C is boggest " + c); } } else { if (b > c) System.out.println("B is biggest " + b); else System.out.println("C is Biggest " + c); } } } 6 www.infoviaan.com
  • 7. Switch statement • A switch statement allows a variable to be tested for equality against a list of values. • Each value is called a case, and the variable being switched on is checked for each case. 7 www.infoviaan.com
  • 8. Program - Switch public class Switch { public static void main(String[] args) { int i = 2; switch (i) { case 1: System.out.println(“Addition"); break; case 2:System.out.println(“Subtraction"); break; case 3:System.out.println(“Mulplication"); break; default: System.out.println(“Not Available"); } } } 8 www.infoviaan.com
  • 9. Program - Switch with String (Java 5)public class Switch { public static void main(String[] args) { String c = “Ram"; switch (c) { case “Ram":System.out.println(“Lord"); break; case "Shani": System.out.println(“Dev"); break; case “RamDev“: System.out.println(“BaBa"); break; default: System.out.println(“People"); } } } 9 www.infoviaan.com
  • 10. Loop • while - allows code to be executed repeatedly based on a given Boolean condition • do...while - executes a block of code at least once, and then repeatedly executes the block, or not, depending on a given boolean condition at the end of the block • for – it specifying iteration, which allows code to be executed repeatedly • for each (java 5) - used to access each successive value in a collection of values. Arrays and Collections 10 www.infoviaan.com
  • 12. Program - While Loop public class TestWhile { public static void main(String[] args) { int i = 1; while (i <= 5) { System.out.println("Hello "+i); i++; } } } 12 www.infoviaan.com
  • 14. Program – do While Loop public class TestDo { public static void main(String[] args) { int i = 1; do { System.out.println("Hello " + i); i++; } while (i <= 5); } } 14 www.infoviaan.com
  • 16. Program - for Loop public class TestForLoop { public static void main(String[] args) { int n = 4; int fact = 1; for (int i = 1; i <= n; i++) { fact = fact * i; } System.out.println("Factorial Value is = " + fact); } } 16 www.infoviaan.com
  • 19. Nested Loop • A nested loop is a (inner) loop that appears in the loop body of another (outer) loop. • The inner or outerloop can be any type: while, do while, or for. • For example, the inner loop can be a while loop while an outer loop can be a for loop. Of course, they can be the same kind of loops too. 19 www.infoviaan.com
  • 20. Programs - Nested Loop public class TestNestedLoop { public static void main(String[] args) { for (int i = 1; i <= 5; i++) { for (int j = 1; j <= i; j++) { System.out.print("*"); } System.out.println(); } } } 20 www.infoviaan.com
  • 21. For each Loop(java 5) • Access value directly from array or collection. for(type variable : array){ //statement } Program : - public class TestForEach{ public static void main(String[] args) { int ar[]= {10,20,30,40,50}; for (int i : ar) { System.out.println(i); } } } 21 www.infoviaan.com
  • 22. Array Animal Human • An array is a container object that holds a fixed number of values of a single type. • It’s reference data type, which contain 2 byte memory for holding address • Advantages: • Contiguous memory allocation • Collection of similar data types • Limitations: • Can’t store dissimilar data types • Not resizable 22 www.infoviaan.com
  • 23. Declare an Array Animal Human 1. int x[] = {10,20,30,40,50}; //memory allocate at compile time 2. int x[] = new int[5]; 3. int x[] = new int[]{10,20,30,40,50}; //memory allocate at runtime • Which one is better to use? Knows the elements - 3 Don't know the elements -2 23 www.infoviaan.com
  • 24. One-D Array Animal Human int a[] = new int[9]; a[0] = 10; a[1] = 20; ….. a[8] = 90; or int a[] = {10, 20, 30, 40, 50, 60, 70, 80, 90}; int size = a.length; a[0] a[1] a[2] a[3] a[4] a[5] a[6] a[7] a[8] 1000 1000 10 20 30 40 50 60 70 80 90 4 B 2 B 24 www.infoviaan.com
  • 25. One-D Array Animal char c[] = new char[7]; c[0] = ‘A’; c[1] = ‘V’; … Or char c[] = {‘A’, ’V’, ’R’, ’T’, ’E’, ’T’, ’P’}; String names[] = new String[5]; name[0] = “Ram”; name[1] = “Lakhan”; … Or String names[] = {“Ram”, ”Lakhan”, ”Sita”, “Hanuman”, “Ramayana”}; 25 www.infoviaan.com
  • 26. Programs – One_D_Array public class TestOneDArray { public static void main(String[] args) { char ar[]= {'X','Y','Z','A','@'}; for (char c : ar) { System.out.println(c); } } } 26 www.infoviaan.com
  • 27. Copy an Array Animal • To copy an arrays value to another use System.ArrayCopy() method, which take 5 arguments. System.ArrayCopy(copyFrom, 2, copyTo, 0, 5); Start index Start index No. of elements 27 www.infoviaan.com
  • 28. Programs – Copy Array public class TestArrayCopy { public static void main(String[] args) { char cf[] = { 'X', 'Y', 'Z', 'A', 'W' }; char ct[] = new char[4]; System.arraycopy(cf, 1, ct, 0, 4); System.out.println(ct); } } 28 www.infoviaan.com
  • 30. Array of Array/ 2D array Animal Human Two types • Square Array • Zagged Array 1. int x[][]={ {10,20,30}, {10}, {10,20} }; 2. int x[][]=new int[3][]; 3. int x[][]=new int[][] { {10,20,30}, {10}, {10,20} }; 30 www.infoviaan.com
  • 33. Programs - TwoDArray public class TestTwoD { public static void main(String[] args) { int x[][] = { { 12, 23, 67, 89 }, { 89, 78, 67 }, { 12, 34, 78 } }; int i=0; while(i<x.length) { int j=0; while(j<x[i].length) { System.out.print(x[i][j]+" "); j++; } System.out.println(); i++; } } } 33 www.infoviaan.com
  • 35. QA 1. What is difference between while and do..while loop? 2. Switch case work with which of the followings. Byte,short,int,long,float,double,String 3. Difference between for in and for loop. 4. What happens when we forget to put a break statement in a case clause of a switch? 5. Which method is given parameter via command line arguments? 6. How many arguments can be passed to main()? 7. Which loop will execute the body of loop even when condition controlling the loop is initially false? 8. Which of the jump statements can skip processing remainder of code in its body for a particular iteration? 9. Why java uses String array as command Line Argument? 35 www.infoviaan.com
  • 36. QA 1. What is the index of Brighton in the following array? String[] skiResorts = { "Whistler Blackcomb", "Squaw Valley", "Brighton", "Snowmass", "Sun Valley", "Taos" }; 2. Write an expression that refers to the string Brighton within the array. 3. What is the value of the expression skiResorts.length? 4. What is the index of the last item in the array? 5. What is the value of the expression skiResorts[4]? 36 www.infoviaan.com
  • 37. Module Project 1 Ross is an event organizer. He has received data regarding the participation of employees in two different events. Some employees have participated in only one event and others have participated in both events. Ross now needs to count the number of employees who have taken part in both events. The records received by Ross consist of employee ids, which are unique. Write a program that accepts the employee ids participating in each event (the first line relates to the first event and the second line relates to the second event). The program should print the number of common employee ids in both the events. Suppose the following input is given to the program, where each line represents a different event: 1001,1002,1003,1004,1005 1106,1008,1005,1003,1016,1017,1112 Now the common employee ids are 1003 and 1005, so the program should give the output as: 2 www.infoviaan.com
  • 38. Module Project 2 Write a java code to find the distance from Mumbai to major cities of India. Hint: Create an String array of major cities and integer array of distances. User gives the city name and the same is searched in the respective array and displays result. www.infoviaan.com
  • 39. Get in Touch Thank You www.infoviaan.com 39 www.infoviaan.com