Docsity

wireless and computer network, Summaries of Computer Science

Partial preview of the text

Download wireless and computer network and more Summaries Computer Science in PDF only on Docsity!

Chapter- Three

Decision and Repetition Statements

Topics we will cover in chapter- Three

  • (^) Overview of Java

statements

  • (^) If statement

Switch statement

  • (^) For loop
    • (^) While loop

Do while loop

  • (^) Break

Continue

Overview of Java statements …

  • (^) create decisions using different forms of if...else statement.

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

}

  • (^) In the above syntax,
    • (^) the if statement evaluates the test expression inside parenthesis.
    • (^) If test expression is evaluated to true (nonzero) , statements inside the body of if is

executed.

  • (^) If test expression is evaluated to false (0) , statements inside the body of if are

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

  • A program that accepts week number and displays week name.

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

Loop

In Java, a loop repeatedly

executes the same set of

instructions until a

termination condition is

met.

Java provides three

iteration statements as

shown in fig: