SlideShare ist ein Scribd-Unternehmen logo
1 von 16
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?

Weitere ähnliche Inhalte

Was ist angesagt?

Javascript basic course
Javascript basic courseJavascript basic course
Javascript basic courseTran Khoa
 
Ejemplo completo de integración JLex y CUP
Ejemplo completo de integración JLex y CUPEjemplo completo de integración JLex y CUP
Ejemplo completo de integración JLex y CUPEgdares Futch H.
 
2. Design patterns. part #2
2. Design patterns. part #22. Design patterns. part #2
2. Design patterns. part #2Leonid Maslov
 
What is storage class
What is storage classWhat is storage class
What is storage classIsha Aggarwal
 
A fresh eye on Oracle VM VirtualBox
A fresh eye on Oracle VM VirtualBoxA fresh eye on Oracle VM VirtualBox
A fresh eye on Oracle VM VirtualBoxPVS-Studio
 
Analyzing ReactOS One More Time
Analyzing ReactOS One More TimeAnalyzing ReactOS One More Time
Analyzing ReactOS One More TimePVS-Studio
 
모던자바의 역습
모던자바의 역습모던자바의 역습
모던자바의 역습DoHyun Jung
 
Javascript good parts - for novice programmers
Javascript good parts - for novice programmersJavascript good parts - for novice programmers
Javascript good parts - for novice programmersManohar Shetty
 

Was ist angesagt? (18)

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

Andere mochten auch

9 hist 21.7.11
9 hist 21.7.119 hist 21.7.11
9 hist 21.7.11pratik8897
 
Net beansprojects
Net beansprojectsNet beansprojects
Net beansprojectspratik8897
 
Increment & decrement operator
Increment & decrement operatorIncrement & decrement operator
Increment & decrement operatorpratik8897
 
Class 9 civics
Class 9 civicsClass 9 civics
Class 9 civicspratik8897
 
Computer architecture
Computer architectureComputer architecture
Computer architectureSanjeev Patel
 
Computer architecture
Computer architectureComputer architecture
Computer architectureRishabha Garg
 

Andere mochten auch (8)

9 hist 21.7.11
9 hist 21.7.119 hist 21.7.11
9 hist 21.7.11
 
9 his(nazism)
9 his(nazism)9 his(nazism)
9 his(nazism)
 
Net beansprojects
Net beansprojectsNet beansprojects
Net beansprojects
 
9 his 22.7.11
9 his 22.7.119 his 22.7.11
9 his 22.7.11
 
Increment & decrement operator
Increment & decrement operatorIncrement & decrement operator
Increment & decrement operator
 
Class 9 civics
Class 9 civicsClass 9 civics
Class 9 civics
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
 
Computer architecture
Computer architectureComputer architecture
Computer architecture
 

Ähnlich wie Java if and else (20)

Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Java basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini indiaJava basic tutorial by sanjeevini india
Java basic tutorial by sanjeevini india
 
Java tutorials
Java tutorialsJava tutorials
Java tutorials
 
Java Tutorial | My Heart
Java Tutorial | My HeartJava Tutorial | My Heart
Java Tutorial | My Heart
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.pptJava_Tutorial_Introduction_to_Core_java.ppt
Java_Tutorial_Introduction_to_Core_java.ppt
 
Java tut1
Java tut1Java tut1
Java tut1
 
Java tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo CahersiveenJava tut1 Coderdojo Cahersiveen
Java tut1 Coderdojo Cahersiveen
 
Java tut1
Java tut1Java tut1
Java tut1
 
Javatut1
Javatut1 Javatut1
Javatut1
 
Synapseindia reviews.odp.
Synapseindia reviews.odp.Synapseindia reviews.odp.
Synapseindia reviews.odp.
 
Java tut1
Java tut1Java tut1
Java tut1
 
Tutorial java
Tutorial javaTutorial java
Tutorial java
 
Java Tut1
Java Tut1Java Tut1
Java Tut1
 
Java Tutorial
Java TutorialJava Tutorial
Java Tutorial
 
Java tutorial PPT
Java tutorial PPTJava tutorial PPT
Java tutorial PPT
 
Java tutorial PPT
Java tutorial  PPTJava tutorial  PPT
Java tutorial PPT
 
Java
Java Java
Java
 
Core Java introduction | Basics | free course
Core Java introduction | Basics | free course Core Java introduction | Basics | free course
Core Java introduction | Basics | free course
 
Java Tutorial
Java Tutorial Java Tutorial
Java Tutorial
 

Kürzlich hochgeladen

Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterMateoGardella
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfSanaAli374401
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxVishalSingh1417
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingTeacherCyreneCayanan
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactPECB
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104misteraugie
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.MateoGardella
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...christianmathematics
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingTechSoup
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactdawncurless
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docxPoojaSen20
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxVishalSingh1417
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhikauryashika82
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Disha Kariya
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docxPoojaSen20
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxDenish Jangid
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfChris Hunter
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxheathfieldcps1
 

Kürzlich hochgeladen (20)

INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptxINDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
INDIA QUIZ 2024 RLAC DELHI UNIVERSITY.pptx
 
Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024Mehran University Newsletter Vol-X, Issue-I, 2024
Mehran University Newsletter Vol-X, Issue-I, 2024
 
Gardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch LetterGardella_PRCampaignConclusion Pitch Letter
Gardella_PRCampaignConclusion Pitch Letter
 
An Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdfAn Overview of Mutual Funds Bcom Project.pdf
An Overview of Mutual Funds Bcom Project.pdf
 
Unit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptxUnit-V; Pricing (Pharma Marketing Management).pptx
Unit-V; Pricing (Pharma Marketing Management).pptx
 
fourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writingfourth grading exam for kindergarten in writing
fourth grading exam for kindergarten in writing
 
Beyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global ImpactBeyond the EU: DORA and NIS 2 Directive's Global Impact
Beyond the EU: DORA and NIS 2 Directive's Global Impact
 
Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104Nutritional Needs Presentation - HLTH 104
Nutritional Needs Presentation - HLTH 104
 
Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.Gardella_Mateo_IntellectualProperty.pdf.
Gardella_Mateo_IntellectualProperty.pdf.
 
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
Explore beautiful and ugly buildings. Mathematics helps us create beautiful d...
 
Grant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy ConsultingGrant Readiness 101 TechSoup and Remy Consulting
Grant Readiness 101 TechSoup and Remy Consulting
 
Accessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impactAccessible design: Minimum effort, maximum impact
Accessible design: Minimum effort, maximum impact
 
PROCESS RECORDING FORMAT.docx
PROCESS      RECORDING        FORMAT.docxPROCESS      RECORDING        FORMAT.docx
PROCESS RECORDING FORMAT.docx
 
Unit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptxUnit-IV; Professional Sales Representative (PSR).pptx
Unit-IV; Professional Sales Representative (PSR).pptx
 
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in DelhiRussian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
Russian Escort Service in Delhi 11k Hotel Foreigner Russian Call Girls in Delhi
 
Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..Sports & Fitness Value Added Course FY..
Sports & Fitness Value Added Course FY..
 
psychiatric nursing HISTORY COLLECTION .docx
psychiatric  nursing HISTORY  COLLECTION  .docxpsychiatric  nursing HISTORY  COLLECTION  .docx
psychiatric nursing HISTORY COLLECTION .docx
 
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptxBasic Civil Engineering first year Notes- Chapter 4 Building.pptx
Basic Civil Engineering first year Notes- Chapter 4 Building.pptx
 
Making and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdfMaking and Justifying Mathematical Decisions.pdf
Making and Justifying Mathematical Decisions.pdf
 
The basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptxThe basics of sentences session 2pptx copy.pptx
The basics of sentences session 2pptx copy.pptx
 

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?