SlideShare a Scribd company logo
1 of 23
Download to read offline
LANGUAGE BASICS PART3
JAVA
BY AMR ELGHADBAN
JAVA :Language Basics part3
EXPRESSIONS
▸ the following expression gives different results, depending on
whether you perform the addition or the division operation first:
x + y / 100 // ambiguous
▸ to make the previous expression unambiguous, you could write
the following: using balanced parenthesis: ( and ).
(x + y) / 100 // unambiguous, recommended
x + (y / 100) // unambiguous, recommended
JAVA :Language Basics part3
BLOCKS
▸ A block is a group of zero or more statements between
balanced braces and can be used anywhere a single
statement is allowed. The following example illustrates the
use of blocks:
class BlockDemo {
public static void main(String[] args) {
boolean condition = true;
if (condition) { // begin block 1
System.out.println("Condition is true.");
} // end block one
else { // begin block 2
System.out.println("Condition is false.");
} // end block 2
}
}
JAVA :Language Basics part3
THE IF-THEN AND IF-THEN-ELSE STATEMENTS
▸ The if-then statement is the most basic of all the control flow statements. It
tells your program to execute a certain section of code only if a particular test
evaluates to true:
void applyBrakes() {
// the "if" clause: bicycle must be moving
if (isMoving){
// the "then" clause: decrease current speed
currentSpeed--;
}
}
———————————-
void applyBrakes() {
// same as above, but without braces
if (isMoving)
currentSpeed--;
}
▸ Deciding when to omit the braces is a matter of personal taste.
JAVA :Language Basics part3
THE IF-THEN AND IF-THEN-ELSE STATEMENTS
▸ The if-then-else statement provides a secondary path
of execution when an "if" clause evaluates to false.
void applyBrakes(){ 

if (isMoving) {
currentSpeed--;
} else {
System.err.println("The bicycle has already
stopped!");
}
}
JAVA :Language Basics part3
THE IF-THEN AND IF-THEN-ELSE STATEMENTS
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
JAVA :Language Basics part3
THE IF-THEN AND IF-THEN-ELSE STATEMENTS
class IfElseDemo {
public static void main(String[] args) {
int testscore = 76;
char grade;
if (testscore >= 90) {
grade = 'A';
} else if (testscore >= 80) {
grade = 'B';
} else if (testscore >= 70) {
grade = 'C';
} else if (testscore >= 60) {
grade = 'D';
} else {
grade = 'F';
}
System.out.println("Grade = " + grade);
}
}
JAVA :Language Basics part3
THE SWITCH STATEMENT
▸ A switch works with the byte, short, char, and int primitive data types.
▸ The body of a switch statement is known as a switch block.
▸ A statement in the switch block can be labeled with one or more case or
default labels.
▸ The switch statement evaluates its expression, then executes all
statements that follow the matching case label.
▸ Another point of interest is the break statement. Each break statement
terminates the enclosing switch statement.
▸ The break statements are necessary because without them, statements in
switch blocks fall through: All statements after the matching case label
are executed in sequence, regardless of the expression of subsequent
case labels, until a break statement is encountered.
JAVA :Language Basics part3
▸ public class SwitchDemo {
public static void main(String[] args) {
int month = 8;
String monthString;
switch (month) {
case 1:
monthString = “January";
break;
case 2:
monthString = “February";
break;
case 3:
monthString = “March";
break;
JAVA :Language Basics part3
▸ case 4:
monthString = "April";
break;
case 5:
monthString = “May”;
break;
case 6:
monthString = "June";
break;
case 7:
monthString = "July";
break;
JAVA :Language Basics part3
case 8:
monthString = "August";
break;
case 9:
monthString = “September";
break;
case 10:
monthString = "October";
break;
case 11:
monthString = "November";
break;
JAVA :Language Basics part3
case 12:
monthString = "December";
break;
default:
monthString = "Invalid month";
break;
}
System.out.println(monthString);
}
} // end of class
In this case, August is printed to standard output.
JAVA :Language Basics part3
‣ The following code example
shows how a statement can have
multiple case labels.
‣ The code example calculates the
number of days in a particular
month:
JAVA :Language Basics part3
class SwitchDemo2 {
public static void main(String[] args) {
int month = 2;
int year = 2000;
int numDays = 0;
switch (month) {
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
numDays = 31;
break;
case 4: case 6: 

case 9: case 11:
numDays = 30;
break;
case 2:

if ( ((year % 4 == 0) && !(year % 100 == 0)) 

|| (year % 400 == 0) )
numDays = 29;
else
numDays = 28;
break;

default: 

System.out.println("Invalid month."); 

break;

}//end of switch block
System.out.println("Number of Days = “ + numDays); 

} // end of main method 

}// end of class
JAVA :Language Basics part3
USING STRINGS IN SWITCH STATEMENT.
▸ In Java SE 7 and later, you can use a String object in the
switch statement's expression.
case "january":
monthNumber = 1;
break;
▸ The String in the switch expression is compared with
the expressions associated with each case label as if the
String.equals method were being used.
▸ Note: This example checks if the expression in the switch
statement is null. Ensure that the expression in any
switch statement is not null to prevent a
NullPointerException from being thrown.
JAVA :Language Basics part3
THE WHILE AND DO-WHILE STATEMENTS
▸ InThe while statement continually executes a block of
statements while a particular condition is true. Its syntax
can be expressed as:
while (expression) {
statement(s)
}
JAVA :Language Basics part3
THE WHILE AND DO-WHILE STATEMENTS
▸ The while statement evaluates expression, which must
return a boolean value. If the expression evaluates to
true, the while statement executes the statement(s) in
the while block.
▸ The while statement continues testing the expression and
executing its block until the expression evaluates to
false.
JAVA :Language Basics part3
THE WHILE AND DO-WHILE STATEMENTS
▸ Using the while statement to print the values from 1
through 10 can be accomplished as in the following
WhileDemo program:
class WhileDemo {
public static void main(String[] args){
int count = 1;
while (count < 11) {
System.out.println("Count is: " + count);
count++;
}
}
}
JAVA :Language Basics part3
THE WHILE AND DO-WHILE STATEMENTS
▸ You can implement an infinite loop using the while statement as follows:
while (true){
// your code goes here
}
▸ The Java programming language also provides a do-while statement, which
can be expressed as follows:
do {
statement(s)
} while (expression);
‣ The difference between do-while and while is that do-while evaluates its
expression at the bottom of the loop instead of the top. Therefore, the
statements within the do block are always executed at least once.
int count = 12;
do {
System.out.println("Count is: " + count);
count++;
} while (count < 11);
JAVA :Language Basics part3
THE FOR STATEMENT “FOR LOOP”
▸ The for statement provides a compact way to iterate over a range of
values.
▸ the way in which it repeatedly loops until a particular condition is
satisfied. The general form of the for statement can be expressed as
follows:
for (initialization; termination; increment) {
statement(s)
}
‣ When using this version of the for statement, keep in mind that:
The initialization expression initializes the loop; it's executed once, as the
loop begins.
When the termination expression evaluates to false, the loop terminates.
The increment expression is invoked after each iteration through the loop,
it is perfectly acceptable for this expression to increment or decrement a
value.
JAVA :Language Basics part3
THE FOR STATEMENT “FOR LOOP”
▸ Print the numbers 1 through 10 to standard output:
class ForDemo {
public static void main(String[] args){
for(int count=1; count<11; count++){
System.out.println("Count is: " + i);
}
}
}
——————————
‣ // infinite loop
for ( ; ; ) {
// your code goes here
}
——————————
‣ The for statement also has another form designed for iteration
int[] numbers =
{1,2,3,4,5,6,7,8,9,10};
for (int item : numbers){
System.out.println("Count is: " + item);
}
JAVA :Language Basics part3
BRANCHING STATEMENTS
▸ The break Statement
▸ You can also use an break to terminate a for, while, or do-while loop
▸ The continue Statement
▸ The continue statement skips the current iteration of a for, while , or do-while loop.
if (searchMe.charAt(i) != ‘p')
continue;
▸ 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;
THANKS
WISH YOU A WONDERFUL DAY
▸ Skype : amr_elghadban
▸ Email :amr.elghadban@gmail.com
▸ Phone : (+20)1098558500
▸ Fb/amr.elghadban
▸ Linkedin/amr_elghadban

More Related Content

What's hot

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAAiman Hud
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)heoff
 
C++ control structure
C++ control structureC++ control structure
C++ control structurebluejayjunior
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...ssuserd6b1fd
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Adam Mukharil Bachtiar
 
C++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlC++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlAAKASH KUMAR
 
nuts and bolts of c++
nuts and bolts of c++nuts and bolts of c++
nuts and bolts of c++guestfb6ada
 
Jumping statements
Jumping statementsJumping statements
Jumping statementsSuneel Dogra
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5patcha535
 
15646254 c-balaguruswamy-solved-programs
15646254 c-balaguruswamy-solved-programs15646254 c-balaguruswamy-solved-programs
15646254 c-balaguruswamy-solved-programspremrings
 
Introduction to writing algorithms
Introduction to writing algorithmsIntroduction to writing algorithms
Introduction to writing algorithms Krishna Chaytaniah
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Anshul Vinayak
 
Eff Plsql
Eff PlsqlEff Plsql
Eff Plsqlafa reg
 

What's hot (20)

POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Introduction to C#
Introduction to C#Introduction to C#
Introduction to C#
 
Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)Final project powerpoint template (fndprg) (1)
Final project powerpoint template (fndprg) (1)
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Control statements
Control statementsControl statements
Control statements
 
C++ control structure
C++ control structureC++ control structure
C++ control structure
 
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
Notes for GNU Octave - Numerical Programming - for Students - 02 of 02 by aru...
 
Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)Algorithm and Programming (Branching Structure)
Algorithm and Programming (Branching Structure)
 
C++ programming Unit 5 flow of control
C++ programming Unit 5 flow of controlC++ programming Unit 5 flow of control
C++ programming Unit 5 flow of control
 
nuts and bolts of c++
nuts and bolts of c++nuts and bolts of c++
nuts and bolts of c++
 
Jumping statements
Jumping statementsJumping statements
Jumping statements
 
Lab # 3
Lab # 3Lab # 3
Lab # 3
 
Chapter 13.1.5
Chapter 13.1.5Chapter 13.1.5
Chapter 13.1.5
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
15646254 c-balaguruswamy-solved-programs
15646254 c-balaguruswamy-solved-programs15646254 c-balaguruswamy-solved-programs
15646254 c-balaguruswamy-solved-programs
 
Introduction to writing algorithms
Introduction to writing algorithmsIntroduction to writing algorithms
Introduction to writing algorithms
 
Code Smells and Its type (With Example)
Code Smells and Its type (With Example)Code Smells and Its type (With Example)
Code Smells and Its type (With Example)
 
Decision making and looping
Decision making and loopingDecision making and looping
Decision making and looping
 
Eff Plsql
Eff PlsqlEff Plsql
Eff Plsql
 
Program control statements in c#
Program control statements in c#Program control statements in c#
Program control statements in c#
 

Viewers also liked (20)

10- java language basics part4
10- java language basics part410- java language basics part4
10- java language basics part4
 
8- java language basics part2
8- java language basics part28- java language basics part2
8- java language basics part2
 
7-Java Language Basics Part1
7-Java Language Basics Part17-Java Language Basics Part1
7-Java Language Basics Part1
 
1-oop java-object
1-oop java-object1-oop java-object
1-oop java-object
 
0-oop java-intro
0-oop java-intro0-oop java-intro
0-oop java-intro
 
3-oop java-inheritance
3-oop java-inheritance3-oop java-inheritance
3-oop java-inheritance
 
Core java online training
Core java online trainingCore java online training
Core java online training
 
Yaazli International AngularJS 5 Training
Yaazli International AngularJS 5 TrainingYaazli International AngularJS 5 Training
Yaazli International AngularJS 5 Training
 
09events
09events09events
09events
 
Java quick reference v2
Java quick reference v2Java quick reference v2
Java quick reference v2
 
Exception handling in java
Exception handling in java Exception handling in java
Exception handling in java
 
Savr
SavrSavr
Savr
 
Yaazli International Hibernate Training
Yaazli International Hibernate TrainingYaazli International Hibernate Training
Yaazli International Hibernate Training
 
Non ieee dot net projects list
Non  ieee dot net projects list Non  ieee dot net projects list
Non ieee dot net projects list
 
For Loops and Variables in Java
For Loops and Variables in JavaFor Loops and Variables in Java
For Loops and Variables in Java
 
02basics
02basics02basics
02basics
 
Yaazli International Spring Training
Yaazli International Spring Training Yaazli International Spring Training
Yaazli International Spring Training
 
Yaazli International Web Project Workshop
Yaazli International Web Project WorkshopYaazli International Web Project Workshop
Yaazli International Web Project Workshop
 
Java Exception handling
Java Exception handlingJava Exception handling
Java Exception handling
 
Toolbarexample
ToolbarexampleToolbarexample
Toolbarexample
 

Similar to 9-java language basics part3

Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrayssshhzap
 
control statements
control statementscontrol statements
control statementsAzeem Sultan
 
Programming basics
Programming basicsProgramming basics
Programming basics246paa
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptxeaglesniper008
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptSanjjaayyy
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programmingnmahi96
 
Gnu octave help book 02 of 02
Gnu octave help book 02 of 02Gnu octave help book 02 of 02
Gnu octave help book 02 of 02Arun Umrao
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa Thapa
 
11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.ppt11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.pptAnjali127411
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc csKALAISELVI P
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chapthuhiendtk4
 

Similar to 9-java language basics part3 (20)

Chapter 4 flow control structures and arrays
Chapter 4 flow control structures and arraysChapter 4 flow control structures and arrays
Chapter 4 flow control structures and arrays
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
control statements
control statementscontrol statements
control statements
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
Programming basics
Programming basicsProgramming basics
Programming basics
 
computer programming Control Statements.pptx
computer programming Control Statements.pptxcomputer programming Control Statements.pptx
computer programming Control Statements.pptx
 
CONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.pptCONTROLSTRUCTURES.ppt
CONTROLSTRUCTURES.ppt
 
Control statements-Computer programming
Control statements-Computer programmingControl statements-Computer programming
Control statements-Computer programming
 
Gnu octave help book 02 of 02
Gnu octave help book 02 of 02Gnu octave help book 02 of 02
Gnu octave help book 02 of 02
 
00_Introduction to Java.ppt
00_Introduction to Java.ppt00_Introduction to Java.ppt
00_Introduction to Java.ppt
 
CHAPTER 5
CHAPTER 5CHAPTER 5
CHAPTER 5
 
Bikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptxBikalpa_Thapa_Python_Programming_(Basics).pptx
Bikalpa_Thapa_Python_Programming_(Basics).pptx
 
Java chapter 5
Java chapter 5Java chapter 5
Java chapter 5
 
11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.ppt11-ScriptsAndConditionals.ppt
11-ScriptsAndConditionals.ppt
 
Java Basics 1.pptx
Java Basics 1.pptxJava Basics 1.pptx
Java Basics 1.pptx
 
Python unit 2 M.sc cs
Python unit 2 M.sc csPython unit 2 M.sc cs
Python unit 2 M.sc cs
 
C Programming Unit-2
C Programming Unit-2C Programming Unit-2
C Programming Unit-2
 
Lecture 9- Control Structures 1
Lecture 9- Control Structures 1Lecture 9- Control Structures 1
Lecture 9- Control Structures 1
 
1584503386 1st chap
1584503386 1st chap1584503386 1st chap
1584503386 1st chap
 
Groovy closures
Groovy closuresGroovy closures
Groovy closures
 

More from Amr Elghadban (AmrAngry) (9)

Code detox
Code detoxCode detox
Code detox
 
08 objective-c session 8
08  objective-c session 808  objective-c session 8
08 objective-c session 8
 
07 objective-c session 7
07  objective-c session 707  objective-c session 7
07 objective-c session 7
 
05 objective-c session 5
05  objective-c session 505  objective-c session 5
05 objective-c session 5
 
04 objective-c session 4
04  objective-c session 404  objective-c session 4
04 objective-c session 4
 
03 objective-c session 3
03  objective-c session 303  objective-c session 3
03 objective-c session 3
 
02 objective-c session 2
02  objective-c session 202  objective-c session 2
02 objective-c session 2
 
01 objective-c session 1
01  objective-c session 101  objective-c session 1
01 objective-c session 1
 
00 intro ios
00 intro ios00 intro ios
00 intro ios
 

Recently uploaded

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking MenDelhi Call girls
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountPuma Security, LLC
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxKatpro Technologies
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure servicePooja Nehwal
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024BookNet Canada
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...HostedbyConfluent
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationSafe Software
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticscarlostorres15106
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdfhans926745
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreternaman860154
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptxHampshireHUG
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)Gabriella Davis
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Alan Dix
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking MenDelhi Call girls
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024Scott Keck-Warren
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersThousandEyes
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsEnterprise Knowledge
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?XfilesPro
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsMemoori
 

Recently uploaded (20)

08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men08448380779 Call Girls In Civil Lines Women Seeking Men
08448380779 Call Girls In Civil Lines Women Seeking Men
 
Breaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path MountBreaking the Kubernetes Kill Chain: Host Path Mount
Breaking the Kubernetes Kill Chain: Host Path Mount
 
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptxFactors to Consider When Choosing Accounts Payable Services Providers.pptx
Factors to Consider When Choosing Accounts Payable Services Providers.pptx
 
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure serviceWhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
WhatsApp 9892124323 ✓Call Girls In Kalyan ( Mumbai ) secure service
 
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
#StandardsGoals for 2024: What’s new for BISAC - Tech Forum 2024
 
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
Transforming Data Streams with Kafka Connect: An Introduction to Single Messa...
 
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry InnovationBeyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
Beyond Boundaries: Leveraging No-Code Solutions for Industry Innovation
 
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmaticsKotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
Kotlin Multiplatform & Compose Multiplatform - Starter kit for pragmatics
 
[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf[2024]Digital Global Overview Report 2024 Meltwater.pdf
[2024]Digital Global Overview Report 2024 Meltwater.pdf
 
Presentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreterPresentation on how to chat with PDF using ChatGPT code interpreter
Presentation on how to chat with PDF using ChatGPT code interpreter
 
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
04-2024-HHUG-Sales-and-Marketing-Alignment.pptx
 
A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)A Domino Admins Adventures (Engage 2024)
A Domino Admins Adventures (Engage 2024)
 
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...Swan(sea) Song – personal research during my six years at Swansea ... and bey...
Swan(sea) Song – personal research during my six years at Swansea ... and bey...
 
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
08448380779 Call Girls In Diplomatic Enclave Women Seeking Men
 
SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024SQL Database Design For Developers at php[tek] 2024
SQL Database Design For Developers at php[tek] 2024
 
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for PartnersEnhancing Worker Digital Experience: A Hands-on Workshop for Partners
Enhancing Worker Digital Experience: A Hands-on Workshop for Partners
 
IAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI SolutionsIAC 2024 - IA Fast Track to Search Focused AI Solutions
IAC 2024 - IA Fast Track to Search Focused AI Solutions
 
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
Neo4j - How KGs are shaping the future of Generative AI at AWS Summit London ...
 
How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?How to Remove Document Management Hurdles with X-Docs?
How to Remove Document Management Hurdles with X-Docs?
 
AI as an Interface for Commercial Buildings
AI as an Interface for Commercial BuildingsAI as an Interface for Commercial Buildings
AI as an Interface for Commercial Buildings
 

9-java language basics part3

  • 2. JAVA :Language Basics part3 EXPRESSIONS ▸ the following expression gives different results, depending on whether you perform the addition or the division operation first: x + y / 100 // ambiguous ▸ to make the previous expression unambiguous, you could write the following: using balanced parenthesis: ( and ). (x + y) / 100 // unambiguous, recommended x + (y / 100) // unambiguous, recommended
  • 3. JAVA :Language Basics part3 BLOCKS ▸ A block is a group of zero or more statements between balanced braces and can be used anywhere a single statement is allowed. The following example illustrates the use of blocks: class BlockDemo { public static void main(String[] args) { boolean condition = true; if (condition) { // begin block 1 System.out.println("Condition is true."); } // end block one else { // begin block 2 System.out.println("Condition is false."); } // end block 2 } }
  • 4. JAVA :Language Basics part3 THE IF-THEN AND IF-THEN-ELSE STATEMENTS ▸ The if-then statement is the most basic of all the control flow statements. It tells your program to execute a certain section of code only if a particular test evaluates to true: void applyBrakes() { // the "if" clause: bicycle must be moving if (isMoving){ // the "then" clause: decrease current speed currentSpeed--; } } ———————————- void applyBrakes() { // same as above, but without braces if (isMoving) currentSpeed--; } ▸ Deciding when to omit the braces is a matter of personal taste.
  • 5. JAVA :Language Basics part3 THE IF-THEN AND IF-THEN-ELSE STATEMENTS ▸ The if-then-else statement provides a secondary path of execution when an "if" clause evaluates to false. void applyBrakes(){ 
 if (isMoving) { currentSpeed--; } else { System.err.println("The bicycle has already stopped!"); } }
  • 6. JAVA :Language Basics part3 THE IF-THEN AND IF-THEN-ELSE STATEMENTS class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
  • 7. JAVA :Language Basics part3 THE IF-THEN AND IF-THEN-ELSE STATEMENTS class IfElseDemo { public static void main(String[] args) { int testscore = 76; char grade; if (testscore >= 90) { grade = 'A'; } else if (testscore >= 80) { grade = 'B'; } else if (testscore >= 70) { grade = 'C'; } else if (testscore >= 60) { grade = 'D'; } else { grade = 'F'; } System.out.println("Grade = " + grade); } }
  • 8. JAVA :Language Basics part3 THE SWITCH STATEMENT ▸ A switch works with the byte, short, char, and int primitive data types. ▸ The body of a switch statement is known as a switch block. ▸ A statement in the switch block can be labeled with one or more case or default labels. ▸ The switch statement evaluates its expression, then executes all statements that follow the matching case label. ▸ Another point of interest is the break statement. Each break statement terminates the enclosing switch statement. ▸ The break statements are necessary because without them, statements in switch blocks fall through: All statements after the matching case label are executed in sequence, regardless of the expression of subsequent case labels, until a break statement is encountered.
  • 9. JAVA :Language Basics part3 ▸ public class SwitchDemo { public static void main(String[] args) { int month = 8; String monthString; switch (month) { case 1: monthString = “January"; break; case 2: monthString = “February"; break; case 3: monthString = “March"; break;
  • 10. JAVA :Language Basics part3 ▸ case 4: monthString = "April"; break; case 5: monthString = “May”; break; case 6: monthString = "June"; break; case 7: monthString = "July"; break;
  • 11. JAVA :Language Basics part3 case 8: monthString = "August"; break; case 9: monthString = “September"; break; case 10: monthString = "October"; break; case 11: monthString = "November"; break;
  • 12. JAVA :Language Basics part3 case 12: monthString = "December"; break; default: monthString = "Invalid month"; break; } System.out.println(monthString); } } // end of class In this case, August is printed to standard output.
  • 13. JAVA :Language Basics part3 ‣ The following code example shows how a statement can have multiple case labels. ‣ The code example calculates the number of days in a particular month:
  • 14. JAVA :Language Basics part3 class SwitchDemo2 { public static void main(String[] args) { int month = 2; int year = 2000; int numDays = 0; switch (month) { case 1: case 3: case 5: case 7: case 8: case 10: case 12: numDays = 31; break; case 4: case 6: 
 case 9: case 11: numDays = 30; break; case 2:
 if ( ((year % 4 == 0) && !(year % 100 == 0)) 
 || (year % 400 == 0) ) numDays = 29; else numDays = 28; break;
 default: 
 System.out.println("Invalid month."); 
 break;
 }//end of switch block System.out.println("Number of Days = “ + numDays); 
 } // end of main method 
 }// end of class
  • 15. JAVA :Language Basics part3 USING STRINGS IN SWITCH STATEMENT. ▸ In Java SE 7 and later, you can use a String object in the switch statement's expression. case "january": monthNumber = 1; break; ▸ The String in the switch expression is compared with the expressions associated with each case label as if the String.equals method were being used. ▸ Note: This example checks if the expression in the switch statement is null. Ensure that the expression in any switch statement is not null to prevent a NullPointerException from being thrown.
  • 16. JAVA :Language Basics part3 THE WHILE AND DO-WHILE STATEMENTS ▸ InThe while statement continually executes a block of statements while a particular condition is true. Its syntax can be expressed as: while (expression) { statement(s) }
  • 17. JAVA :Language Basics part3 THE WHILE AND DO-WHILE STATEMENTS ▸ The while statement evaluates expression, which must return a boolean value. If the expression evaluates to true, the while statement executes the statement(s) in the while block. ▸ The while statement continues testing the expression and executing its block until the expression evaluates to false.
  • 18. JAVA :Language Basics part3 THE WHILE AND DO-WHILE STATEMENTS ▸ Using the while statement to print the values from 1 through 10 can be accomplished as in the following WhileDemo program: class WhileDemo { public static void main(String[] args){ int count = 1; while (count < 11) { System.out.println("Count is: " + count); count++; } } }
  • 19. JAVA :Language Basics part3 THE WHILE AND DO-WHILE STATEMENTS ▸ You can implement an infinite loop using the while statement as follows: while (true){ // your code goes here } ▸ The Java programming language also provides a do-while statement, which can be expressed as follows: do { statement(s) } while (expression); ‣ The difference between do-while and while is that do-while evaluates its expression at the bottom of the loop instead of the top. Therefore, the statements within the do block are always executed at least once. int count = 12; do { System.out.println("Count is: " + count); count++; } while (count < 11);
  • 20. JAVA :Language Basics part3 THE FOR STATEMENT “FOR LOOP” ▸ The for statement provides a compact way to iterate over a range of values. ▸ the way in which it repeatedly loops until a particular condition is satisfied. The general form of the for statement can be expressed as follows: for (initialization; termination; increment) { statement(s) } ‣ When using this version of the for statement, keep in mind that: The initialization expression initializes the loop; it's executed once, as the loop begins. When the termination expression evaluates to false, the loop terminates. The increment expression is invoked after each iteration through the loop, it is perfectly acceptable for this expression to increment or decrement a value.
  • 21. JAVA :Language Basics part3 THE FOR STATEMENT “FOR LOOP” ▸ Print the numbers 1 through 10 to standard output: class ForDemo { public static void main(String[] args){ for(int count=1; count<11; count++){ System.out.println("Count is: " + i); } } } —————————— ‣ // infinite loop for ( ; ; ) { // your code goes here } —————————— ‣ The for statement also has another form designed for iteration int[] numbers = {1,2,3,4,5,6,7,8,9,10}; for (int item : numbers){ System.out.println("Count is: " + item); }
  • 22. JAVA :Language Basics part3 BRANCHING STATEMENTS ▸ The break Statement ▸ You can also use an break to terminate a for, while, or do-while loop ▸ The continue Statement ▸ The continue statement skips the current iteration of a for, while , or do-while loop. if (searchMe.charAt(i) != ‘p') continue; ▸ 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;
  • 23. THANKS WISH YOU A WONDERFUL DAY ▸ Skype : amr_elghadban ▸ Email :amr.elghadban@gmail.com ▸ Phone : (+20)1098558500 ▸ Fb/amr.elghadban ▸ Linkedin/amr_elghadban