0% found this document useful (0 votes)
11 views7 pages

New Microsoft Word Document

The document discusses Java code examples using if-else statements, relational operators, and loops. It provides code to calculate the area of a circle given a radius, a quiz addition program that tests user input, and examples of if statements that assign a value or increase pay based on a condition. It also covers body mass index calculation using a multi-way if-else statement and examples of while and for loops to iterate through code.

Uploaded by

WanAmir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
11 views7 pages

New Microsoft Word Document

The document discusses Java code examples using if-else statements, relational operators, and loops. It provides code to calculate the area of a circle given a radius, a quiz addition program that tests user input, and examples of if statements that assign a value or increase pay based on a condition. It also covers body mass index calculation using a multi-way if-else statement and examples of while and for loops to iterate through code.

Uploaded by

WanAmir
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 7

Chapter 3: Selections (ms 97)

****

Listing 2.2

If (radius < 0) {

System.out.println(“ Incorrect Input ”);

} else {

area = radius * radius *3.14159;

System.out.println(“ Area is “ + area);

****

BOOLEAN EXPRESSION:

- Expression to evaluate Boolean value: true or false.

Relational operators = comparison operators

- There are 6 type of relational operators

Equality (Bandingkan) Assignment


== =

Boolean variable = Variable that holds Boolean variable

Boolean data type = To declare Boolean variables

Listing 3.1

Import java.util.Scanner;

public class AdditionQuiz{

public static void main(String[] args){

int number1 = (int)(System.currentTimeMillis() % 10);

int number2 = (int)(System.currentTimeMillis() / 7 % 10);

Scanner input = new Scanner(System.in);

System.out.println(“ What is ” + number1 + “+” + number2 + “?” );

Int answer = input.nextInt();

System.out.println( number1 + “+ ” + number2 + “=” + answer + “is” +

(number1 + number2 == answer));

}
}

Question:

3.1 Six relational operator

- < , <= , > , >= , != , ==

3.2 Show the result each: (x is 1)

(x > 0) = true

(x < 0) = false

(x != 0) = true

(x >= 0) = false

(x != 1) = false

3.3 Testing casting verified or not

boolean b = true;

i = (int)b; // cannot cast like this because i not declare

int i =1;

boolean b = (boolean)i; // not verified this type of casting

IF STATEMENT

Type of If Statement

1. One-way if statement

If (Boolean-expression / condition) {
Statement(s);
}

2. Two-way if-else statement

If (Boolean-expression / condition) {
Statement(s) -for-the-true-case;
}
Else {
Statement(s)-for-the-false-caase;
}

3. Nested if statement
4. Multi-way if-else statement
5. Switch statement
6. Conditional expression
Syntax and Element:

1. Word: if
2. Parentheses
3. Condition or Boolean expression
4. Curly braces
5. Statement

Code example:

If (radius >= 0) {

Area = radius*radius*PI;

System.out.println(“ The area of the circle of radius ” +radius+ “ is ” +area);

Listing 3.2

import java.util.Scanner;

public class SimpleIfDemo{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.println(“ Enter an integer: ”);

int number = input.nextInt();

if (number % 5 == 0)

System.out.println(“ HiFive “);

if (number % 2 == 0)

System.out.println(“ HiEven “);

Check Point

3.4 If Statement that assign 1 to x if y is greater than 0

import java.util.Scanner;

public class AssignX{

public static void main(String[] args){

Scanner input = new Scanner(System.in);


int y = input.nextInt();

int x;

if ( y > 0 )

x = 1;

System.out.println( y + " is greater than 0 ");

3.5 If statement that increase pay by 3% if score is greater than 90

import java.util.Scanner;

public class IncreasePay{

public static void main(String[] args){

Scanner input = new Scanner ( System.in );

System.out.println(" Enter your score ");

double score = input.nextDouble();

if ( score > 90){

double pay = (30d/100d)*30d;

System.out.println(" Your payment increase to " + pay);

Two-Way If-Else

if ( )

Case Study: Computing Body Mass Index

import java.util.Scanner;

public class ComputingBmi{

public static void main(String[] args){

Scanner input = new Scanner(System.in);

System.out.println(" Enter your weight in pounds: ");


double weight = input.nextDouble();

System.out.println(" Enter your height in inches: ");

double height = input.nextDouble();

final double KILOGRAMS = 0.45359237; //in kg

final double METER = 0.0254; //in metre

double weight2 = weight * KILOGRAMS;

double height2 = height * METER;

double bmi = weight2 / (height*height);

System.out.println(" Your BMI is : " +bmi);

if ( bmi < 18.5)

System.out.println(" Underweight ");

else if ( bmi <= 25.0 )

System.out.println(" Normal ");

else if ( bmi <= 30.0)

System.out.println(" Overwerght");

else

System.out.println(" Obese ");

CHAPTER 5: LOOPS

5.1 Intro
- Loop means executed statement repeatedly

Code Example 1:

int count = 0;

while ( count < 100 ) {

System.out.println(“ Welcome to java “);


count++;

Code Example 2:

int sum = 0, i = 1;

while ( i < 10) {

sum = sum + i;

i++;

System.out.println(“ sum is “ +sum); // sum is 45

sum i i < 10 sum + i sum i++


1 0 1 true 0+1 1 2
2 1 2 true 1+2 3 3
3 3 3 true 3+3 6 4
4 6 4 true 6+4 10 5
5 10 5 true 10 + 5 15 6
6 15 6 true 15 + 6 21 7
7 21 7 true 21 + 7 28 8
8 28 8 true 28 + 8 36 9
9 36 9 true 36 + 9 45 10
10 45 10 false - - -

3 Type of Loop Statement

1. While loop
2. do-while loop
3. for loop

While Do-While For

While (loop-continuation-condition) {
// Loop body
Statement(s);
}

loop-
continuation-
condition
False

Statement(s)
(loop body)
True

You might also like