easy practice with if statement and boolean type

17
Easy practice with if statement and Boolean type Java programming Nooria Esmaelzade 2016

Upload: nooria-esmaelzade

Post on 09-Apr-2017

31 views

Category:

Self Improvement


0 download

TRANSCRIPT

Page 1: Easy practice with if statement and boolean type

Easy practice with if statement and Boolean

typeJava programming Nooria Esmaelzade

2016

Page 2: Easy practice with if statement and boolean type

Review to data types74532573

723.3634

MaryamThis is a string.

A

int, short, byte, long

Float, double

String

char

Page 3: Easy practice with if statement and boolean type

But how you can save true or false values??How do you save the result of comparison? int x = 10;

int y = 20;

(X > Y ) ( X < Y ) (X = Y)

Page 4: Easy practice with if statement and boolean type

Primitive data types

integral (صحیح) floating point(اعشاری) Boolean (true, false)

Byte short int long

8-bit 16-bit 32-bit 64-bit

Float double 32-bit 64-bit

Page 5: Easy practice with if statement and boolean type

Boolean data type

A boolean is used to perform logical operations, most commonly to determine whether some condition is true.

Boolean only can have two values : True

false

Page 6: Easy practice with if statement and boolean type

Boolean examplepublic class testBoolean {

public static void main(String[] args) {

boolean b = true;boolean b2 = false;boolean b3 = b2;

System.out.println(b);System.out.println(b2);System.out.println(b3);

}

}

Page 7: Easy practice with if statement and boolean type

Boolean data typeThe result of the comparison is a boolean value

int radius = 10;

Page 8: Easy practice with if statement and boolean type

Boolean example 2 public class testBoolean {

public static void main(String[] args) {

boolean b ;int x = 10;int y = 20;

b = x>y;

System.out.println(b);System.out.println( x < y);}

}

Page 9: Easy practice with if statement and boolean type

practice additions gameimport java.util.Scanner; public class AdditionQuiz { public static void main(String[] args) {

int number2 = (int)(System.currentTimeMillis() * 7 % 10); int number1 = (int)(System.currentTimeMillis() % 10);int addition = number2 + number1;

Scanner input = new Scanner(System.in);

System.out.print( "What is " + number1 + " + " + number2 + " ? ");int answer = input.nextInt();

boolean result = addition == answer;System.out.println(number1 + " + " + number2 + " = " + answer + " is " + result);}

}

Page 10: Easy practice with if statement and boolean type

If statements

Page 11: Easy practice with if statement and boolean type

What do you think??

Page 12: Easy practice with if statement and boolean type

So???

If you are a good person You will go to the heaven

If you are a bad person You will go to the hell

Page 13: Easy practice with if statement and boolean type

How we use if in java

if (you are bad) {

you are in hell ; You will be tortured;

}

if (you are good) {

you are in heaven;

You will enjoy;

}

Page 14: Easy practice with if statement and boolean type

Another example Who succeeds and who fails ???

If the test score is greater than 55, the student is passed

If the test score is less than 55, the student is failed

Page 15: Easy practice with if statement and boolean type

How we use it in java

if (score > 55){

Student passed;

}

if (score < 55){

Student failed;

}

Page 16: Easy practice with if statement and boolean type

Simple program to check if student is passed or failed

import java.util.Scanner;public class passOrFail {

public static void main(String[] args) {

Scanner input = new Scanner (System.in);System.out.println("Enter your score: ");int score = input.nextInt();

if (score >= 55){System.out.println("hahaha you are passed \u263A");}

if (score < 55){

System.out.println("Sorry you are failed \u2639");}

}}

Page 17: Easy practice with if statement and boolean type

Homework Write the program that ask the user to enter an integer number. The program should check if it is odd or even.

If the number is even, print “the number is even”Otherwise print, “the number is odd”