SlideShare a Scribd company logo
1 of 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?

More Related Content

What's hot

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
 

What's hot (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
 

Viewers also liked

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
 

Viewers also liked (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
 

Similar to 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
 

Recently uploaded

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfagholdier
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxmarlenawright1
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfDr Vijay Vishwakarma
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17Celine George
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxPooja Bhuva
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxJisc
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxEsquimalt MFRC
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSCeline George
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...pradhanghanshyam7136
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.MaryamAhmad92
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfSherif Taha
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.pptRamjanShidvankar
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...Nguyen Thanh Tu Collection
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxPooja Bhuva
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...ZurliaSoop
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxDr. Ravikiran H M Gowda
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jisc
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17Celine George
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17Celine George
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.christianmathematics
 

Recently uploaded (20)

Holdier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdfHoldier Curriculum Vitae (April 2024).pdf
Holdier Curriculum Vitae (April 2024).pdf
 
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptxHMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
HMCS Vancouver Pre-Deployment Brief - May 2024 (Web Version).pptx
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17How to Create and Manage Wizard in Odoo 17
How to Create and Manage Wizard in Odoo 17
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Wellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptxWellbeing inclusion and digital dystopias.pptx
Wellbeing inclusion and digital dystopias.pptx
 
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptxHMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
HMCS Max Bernays Pre-Deployment Brief (May 2024).pptx
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...Kodo Millet  PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
Kodo Millet PPT made by Ghanshyam bairwa college of Agriculture kumher bhara...
 
ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.ICT role in 21st century education and it's challenges.
ICT role in 21st century education and it's challenges.
 
Food safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdfFood safety_Challenges food safety laboratories_.pdf
Food safety_Challenges food safety laboratories_.pdf
 
Application orientated numerical on hev.ppt
Application orientated numerical on hev.pptApplication orientated numerical on hev.ppt
Application orientated numerical on hev.ppt
 
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
TỔNG ÔN TẬP THI VÀO LỚP 10 MÔN TIẾNG ANH NĂM HỌC 2023 - 2024 CÓ ĐÁP ÁN (NGỮ Â...
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
Jual Obat Aborsi Hongkong ( Asli No.1 ) 085657271886 Obat Penggugur Kandungan...
 
REMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptxREMIFENTANIL: An Ultra short acting opioid.pptx
REMIFENTANIL: An Ultra short acting opioid.pptx
 
Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)Jamworks pilot and AI at Jisc (20/03/2024)
Jamworks pilot and AI at Jisc (20/03/2024)
 
How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17How to Give a Domain for a Field in Odoo 17
How to Give a Domain for a Field in Odoo 17
 
How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17How to Add New Custom Addons Path in Odoo 17
How to Add New Custom Addons Path in Odoo 17
 
This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.This PowerPoint helps students to consider the concept of infinity.
This PowerPoint helps students to consider the concept of infinity.
 

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?