easy practice with if statement and boolean type
TRANSCRIPT
![Page 1: Easy practice with if statement and boolean type](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/1.jpg)
Easy practice with if statement and Boolean
typeJava programming Nooria Esmaelzade
2016
![Page 2: Easy practice with if statement and boolean type](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/2.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/3.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/4.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/5.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/6.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/7.jpg)
Boolean data typeThe result of the comparison is a boolean value
int radius = 10;
![Page 8: Easy practice with if statement and boolean type](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/8.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/9.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/10.jpg)
If statements
![Page 11: Easy practice with if statement and boolean type](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/11.jpg)
What do you think??
![Page 12: Easy practice with if statement and boolean type](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/12.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/13.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/14.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/15.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/16.jpg)
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](https://reader036.vdocument.in/reader036/viewer/2022070518/58ea14fb1a28ab064e8b59d3/html5/thumbnails/17.jpg)
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”