Cheap Rate ➥8448380779 ▻Call Girls In Mg Road Gurgaon
Easy practice with if statement and boolean type
1. Easy practice with if
statement and Boolean type
Java programming
Nooria Esmaelzade
2016
2. Review to data types
74532573
723.3634
Maryam
This is a string.
A
int, short, byte, long
Float, double
String
char
3. 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)
4. 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
5. 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
6. Boolean example
public 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);
}
}
8. 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);
}
}
9. practice additions game
import 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);
}
}
12. 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
13. 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;
}
14. 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
15. How we use it in java
if (score > 55){
Student passed;
}
if (score < 55){
Student failed;
}
16. 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");
}
}
}
17. 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”