Istruzioni (Statements)

Assegnazione

< NAME >= < EXPRESSION >
int x;
x = 5;

<EXPRESSION>.<NAME>= <EXPRESSION>

Point pt = new Point(3,4);
pt.x = 5;

Alternativi

<NAME> += <EXPRESSION>

x += 2 equivalente a x = x+2

<NAME> -= <EXPRESSION>

x -= 2 equivalente a x = x-2

<NAME> *= <EXPRESSION>

x *= 2 equivalente a x = x*2

<NAME> /= <EXPRESSION>

x /= 2 equivalente a x = x/2

<NAME> %= <EXPRESSION>

x %= 2 equivalente a x = x%2

++<NAME>

++x equivalente a x = x+1

--<NAME>

--x equivalente a x = x-1

L'assegnazione è anche un'espressione x = (i = 1), non usare.

<NAME>++

<NAME>--

Dopo

int x,i;
i = 5;
x = ++i;
i vale 6 e x vale 6

Dopo

int x,i;
i = 5;
x = i++;
i vale 6 e x vale 5

Return

return < EXPRESSION >

Blocco (Block)

{
   <STATEMENT> ;
   <STATEMENT> ;
   ...
   <STATEMENT> ;
}

Portata (Scope)

{
  int i,j;
  i = 5;
  j = 6;
}
Errore:

{
  {
    int i;
    i = 5;
  }
  i += 6;
}
Corretto:

{
  int i;
  {
    i = 5;
  }
  i += 6;
}

If

if (<EXPRESSION >)
   < STATEMENT > ;
else
   < STATEMENT > ;
int max (int x, int y) {
  if (x <= y)
    return y;
  else 
    return x;
}
int max (int x, int y, int z) {
  if (x <= y)
    if (y <= z)
      return z;
    else 
      return y;
  else
    if (x <= z)
      return z;
    else 
      return z;
}
difficile da leggere, meglio:
int max (int x, int y, int z) {
  if (x <= y) {
    if (y <= z) {
      return z;
    } else {
      return y;
    }
  } else {
    if (x <= z) {
      return z;
    } else {
      return z;
    }
  }
}

Alternativa

if (<EXPRESSION >)
   < STATEMENT > ;

Per l'else

if (!<EXPRESSION >)
   < STATEMENT > ;

int max (int x, int y) {
  if (x <= y)
    return y;
  return x;
}

if (x <= y)
  if (y <= z)
    return z;
else 
  return y;
è interpretato come:
if (x <= y) {
  if (y <= z) {
    return z;
  } else { 
    return y;
  }
}
e non come:
if (x <= y) {
  if (y <= z) {
    return z;
  }
} else { 
  return y;
}

While

while (<EXPRESSION >)
   < STATEMENT > ;
int i = 1, res = 0;
while (i <= 100) {
  res += i;
  i++;
}
Alla fine res vale 101*50 = 5050
int i = 1, res = 0;
while (i <= 100) {
  res += i;
  i *= 2;
}
Alla fine res vale 1+2+4+8+16+32+64 = 117

Alternativa

do < STATEMENT > ;
while (<EXPRESSION >)
int i = 101, res = 0;
while (i <= 100) {
  res += i;
  i++;
}
Alla fine res vale 0. Invece
int i = 101, res = 0;
do {
  res += i;
  i++;
}
while (i <= 100);
Alla fine res vale 101.

For

for (< INITS >;< EXPRESSION >;< INCR >)
   < STATEMENT > ;
int res = 0;
for (int i = 1; i <= 100; i++)
  res += i;
}
Alla fine res vale 101*50 = 5050

Alternativi

int i,res = 0;
for (i = 1; i <= 100; i++)
  res += i;
}
for (int i = 1, res = 0; i <= 100; res += i,i++) {
}
int res = 0;
for (int i = 0,j = 10; i < j; i++,j--)
  res += i+j;
}
Alla fine res vale 50.

Switch

switch (< EXPRESSION >) {
   case < EXPRESSION > :
     < STATEMENT > ;
   case < EXPRESSION > :
     < STATEMENT > ;
   ...
   case < EXPRESSION > :
     < STATEMENT > ;
   default :
     < STATEMENT > ;
}
boolean checkAnswer (char c) {
  switch (c) {
    case 'y':
      return true;
    case 'n':
      return false;
    default:
      return false;
  }
}

boolean checkAnswer (char c) {
  boolean res;
  switch (c) {
    case 'y':
      res = true;
    default:
      res = false;
  }
  return res;
}
ritorna sempre false.

Alternativa

switch (< EXPRESSION >) {
   case < EXPRESSION > :
     < STATEMENT > ;
   case < EXPRESSION > :
     < STATEMENT > ;
   ...
   case < EXPRESSION > :
     < STATEMENT > ;
}

Break

boolean checkAnswer (char c) {
  boolean res;
  switch (c) {
    case 'y': {
      res = true;
      break;
    }
    default:
      res = false;
  }
  return res;
}
int res = 0;
for (int i = 0,j = 10; i<y; i++,j--) {
  if (res >= 30) {
    break;
  }
  res += i+j;
}

Continue

int res = 0;
for (int i = 0,j = 10; i<y; i++,j--) {
  if (i = 3) {
    continue;
  }
  res += i+j;
}

Esercizi

A1

Scrivere un metodo isPrime che, dato un numero n intero, verifica se tale numero è primo, ovvero se è divisibile solo da 1 e da n.
Suggerimento: a divide b ( b%a==0) A2*. Scrivere un metodo isPerfect che, dato un numero intero, verifica se tale numero è perfetto, ovvero se è uguale alla somma dei suoi divisori (dove il numero stesso non è incluso fra i divisori). Per esempio, 6 è perfetto (6 = 1+2+3).

A3*. Una congettura matematica ancora aperta è quella di Goldbach: ogni numero pari piú grande di 4 è la somma di due numeri primi. Per esempio, abbiamo 4 = 2+2, 6 = 3+3, 8 = 5+3, ...
Scrivere un programma che, dato un numero intero n, verifica che la congettura è vera per tutti i numeri minori di n. Se può usare il metodo isPrime definito nell'esercizio A1.


Laurent Théry
Last modified: Fri Jan 23 00:26:11 MET 2004