Recent Comments widget by VAMPIRE

Thursday, June 30, 2011

lesson 3-Decisions

3 : Decisions

The if statement is

used to implement a

decision.

The if statement has

two parts: a test and a

body.

The body of the if

consists of

statements.

if (condition)

{

statement1;

statement2;

. . .

}

Decisions

To implement

alternative conditions,

use the if/else

statement.

if (condition)

{

statement;

. . .

}

else

statement;

. . .

}

Decisions

if (condition)

statement;

else if (condition)

statement;

else if (condition)

statement;

else

statement;

Relational Operators

Equality: = = (Equal)

! = (Not Equal)

Relational: >

>=

<
<=
Logical: && (AND)
| | (OR)
! (NOT)
Example
int x = 10, y = 20;
if (x = = 10 && y > = 15)

{….}

public class Compare {

public static void main

(String[] args) {

int num1 =

Integer.parseInt(args[0]);

int num2 =

Integer.parseInt(args[1]);

if (num1 > num2)

System.out.println

(num1+“is greater than”

+num2);

else if (num2 > num1)

System.out.println(num2

+ “ is greater than ” +

num1);

else

System.out.println(num2

+ “ is equal to ” + num1);

}

}

- Complie c:> javac

Compare.java

- Run c:> java Compare 10

20

Selection Operator

condition ? expression1 :

expression2

if (a > b)

z = a;

else

z = b;

==> z = (a > b) ? a : b;

switch statement

switch (expression) {

case value1 : statements;

break;

case value2 : statements;

break;

case …

default: statements;

}

Remember!

The test cases of the

switch statement must

be constant, and they

must be integers.

You can not use a

switch to branch on

floating-point or string

values.

Every branch of the

switch is terminated

by a break instruction.

For Example :

int num;

...

switch (num) {

case 1 : System.out.print

(“one”); break;

case 2 : System.out.print

(“two”); break;

case 3 : System.out.print

(“three”); break;

case 4 : System.out.print

(“four”); break;

case 5 : System.out.print

(“five”); break;

default: System.out.println

(“Invalid number”);

}



Iteration

while statement

while (expression) {

statement;

}

Example:

i = 1;

while (i <= 5) {
System.out.println(“Hello ”
+ i);
i++;
}
do-while statement
do {
statement
} while (expression)
Example:
i = 1;
do {
System.out.println(“Hello ”
+ i);
i++;
} while (i > 5);

for statement

for (expression1;

expression2; expression3)

statement;

Example:

for (i = 1; i <= 5; i++)

System.out.println(“Hello ”

+ i);

The break and continue

statements

The break statement

can be used to exit a

while, for, or do loop.

i = 1;

while (i <= 10) {

System.out.println(“Hello ”

+ i);

if (i = = 6)

break;

i++;

}

The continue

statement is another

goto-like statement.

It jumps to the end of

the current iteration of

the loop.

i = 1;

while (i <= 10) {

System.out.println

(“Hello ” + i);

if (i = = 6) {

i++;

continue; // jump to

the end of the loop

body

i++;

}

Exercise

Write a program to

read the rank order and

five integers then

determine, and print

the largest and

smallest integers in

the format shown

below.. Do not use

pre-defined Math

methods.

Number inputs: 27, 83, 15,

94, 25

The smallest number is 15

The largest number is 94

0 comments:

Post a Comment

Twitter Delicious Facebook Digg Stumbleupon Favorites More

 
Design by Free WordPress Themes | Bloggerized by Lasantha - Premium Blogger Themes | Sweet Tomatoes Printable Coupons