0% found this document useful (0 votes)
2 views

Java-class-3

The document explains Java keywords, which are reserved words that cannot be used as variable or class names. It details control flow statements in Java, including decision-making, loop, and jump statements, with examples of if statements, switch statements, and loops. Additionally, it highlights the structure and usage of these statements to control the flow of Java programs.

Uploaded by

Candy Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
2 views

Java-class-3

The document explains Java keywords, which are reserved words that cannot be used as variable or class names. It details control flow statements in Java, including decision-making, loop, and jump statements, with examples of if statements, switch statements, and loops. Additionally, it highlights the structure and usage of these statements to control the flow of Java programs.

Uploaded by

Candy Man
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 13

Java keywords are also known as reserved words.

Keywords are
particular words that act as a key to a code. These are predefined
words by Java so they cannot be used as a variable or object name or
class name.

abstract continue for new switch


synchroniz
assert*** default goto* package
ed
boolean do if private this
break double implementsprotected throw
byte else import public throws
case enum**** instanceof return transient
catch extends int short try
char final interface static void
class finally long strictfp** volatile
const* float native super while
Java compiler executes the code from top to bottom. The statements in
the code are executed according to the order in which they appear.
However, Java provides statements that can be used to control the flow
of Java code. Such statements are called control flow statements. It is one
of the fundamental features of Java, which provides a smooth flow of
program.
Java provides three types of control flow statements.

Decision Making statements


if statements
switch statement

Loop statements
do while loop
while loop
for loop
for-each loop

Jump statements
break statement
continue statement
1) Simple if statement:

if(condition) {
statement 1; //executes when condition is
true
}

public class Student {


public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y > 20) {
System.out.println("x + y is greater than 20
");
}
}
}
2) if-else statement

if(condition) {
statement 1; //executes when condition is
true
}
else{
statement 2; //executes when condition is
false
}

public class Student {


public static void main(String[] args) {
int x = 10;
int y = 12;
if(x+y < 10) {
System.out.println("x + y is less than 10"
);
} else {
System.out.println("x + y is greater than 20
");
} } }
3) if-else-if ladder:

if(condition 1) {
statement 1; //executes when condition 1 i
s true
}
else if(condition 2) {
statement 2; //executes when condition 2 i
s true
}
else {
statement 2; //executes when all the condi
tions are false
}
public class Student {
public static void main(String[] args) {
String city = "Delhi";
if(city == "Meerut") {
System.out.println("city is meerut");
}else if (city == "Noida") {
System.out.println("city is noida");
}else if(city == "Agra") {
System.out.println("city is agra");
}else {
System.out.println(city);
}
}
}
4. Nested if-statement

if(condition 1) {
statement 1; //executes when condition 1 i
s true
if(condition 2) {
statement 2; //executes when condition 2 i
s true
}
else{
statement 2; //executes when condition 2 i
s false
}
}
public class Student {
public static void main(String[] args) {
String address = "Delhi, India";

if(address.endsWith("India")) {
if(address.contains("Meerut")) {
System.out.println("Your city is Meerut");

}else if(address.contains("Noida")) {
System.out.println("Your city is Noida");
}else {
System.out.println(address.split(",")[0]);
}
}else {
System.out.println("You are not living in I
ndia");
}
}
}
Switch Statement:
In Java, Switch statements are similar to if-else-if statements. The switch
statement contains multiple blocks of code called cases and a single case is
executed based on the variable which is being switched. The switch
statement is easier to use instead of if-else-if statements. It also enhances
the readability of the program.

Points to be noted about switch statement:


•The case variables can be int, short, byte, char, or enumeration. String type
is also supported since version 7 of Java
•Cases cannot be duplicate
•Default statement is executed when any of the case doesn't match the
value of expression. It is optional.
•Break statement terminates the switch block when the condition is satisfied.
It is optional, if not used, next case is executed.
•While using switch statements, we must notice that the case expression will
be of the same type as the variable. However, it will also be a constant
value.
switch (expression){
case value1:
statement1;
break;
.
.
.
case valueN:
statementN;
break;
default:
default statement;
}
public class Student {
public static void main(String[] args) {
int num = 2;
switch (num){
case 0:
System.out.println("number is 0");
break;
case 1:
System.out.println("number is 1");
break;
default:
System.out.println(num);
}
}
}
for loop
while loop
do-while loop

You might also like