30.06.2013 Views

Apostila Java

Apostila Java

Apostila Java

SHOW MORE
SHOW LESS

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

FLUXO DE CONTROLE<br />

}<br />

Repetição for<br />

break;<br />

case 1:<br />

System.out.println("emitir nota fiscal");<br />

break;<br />

case 2:<br />

System.out.println("cancelar compra");<br />

break;<br />

default:<br />

System.out.println("efetuar venda");<br />

break;<br />

A declaração for é utilizada para definir que um bloco de comandos deve ser executado 'n' vezes, onde 'n'<br />

é um número inteiro. A sintaxe do comando for é a seguinte:<br />

exemplo:<br />

for (int i = 0; i < 10; i++)<br />

{<br />

// bloco de comandos<br />

}<br />

/**<br />

* repetição de comandos usando FOR<br />

*/<br />

// Calculando o fatorial de um número:<br />

int numero = 10;<br />

int fatorial = 1;<br />

for (int i = numero; i > 0; i--)<br />

{<br />

fatorial = fatorial * i;<br />

}<br />

System.out.println("fatorial de " + valor<br />

+ " = " + fatorial);<br />

// Imprimindo os dias do ano:<br />

for (int mes = 1; mes < 12; mes++)<br />

{<br />

switch (mes)<br />

{<br />

case 1:<br />

System.out.println("janeiro");<br />

break;<br />

case 2:<br />

System.out.println("março");<br />

break;<br />

case 3:<br />

29

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!