SlideShare a Scribd company logo
Java - General
 Java    is:
  – platform independent programming
    language
  – similar to C++ in syntax
  – similar to Smalltalk in mental paradigm
 Pros: also ubiquitous to net
 Cons: interpreted, and still under
  development (moving target)
How it works…!
Compile-time Environment                Compile-time Environment

                                          Class
                                         Loader                Java
                                                               Class
                                        Bytecode             Libraries
       Java                              Verifier
      Source
      (.java)

                                                   Just in
                       Java          Java
                                                    Time
                    Bytecodes     Interpreter                Java
                                                  Compiler
                   move locally                              Virtual
                    or through                               machine
       Java          network
     Compiler
                                     Runtime System




       Java                         Operating System
    Bytecode
     (.class )
                                         Hardware
Primitive Types and Variables
   boolean, char, byte, short, int, long, float, double etc.
   These basic (or primitive) types are the only types
    that are not objects (due to performance issues).
   This means that you don’t use the new operator to
    create a primitive variable.
   Declaring primitive variables:
        float initVal;
        int retVal, index = 2;
        double gamma = 1.2, brightness
        boolean valueOk = false;
Initialisation
   If no value is assigned prior to use, then the
    compiler will give an error
   Java sets primitive variables to zero or false
    in the case of a boolean variable
   All object references are initially set to null
   An array of anything is an object
     – Set to null on declaration
     – Elements to zero false or null on creation
Declarations
        int index = 1.2;            // compiler error
        boolean retOk = 1;          // compiler error
        double fiveFourths = 5 / 4; // no error!
        float ratio = 5.8f;         // correct
        double fiveFourths = 5.0 / 4.0;     // correct
   1.2f is a float value accurate to 7 decimal places.
   1.2 is a double value accurate to 15 decimal places.
Assignment
   All Java assignments are right associative
    int a = 1, b = 2, c = 5
    a=b=c
    System.out.print(
    “a= “ + a + “b= “ + b + “c= “ + c)

 What is the value of a, b & c
 Done right to left: a = (b = c);
Basic Mathematical Operators
   * / % + - are the mathematical operators
   * / % have a higher precedence than + or -
double myVal = a + b % d – c * d / b;
   Is the same as:
double myVal = (a + (b % d)) –
                    ((c * d) / b);
If – The Conditional Statement
   The if statement evaluates an expression and if that
    evaluation is true then the specified action is taken
        if ( x < 10 ) x = 10;
   If the value of x is less than 10, make x equal to 10
   It could have been written:
        if ( x < 10 )
        x = 10;
   Or, alternatively:
        if ( x < 10 ) { x = 10; }
Relational Operators
==   Equal (careful)
!=   Not equal
>=   Greater than or equal
<=   Less than or equal
>    Greater than
<    Less than
If… else
   The if … else statement evaluates an expression and
    performs one action if that evaluation is true or a
    different action if it is false.
    if (x != oldx) {
      System.out.print(“x was changed”);
    }
    else {
      System.out.print(“x is unchanged”);
    }
Nested if … else
 if ( myVal > 100 ) {
   if ( remainderOn == true) {
       myVal = mVal % 100;
   }
   else {
     myVal = myVal / 100.0;
   }
 }
 else
 {
   System.out.print(“myVal is in range”);
 }
else if
   Useful for choosing between alternatives:
    if ( n == 1 ) {
      // execute code block #1
    }
    else if ( j == 2 ) {
      // execute code block #2
    }
    else {
      // if all previous tests have failed,
      execute code block #3
    }
A Warning…
WRONG!                    CORRECT!
if( i == j )              if( i == j ) {
                            if ( j == k )
    if ( j == k )
                            System.out.print(
     System.out.print(
                                “i equals k”);
         “i equals k”);
                          }
    else
                          else
     System.out.print(
     “i is not equal
                           System.out.print(“
     to j”);
                           i is not equal to
                           j”);   // Correct!
The switch Statement
   switch ( n ) {
     case 1:
      // execute code block #1
      break;
     case 2:
      // execute code block #2
      break;
      default:
      // if all previous tests fail then
           //execute code block #4
      break;
   }
The for loop
 Loop n times
   for ( i = 0; i < n; n++ ) {
     // this code body will execute n times
     // ifrom 0 to n-1
   }
 Nested for:
   for ( j = 0; j < 10; j++ ) {
     for ( i = 0; i < 20; i++ ){
       // this code body will execute 200 times
     }
   }
while loops
 while(response == 1) {
   System.out.print( “ID =” +
   userID[n]);
   n++;
   response = readInt( “Enter “);
 }
What is the minimum number of times the loop
is executed?
What is the maximum number of times?

More Related Content

PPT
Unit I Advanced Java Programming Course
PPTX
Knowledge of Javascript
PDF
JavaScript Programming
PPT
The JavaScript Programming Language
PDF
Giorgio zoppi cpp11concurrency
PDF
Frege - consequently functional programming for the JVM
PDF
Functional Programming in C#
PDF
Frege Tutorial at JavaOne 2015
Unit I Advanced Java Programming Course
Knowledge of Javascript
JavaScript Programming
The JavaScript Programming Language
Giorgio zoppi cpp11concurrency
Frege - consequently functional programming for the JVM
Functional Programming in C#
Frege Tutorial at JavaOne 2015

What's hot (18)

PDF
Javascript basic course
PPTX
Java For Automation
PPTX
Migrating to JUnit 5
PDF
Ejemplo completo de integración JLex y CUP
PPTX
2. Design patterns. part #2
PPTX
Actors model in gpars
DOC
What is storage class
PPTX
PPSX
Hello Java-First Level
PDF
A fresh eye on Oracle VM VirtualBox
PDF
Java Programming - 04 object oriented in java
PDF
What's new in Swift 3
PPT
C++ programming
PDF
Analyzing ReactOS One More Time
PPT
Swift Programming - Part 2
PPTX
모던자바의 역습
PDF
Javascript good parts - for novice programmers
PDF
Introduction to Swift
Javascript basic course
Java For Automation
Migrating to JUnit 5
Ejemplo completo de integración JLex y CUP
2. Design patterns. part #2
Actors model in gpars
What is storage class
Hello Java-First Level
A fresh eye on Oracle VM VirtualBox
Java Programming - 04 object oriented in java
What's new in Swift 3
C++ programming
Analyzing ReactOS One More Time
Swift Programming - Part 2
모던자바의 역습
Javascript good parts - for novice programmers
Introduction to Swift
Ad

Viewers also liked (8)

PPTX
9 hist 21.7.11
PPTX
9 his(nazism)
PPTX
Net beansprojects
PPTX
9 his 22.7.11
PPT
Increment & decrement operator
PPT
Class 9 civics
PPT
Computer architecture
PPT
Computer architecture
9 hist 21.7.11
9 his(nazism)
Net beansprojects
9 his 22.7.11
Increment & decrement operator
Class 9 civics
Computer architecture
Computer architecture
Ad

Similar to Java if and else (20)

PPT
Java basic tutorial by sanjeevini india
PPT
Java basic tutorial by sanjeevini india
PPT
Tutorial java
PPT
Java Tutorial
PPT
Java tut1
PPT
Java Tut1
PPTX
Java introduction
PPT
Java teaching ppt for the freshers in colleeg.ppt
PPTX
Fundamentals of java --- version 2
ODP
Synapseindia reviews.odp.
PPTX
Java fundamentals
PPT
Java Tutorial | My Heart
PPT
Java Tutorial
PPT
Java_Tutorial_Introduction_to_Core_java.ppt
PPT
Javatut1
PPT
Java tut1 Coderdojo Cahersiveen
PPT
Java tut1
PPT
Java tut1
PPTX
Android webinar class_java_review
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
Tutorial java
Java Tutorial
Java tut1
Java Tut1
Java introduction
Java teaching ppt for the freshers in colleeg.ppt
Fundamentals of java --- version 2
Synapseindia reviews.odp.
Java fundamentals
Java Tutorial | My Heart
Java Tutorial
Java_Tutorial_Introduction_to_Core_java.ppt
Javatut1
Java tut1 Coderdojo Cahersiveen
Java tut1
Java tut1
Android webinar class_java_review

Recently uploaded (20)

PDF
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
PDF
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
PDF
01-Introduction-to-Information-Management.pdf
PDF
O7-L3 Supply Chain Operations - ICLT Program
PDF
TR - Agricultural Crops Production NC III.pdf
PDF
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
PDF
2.FourierTransform-ShortQuestionswithAnswers.pdf
PDF
Open folder Downloads.pdf yes yes ges yes
PDF
STATICS OF THE RIGID BODIES Hibbelers.pdf
PPTX
Renaissance Architecture: A Journey from Faith to Humanism
PPTX
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
PDF
Anesthesia in Laparoscopic Surgery in India
PDF
Pre independence Education in Inndia.pdf
PDF
102 student loan defaulters named and shamed – Is someone you know on the list?
PDF
Insiders guide to clinical Medicine.pdf
PDF
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
PPTX
Open Quiz Monsoon Mind Game Final Set.pptx
PPTX
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
PPTX
Cell Structure & Organelles in detailed.
PDF
Basic Mud Logging Guide for educational purpose
Physiotherapy_for_Respiratory_and_Cardiac_Problems WEBBER.pdf
The Lost Whites of Pakistan by Jahanzaib Mughal.pdf
01-Introduction-to-Information-Management.pdf
O7-L3 Supply Chain Operations - ICLT Program
TR - Agricultural Crops Production NC III.pdf
Chapter 2 Heredity, Prenatal Development, and Birth.pdf
2.FourierTransform-ShortQuestionswithAnswers.pdf
Open folder Downloads.pdf yes yes ges yes
STATICS OF THE RIGID BODIES Hibbelers.pdf
Renaissance Architecture: A Journey from Faith to Humanism
BOWEL ELIMINATION FACTORS AFFECTING AND TYPES
Anesthesia in Laparoscopic Surgery in India
Pre independence Education in Inndia.pdf
102 student loan defaulters named and shamed – Is someone you know on the list?
Insiders guide to clinical Medicine.pdf
Saundersa Comprehensive Review for the NCLEX-RN Examination.pdf
Open Quiz Monsoon Mind Game Final Set.pptx
Introduction_to_Human_Anatomy_and_Physiology_for_B.Pharm.pptx
Cell Structure & Organelles in detailed.
Basic Mud Logging Guide for educational purpose

Java if and else

  • 1. Java - General  Java is: – platform independent programming language – similar to C++ in syntax – similar to Smalltalk in mental paradigm  Pros: also ubiquitous to net  Cons: interpreted, and still under development (moving target)
  • 2. How it works…! Compile-time Environment Compile-time Environment Class Loader Java Class Bytecode Libraries Java Verifier Source (.java) Just in Java Java Time Bytecodes Interpreter Java Compiler move locally Virtual or through machine Java network Compiler Runtime System Java Operating System Bytecode (.class ) Hardware
  • 3. Primitive Types and Variables  boolean, char, byte, short, int, long, float, double etc.  These basic (or primitive) types are the only types that are not objects (due to performance issues).  This means that you don’t use the new operator to create a primitive variable.  Declaring primitive variables: float initVal; int retVal, index = 2; double gamma = 1.2, brightness boolean valueOk = false;
  • 4. Initialisation  If no value is assigned prior to use, then the compiler will give an error  Java sets primitive variables to zero or false in the case of a boolean variable  All object references are initially set to null  An array of anything is an object – Set to null on declaration – Elements to zero false or null on creation
  • 5. Declarations int index = 1.2; // compiler error boolean retOk = 1; // compiler error double fiveFourths = 5 / 4; // no error! float ratio = 5.8f; // correct double fiveFourths = 5.0 / 4.0; // correct  1.2f is a float value accurate to 7 decimal places.  1.2 is a double value accurate to 15 decimal places.
  • 6. Assignment  All Java assignments are right associative int a = 1, b = 2, c = 5 a=b=c System.out.print( “a= “ + a + “b= “ + b + “c= “ + c)  What is the value of a, b & c  Done right to left: a = (b = c);
  • 7. Basic Mathematical Operators  * / % + - are the mathematical operators  * / % have a higher precedence than + or - double myVal = a + b % d – c * d / b;  Is the same as: double myVal = (a + (b % d)) – ((c * d) / b);
  • 8. If – The Conditional Statement  The if statement evaluates an expression and if that evaluation is true then the specified action is taken if ( x < 10 ) x = 10;  If the value of x is less than 10, make x equal to 10  It could have been written: if ( x < 10 ) x = 10;  Or, alternatively: if ( x < 10 ) { x = 10; }
  • 9. Relational Operators == Equal (careful) != Not equal >= Greater than or equal <= Less than or equal > Greater than < Less than
  • 10. If… else  The if … else statement evaluates an expression and performs one action if that evaluation is true or a different action if it is false. if (x != oldx) { System.out.print(“x was changed”); } else { System.out.print(“x is unchanged”); }
  • 11. Nested if … else if ( myVal > 100 ) { if ( remainderOn == true) { myVal = mVal % 100; } else { myVal = myVal / 100.0; } } else { System.out.print(“myVal is in range”); }
  • 12. else if  Useful for choosing between alternatives: if ( n == 1 ) { // execute code block #1 } else if ( j == 2 ) { // execute code block #2 } else { // if all previous tests have failed, execute code block #3 }
  • 13. A Warning… WRONG! CORRECT! if( i == j ) if( i == j ) { if ( j == k ) if ( j == k ) System.out.print( System.out.print( “i equals k”); “i equals k”); } else else System.out.print( “i is not equal System.out.print(“ to j”); i is not equal to j”); // Correct!
  • 14. The switch Statement switch ( n ) { case 1: // execute code block #1 break; case 2: // execute code block #2 break; default: // if all previous tests fail then //execute code block #4 break; }
  • 15. The for loop  Loop n times for ( i = 0; i < n; n++ ) { // this code body will execute n times // ifrom 0 to n-1 }  Nested for: for ( j = 0; j < 10; j++ ) { for ( i = 0; i < 20; i++ ){ // this code body will execute 200 times } }
  • 16. while loops while(response == 1) { System.out.print( “ID =” + userID[n]); n++; response = readInt( “Enter “); } What is the minimum number of times the loop is executed? What is the maximum number of times?