Expressions, Statements and Blocks

Expressions

An expression produces a value when its evaluated. Expressions are made up of variables, operators and methods invocations(which we haven't talked about yet).

Expression Examples

int total = 100; //This is an expression
double sum = 10 + 30; //This an expression

Statements

Statements are roughly equal to statements in natural language. A statement forms a complete unit of execution.

Kinds of Expressions Statements

Control Flow Statements
Declaration Statements
Calling Methods
Assignment Expressions

Control Flow Statements

We will discuss this in detail in the next section. Here is an example of an if statement

boolean isLoggedIn = true;
if(isLogged == true){
 //execute code in this section
}

Declaration Statements

We have seen these before.

double commissionRate = 0.05;

Calling Methods

We will discuss calling methods in the next section, but here is an example of calling a method sum that adds two numbers

Declare Method

static double sum(int x, int y){
    return x + y;
}

Call the Method

double result = sum(10, 20); //We assume sum is declared somewhere

Blocks

A group of zero of more statements between balanced curly braces({}). Blocks are mostly use in control flow statements

if(true){
    //run code here.
    //Everything within the curl braces
}

Let's now look at Control Flow Statements in the next section.

results matching ""

    No results matching ""