SlideShare a Scribd company logo
Easy practice with if
statement and Boolean type
Java programming
Nooria Esmaelzade
2016
Review to data types
74532573
723.3634
Maryam
This is a string.
A
int, short, byte, long
Float, double
String
char
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)
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
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
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);
}
}
Boolean data type
The result of the comparison is a boolean value
int radius = 10;
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);
}
}
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);
}
}
If statements
What do you think??
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 
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;
}
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
How we use it in java
if (score > 55){
Student passed;
}
if (score < 55){
Student failed;
}
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");
}
}
}
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”

More Related Content

What's hot

Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class StructureVicter Paul
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaRadhika Talaviya
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptmanish kumar
 

What's hot (7)

Magic Literals in Pharo
Magic Literals in PharoMagic Literals in Pharo
Magic Literals in Pharo
 
Overview of Java
Overview of Java Overview of Java
Overview of Java
 
Java - Class Structure
Java - Class StructureJava - Class Structure
Java - Class Structure
 
Array
ArrayArray
Array
 
Classes, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with JavaClasses, Objects and Method - Object Oriented Programming with Java
Classes, Objects and Method - Object Oriented Programming with Java
 
Lecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops conceptLecture - 3 Variables-data type_operators_oops concept
Lecture - 3 Variables-data type_operators_oops concept
 
2.oop concept
2.oop concept2.oop concept
2.oop concept
 

Viewers also liked

Why we need to control scope
Why we need to control scope Why we need to control scope
Why we need to control scope Nooria Esmaelzade
 
Easy java key definition &amp; practice
Easy java key definition &amp; practiceEasy java key definition &amp; practice
Easy java key definition &amp; practiceNooria Esmaelzade
 
Easy java installation &amp; practice
Easy java installation &amp; practiceEasy java installation &amp; practice
Easy java installation &amp; practiceNooria Esmaelzade
 
Easy practice with current timemillis method
Easy practice with current timemillis methodEasy practice with current timemillis method
Easy practice with current timemillis methodNooria Esmaelzade
 
Internet degli oggetti
Internet degli oggettiInternet degli oggetti
Internet degli oggettiAlex Fontana
 
Design and synthesis of novel protein kinase R (PKR) inhibitors
Design and synthesis of novel protein kinase R (PKR) inhibitorsDesign and synthesis of novel protein kinase R (PKR) inhibitors
Design and synthesis of novel protein kinase R (PKR) inhibitorssagiv weintraub
 
Paleo grubs book
Paleo grubs bookPaleo grubs book
Paleo grubs booksamcleve19
 
150915 Presentation at ChinaAdhesive 2015
150915 Presentation at ChinaAdhesive 2015150915 Presentation at ChinaAdhesive 2015
150915 Presentation at ChinaAdhesive 2015Maurice Zhu
 
правила безпечної поведінки дітей на канікулах
правила безпечної поведінки дітей на канікулахправила безпечної поведінки дітей на канікулах
правила безпечної поведінки дітей на канікулахOlga Savchenko
 
Easy practice with java variables
Easy practice with java variables Easy practice with java variables
Easy practice with java variables Nooria Esmaelzade
 
Java basics variables
 Java basics   variables Java basics   variables
Java basics variablesJoeReddieMedia
 

Viewers also liked (13)

Why we need to control scope
Why we need to control scope Why we need to control scope
Why we need to control scope
 
Easy java key definition &amp; practice
Easy java key definition &amp; practiceEasy java key definition &amp; practice
Easy java key definition &amp; practice
 
Easy java installation &amp; practice
Easy java installation &amp; practiceEasy java installation &amp; practice
Easy java installation &amp; practice
 
Easy practice with current timemillis method
Easy practice with current timemillis methodEasy practice with current timemillis method
Easy practice with current timemillis method
 
Internet degli oggetti
Internet degli oggettiInternet degli oggetti
Internet degli oggetti
 
Design and synthesis of novel protein kinase R (PKR) inhibitors
Design and synthesis of novel protein kinase R (PKR) inhibitorsDesign and synthesis of novel protein kinase R (PKR) inhibitors
Design and synthesis of novel protein kinase R (PKR) inhibitors
 
Dr. drum
Dr. drumDr. drum
Dr. drum
 
Paleo grubs book
Paleo grubs bookPaleo grubs book
Paleo grubs book
 
Mohammed Gad
Mohammed Gad Mohammed Gad
Mohammed Gad
 
150915 Presentation at ChinaAdhesive 2015
150915 Presentation at ChinaAdhesive 2015150915 Presentation at ChinaAdhesive 2015
150915 Presentation at ChinaAdhesive 2015
 
правила безпечної поведінки дітей на канікулах
правила безпечної поведінки дітей на канікулахправила безпечної поведінки дітей на канікулах
правила безпечної поведінки дітей на канікулах
 
Easy practice with java variables
Easy practice with java variables Easy practice with java variables
Easy practice with java variables
 
Java basics variables
 Java basics   variables Java basics   variables
Java basics variables
 

Similar to Easy practice with if statement and boolean type

Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operatorsmcollison
 
Mutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests qualityMutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests qualityVadim Mikhnevych
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5Vince Vo
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsSvetlin Nakov
 
1 Midterm Preview Time allotted 50 minutes CS 11.docx
1  Midterm Preview Time allotted 50 minutes CS 11.docx1  Midterm Preview Time allotted 50 minutes CS 11.docx
1 Midterm Preview Time allotted 50 minutes CS 11.docxhoney725342
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaRasan Samarasinghe
 
Scjp questions
Scjp questionsScjp questions
Scjp questionsroudhran
 
All based on Zybooks = AP Java with zylabsPlease answer all questi.pdf
All based on Zybooks = AP Java with zylabsPlease answer all questi.pdfAll based on Zybooks = AP Java with zylabsPlease answer all questi.pdf
All based on Zybooks = AP Java with zylabsPlease answer all questi.pdfdeepakarora871
 
Fnt software solutions placement paper
Fnt software solutions placement paperFnt software solutions placement paper
Fnt software solutions placement paperfntsofttech
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.pptMahyuddin8
 
conditional statements
conditional statementsconditional statements
conditional statementsJames Brotsos
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankLauriewest24
 

Similar to Easy practice with if statement and boolean type (20)

Ch4
Ch4Ch4
Ch4
 
Mutation Testing
Mutation TestingMutation Testing
Mutation Testing
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
130706266060138191
130706266060138191130706266060138191
130706266060138191
 
Mutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests qualityMutation testing - the way to improve unit tests quality
Mutation testing - the way to improve unit tests quality
 
DITEC - Programming with Java
DITEC - Programming with JavaDITEC - Programming with Java
DITEC - Programming with Java
 
Java căn bản - Chapter5
Java căn bản - Chapter5Java căn bản - Chapter5
Java căn bản - Chapter5
 
Java Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, LoopsJava Foundations: Basic Syntax, Conditions, Loops
Java Foundations: Basic Syntax, Conditions, Loops
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
Core java
Core javaCore java
Core java
 
1 Midterm Preview Time allotted 50 minutes CS 11.docx
1  Midterm Preview Time allotted 50 minutes CS 11.docx1  Midterm Preview Time allotted 50 minutes CS 11.docx
1 Midterm Preview Time allotted 50 minutes CS 11.docx
 
DISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in JavaDISE - Windows Based Application Development in Java
DISE - Windows Based Application Development in Java
 
Comp102 lec 5.1
Comp102   lec 5.1Comp102   lec 5.1
Comp102 lec 5.1
 
Scjp questions
Scjp questionsScjp questions
Scjp questions
 
All based on Zybooks = AP Java with zylabsPlease answer all questi.pdf
All based on Zybooks = AP Java with zylabsPlease answer all questi.pdfAll based on Zybooks = AP Java with zylabsPlease answer all questi.pdf
All based on Zybooks = AP Java with zylabsPlease answer all questi.pdf
 
Fnt software solutions placement paper
Fnt software solutions placement paperFnt software solutions placement paper
Fnt software solutions placement paper
 
ch04-conditional-execution.ppt
ch04-conditional-execution.pptch04-conditional-execution.ppt
ch04-conditional-execution.ppt
 
conditional statements
conditional statementsconditional statements
conditional statements
 
Java Quiz
Java QuizJava Quiz
Java Quiz
 
Absolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test BankAbsolute Java 5e Savitch Test Bank
Absolute Java 5e Savitch Test Bank
 

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); } }
  • 7. Boolean data type The result of the comparison is a boolean value int radius = 10;
  • 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); } }
  • 11. What do you think??
  • 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”