Institute of Computer Science
  1. Courses
  2. 2017/18 spring
  3. Object-Oriented Programming (Narva College) (P2NC.01.083)
ET
Log in

Object-Oriented Programming (Narva College) 2017/18 spring

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

Logical control structures

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 branches join together again

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 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.

Boolean Expressions

Usually the 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:

OperatorMeaning
A == Bis A equal to B ?
A < Bis A less than B ?
A <= Bis A less than or equal to B ?
A > Bis A Greater than B ?
A >= Bis A Greater than or equal to B ?
A != Bis A not equal to B ?

We will learn a bit later how to evaluate Strings.

Combination of Expressions

The condition in the if statement can be a single expression or a combination of expressions. Java provides three logical operators that combine boolean expressions:

OperatorNameExampleEvaluates to true if and only if…
&&anda && ba AND b are both true
||ora||ba OR b (or both) are true
!not!aa is NOT true

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 ){
    // nested true branch
}
else{
    // nested false branch
}

// statements that are executed after the branches join together again

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 following condition(s).

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");
}
Session 2
  • Institute of Computer Science
  • Faculty of Science and Technology
  • University of Tartu
In case of technical problems or questions write to:

Contact the course organizers with the organizational and course content questions.
The proprietary copyrights of educational materials belong to the University of Tartu. The use of educational materials is permitted for the purposes and under the conditions provided for in the copyright law for the free use of a work. When using educational materials, the user is obligated to give credit to the author of the educational materials.
The use of educational materials for other purposes is allowed only with the prior written consent of the University of Tartu.
Terms of use for the Courses environment