5/17/20151. 2 programming constructs... there are several types of programming constructs in java. -...

19
03/14/22 1

Upload: kory-franklin

Post on 17-Dec-2015

236 views

Category:

Documents


0 download

TRANSCRIPT

04/18/23 1

04/18/23 2

Programming Constructs . . .

• There are several types of programming constructs in JAVA.

- If-else construct or ternary operator- while- do-while- for loops- switch statements

04/18/23 3

if - else construct . . .

• The if-else construct executes some operation based on successful completion of a condition, that returns only Boolean values. If the condition is not satisfied, then the set of instructions under ‘else’ are executed.

Begin

Result When condition is true

False

True

Result When condition is false

if(condition){

//Statements if condition is true}else{

//Statements if condition is false}

if(condition){

//Statements if condition is true}else{

//Statements if condition is false}

If (condition)

04/18/23 4

if - else construct . . .

• The if-else construct executes some operation based on successful completion of a condition, that returns only Boolean values. If the condition is not satisfied, then the set of instructions under ‘else’ are executed.

Begin

If (condition)

Begin

Begin

if(condition){

//Statements if condition is true}else{

//Statements if condition is false}

if(condition){

//Statements if condition is true}else{

//Statements if condition is false}

02/10/10 5

Example: if-else . . .

class Constructs{

public static void main(String arg[]){

int a=10;

if(a<10){

System.out.println(“a is greater than 10”);}else{

System.out.println(“a is less than 10”); }}

}

class Constructs{

public static void main(String arg[]){

int a=10;

if(a<10){

System.out.println(“a is greater than 10”);}else{

System.out.println(“a is less than 10”); }}

}

04/18/23 6

Ternary (conditional) operator . . .

The ternary operator is considered to be an alternative to the if-else construct.

test ? Pass : Fail ;

• test – the condition to be tested

• Pass - value given here is returned when the test is satisfied

• Fail – value given here is returned when the test is failed.

02/10/10 7

Example: ternary operator . . .

class Constructs{

public static void main(String arg[])

{int avg=75;char result;

result=avg>50 ? ‘A’ : ‘F’

System.out.println(result); }

}

class Constructs{

public static void main(String arg[])

{int avg=75;char result;

result=avg>50 ? ‘A’ : ‘F’

System.out.println(result); }

}

04/18/23 8

Nested if - else construct . . .

.dfssdf dsdfsdf

Begin

If (condition)

Begin

Begin

if(condition){

//Statements if condition is true}else{

//Statements if condition is false}

if(condition){

//Statements if condition is true}else{

//Statements if condition is false}

04/18/23 9

Example: Nested if-else . . .

Modify your program, so that it is able to find the grade of each student based on the average. You must use following criteria to obtain the grades.

Example 2 (cont): Store information of students Example 2 (cont): Store information of students

Range of Average Grade85 to 100 A75 to 84 B65 to 74 C55 to 64 S45 to 54 w

<45 F

02/10/10 10

While loop . . .

The while loop executes some operation repeatedly, until the condition returns false.

while(condition){

//body of the loop}

while(condition){

//body of the loop}

When the condition fails, then the loop terminates.

02/10/10 11

Example: While loop . . .

class Constructs{

public static void main(String arg[])

{int i=1;while(i<=10){

System.out.println(i);i++;

}}

}

class Constructs{

public static void main(String arg[])

{int i=1;while(i<=10){

System.out.println(i);i++;

}}

}

Write a JAVA program to display consecutive numbers from 1 to 10, using while loops.

02/10/10 12

Do-While loop . . .

The do-while loop is similar to the while loop except the condition to be evaluated is given at the end.

do{

//body of the loop

} while(condition);

do{

//body of the loop

} while(condition);

The loop is executed at least once even when the condition is false.

02/10/10 13

Example: Do-While loop . . .

class Constructs{

public static void main(String arg[])

{int i=1;do{

System.out.println(i);i++;

} while(i<=10) ;}

}

class Constructs{

public static void main(String arg[])

{int i=1;do{

System.out.println(i);i++;

} while(i<=10) ;}

}

Write a JAVA program to display consecutive numbers from 1 to 10, using do-while loops.

02/10/10 14

For loop . . .

A for loop repeats a set of statements, until a condition is satisfied. There are three major parts in a for loop namely; initialization, test (condition) and the expression.

for(initialization; test; expression){

//Body of the for loop}

for(initialization; test; expression){

//Body of the for loop}

• Initialization- A variable is initialized to a value.

• Test – This is the condition of the loop and it returns a Boolean value.

• Expression – The expression is evaluated every time that the loop is executed.

02/10/10 15

Example: For loop . . .

class Constructs{

public static void main(String arg[])

{for(int i=0;i<10;i++){

System.out.println(i+1);}

}}

class Constructs{

public static void main(String arg[])

{for(int i=0;i<10;i++){

System.out.println(i+1);}

}}

Write a JAVA program to display consecutive numbers from 1 to 10, using for loops.

02/10/10 16

Switch statement . . .

The switch statement dispatches different parts of the code based on the value of a single variable or expression. The value of expression is compared with each of the literal values in the case statements. If they match, the corresponding instructions are executed. Otherwise an optional default statement is executed.

switch(test){

case value1://Statement1break;

…………………………………………………………………………………………………….…………………………………………………………………………………………………….

default://Statement

}

switch(test){

case value1://Statement1break;

…………………………………………………………………………………………………….…………………………………………………………………………………………………….

default://Statement

}

Test is a variable or an expression that evaluates to byte, char or int. It cannot be float, double, long, string or any other object

02/10/10 17

Example: Switch Statement . . .

int num=2;switch(num){

case 1:System.out.println("January");break;

case 2:System.out.println(“February");break;

………………………………………………………………………………………………………………………………………………………………….………………………………………………………………………………………………………………………………………………………………….

default:System.out.println("Invalid

Number");

}

int num=2;switch(num){

case 1:System.out.println("January");break;

case 2:System.out.println(“February");break;

………………………………………………………………………………………………………………………………………………………………….………………………………………………………………………………………………………………………………………………………………….

default:System.out.println("Invalid

Number");

}

Write a JAVA program to display the name of the month when the number is given.

02/10/10 18

Break & Continue. . .

• The break keyword halts the execution of current loop and forces control out of the loop.

• The continue keyword starts the next iteration of the loop rather than halting the execution of entire loop .

for(int i=0;i<10;i++){

if(i==5)

continue;

System.out.println(i);}

for(int i=0;i<10;i++){

if(i==5)

continue;

System.out.println(i);}

for(int i=0;i<10;i++){

if(i==5)break;

System.out.println(i);}

for(int i=0;i<10;i++){

if(i==5)break;

System.out.println(i);}

02/10/10 19

Exercises . . .

Write JAVA programs to display following outputs. You must use programming constructs.