Arvutiteaduse instituut
  1. Kursused
  2. 2019/20 kevad
  3. Objektorienteeritud programmeerimine (Narva Kolledž) (LTAT.NR.003)
EN
Logi sisse

Objektorienteeritud programmeerimine (Narva Kolledž) 2019/20 kevad

  • Home
  • Materials
  • Grading
  • Java Glossary
  • Cheat sheet (S1-S6)
  • Source Example
  • Links
Chapter 2

Logic control structures

A boolean is a primitive data type used in boolean expressions of logic control structures.

Boolean Expressions

Usually a condition is a boolean expression - a combination of literals, operators, variable names, and parentheses used to evaluate variables and values. Boolean expressions often operate on the primitive data types (e.g. numbers, chars) using relational operators:

OperatorExampleEquivalent
==b1==b2returns true if both the left side and right side are equal
!=b1!=b2returns true if left side is not equal to the right side of operator
>b1>b2returns true if left side is greater than right
<b1<b2returns true if left side is less than right side
>=b1>=b2returns true if left side is greater than or equal to right side
<=b1<=b2returns true if left side is less than or equal to right side
equalsb1.equals(b2)to compare two strings

Check an example

public class RelationalDemo {
   public static void main(String args[]) {
      int num1 = 10;
      int num2 = 50;
      if (num1 == num2) {
	     System.out.println("num1 and num2 are equal");
      }

      if (num1 != num2) {
	     System.out.println("num1 and num2 are not equal");
      }
   }
}

Two-way decision

An outline of a two-way decision structure:

// statements that are executed before the decision

if (condition) {
    // true branch
}
else {
    // false branch
}
// statements that are executed after the the if-structure

Here are some details:

  • The condition is a boolean expression which compares values; the result of the evaluation is either true or false.
  • The else keyword separates the true branch from the false branch.
  • The true branch can be a block and the false branch can be a block; a block consists of several statements inside a pair of braces {}.
  • When a block is chosen for execution, the statements in it are executed one by one.
  • The statements after the false branch (after line 8) are always executed.

Note that a condition in the if statement can be a single expression or a combination of expressions - the latter simplifies and shortens the code. Java provides three logical operators that combine boolean expressions:

OperatorExampleExplanation
&&b1&&b2returns true if both b1 and b2 are true else it would return false
||b1||b2returns false if both b1 and b2 are false else it would return true
!!b1returns the opposite of b1, that means it would be true if b1 is false and it would return false if b1 is true

Check an example

// statements that are executed before the decision

if (a == 6 && b == 7) {
    System.out.println(a*b);
}
else {
    System.out.println(a/b);
}

Three-way decision

// statements that are executed before the decision

if (condition 1) {
    // true branch
}
else if (condition 2) {
    // alternative branch
}
else {
    // false branch
}

// statements that are executed after the if-structure 

PS! Only one of these blocks will get executed, and then Java jumps out of the conditional structure. Plan tests properly to avoid overlapping or invalid behaviour of the program.

Nested decisions

Switch operator

The three- or multi-way conditional structures can be substituted by operator switch:

switch (expression) {
   case value1:
        // statements;
        break;
   case value2:
        // statements;
        break;
   . . .
   default:
        // statements if none of the cases has been executed;
}

PS! Be careful with the keyword break. If we forget to add it, then Java will not jump out of the conditional structure and continue checking the conditions that follow.

An example (part of the code):

int weekDay = 4;
switch (weekDay) {
    case 1: System.out.println("Monday"); break;
    case 2: System.out.println("Tuesday"); break;
    case 3: System.out.println("Wednesday"); break;
    case 4: System.out.println("Thursday"); break;
    case 5: System.out.println("Friday"); break;
    case 6: System.out.println("Saturday"); break;
    case 7: System.out.println("Sunday"); break;
    default: System.out.println("Invalid number");
}
Chapter 2
  • Arvutiteaduse instituut
  • Loodus- ja täppisteaduste valdkond
  • Tartu Ülikool
Tehniliste probleemide või küsimuste korral kirjuta:

Kursuse sisu ja korralduslike küsimustega pöörduge kursuse korraldajate poole.
Õppematerjalide varalised autoriõigused kuuluvad Tartu Ülikoolile. Õppematerjalide kasutamine on lubatud autoriõiguse seaduses ettenähtud teose vaba kasutamise eesmärkidel ja tingimustel. Õppematerjalide kasutamisel on kasutaja kohustatud viitama õppematerjalide autorile.
Õppematerjalide kasutamine muudel eesmärkidel on lubatud ainult Tartu Ülikooli eelneval kirjalikul nõusolekul.
Courses’i keskkonna kasutustingimused