






















































Study with the several resources on Docsity
Earn points by helping other students or get them with a premium plan
Prepare for your exams
Study with the several resources on Docsity
Earn points to download
Earn points by helping other students or get them with a premium plan
lab configuring vlans and trunking by using packet tracer
Typology: Summaries
1 / 62
This page cannot be seen from the preview
Don't miss anything!























































Chapter- Three
Topics we will cover in chapter- Three
statements
Switch statement
Do while loop
Continue
o if Statement
o if-else statements
o if...else if Statement
if Statement
In Java, an if statement is a conditional statement that runs a different set of
statements,
if statement is depending on whether an expression is true or false.
Syntax:
if ( expression ){
statement; //code to be executed if the condition is true
}
executed.
skipped.
Example #2: if Statement
A program that checks if a number is even
public class TestClass {
public static void main(String[] args) {
int num = 16;
if (num % 2 == 0){
System.out.println(num + " is even");
if-else statements
An if-else statement adds an additional element to a basic if
statement:
The else statement is to specify a block of code to be executed, if
the condition in the if statement is false.
The basic syntax is
if ( condition ){
//code to be executed if the condition is true
}
else {
//code to be executed if the condition is false
}
if...else if Statement
Used to evaluate more than one conditions at the same time.
Multi selection enables the developer to determine the actions
that have to be accomplished in certain conditions by imposing a
requisite.
A common programming construct that is based upon a
sequence of nested ifs is the if-else-if ladder****.
if...else if Statement…
General Syntax:
if(condition1){
//code to be executed if the condition1 is true
}
else if (condition2 ){
//code to be executed if condition2 is true }
else if( condition ){
//code to be executed if condition3 is true
}
...
else {
//code to be executed if all the above conditions are false
}
Example #
A program that accepts mark and displays letter grade
int score = 76;
char grade;
if (score >= 90) {
grade = 'A';
}
else if (score >= 80){
grade = 'B';
}
else if (score >= 70){
grade = 'C';
}
else if (score >= 60){
grade = 'D';
}
else{
grade = 'F';
}
System.out.println("Grade = " + grade);
Switch Statement
The Java switch statement is used to perform different actions
based on different conditions.
It also works with enumerated types , the String class, and a
few special classes that wrap certain primitive types:
Character,
Byte,
Short, and
Integer.
How Switch Statement is works?
The switch expression (condition) is evaluated once and the value of the
expression is compared with the values of each case.
If there is a match, the associated block of code is executed.
If no matching case clause is found, the program looks for the optional default
clause.
The optional break statement associated with each case label ensures
that the program breaks out of switch once the matched statement is
executed and continues execution at the statement following switch.
If break is omitted, the program continues execution at the next statement in the switch
statement.
Example #1: Switch statement
public class SwitchStatement {
public static void main(String[] args) {
// TODO code application logic here
int inDay=4;
String day;
switch (inDay){
case 1: day="Sunday";
break;
case 2: day="Monday";
break;
case 3: day="Tuesday";
break;
case 4: day="Wednesday";
break;
case 5: day="Thrusday";
break;
case 6: day="Friday";
break;
case 7: day="saturday"; Output: select day is: Wednesday
break;
default: day="Invalid day"; }
System.out.println("select day is:"+ day);
}
}
Example #2:
public class SwitchWoutBreak {
public static void main(String[] args) {
// TODO code application logic here
int inDay=4;
String day;
switch (inDay){
case 1: day="Sunday";
case 2: day="Monday“;
case 3: day="Tuesday";
break;
case 4: day="Wednesday;
case 5: day=" Thursday ";
break;
case 6: day="Friday";
break;
case 7: day="Saturday";
break;
default: day="Invalid day";
}
System.out.println("select day is:"+ day);
}
} Output: select day is: Thursday