Skip to content

Instantly share code, notes, and snippets.

@SpinnerZ
Created August 31, 2024 14:47
Show Gist options
  • Select an option

  • Save SpinnerZ/8a68a1b0c874bb95c8d5e37415114d4f to your computer and use it in GitHub Desktop.

Select an option

Save SpinnerZ/8a68a1b0c874bb95c8d5e37415114d4f to your computer and use it in GitHub Desktop.
Estruturas de repetição em Java: while, do-while e for
package org.example;
import java.util.Scanner;
public class Exercicio {
/*Faça um programa que receba um número e,
usando laços de repetição,
calcule e mostre a tabuada desse número. */
public static void main(String[] args) {
Scanner scanner = new Scanner(System.in);
int number;
System.out.println("\nOlá! Insira o número para calcular a tabuada.");
number = scanner.nextInt();
for (int i = 0; i <= 10; i++)
System.out.printf("\n\n%d x %d = %d", number, i, (number * i));
scanner.close();
}
}
package org.example;
public class For {
public static void main(String[] args) {
// Todos os parâmetros do for são opcionais
// Este for percorre de 0 a 10
for (int i = 0; i <= 5 ; i++) {
// Se for par, execute este bloco de código
if (i % 2 == 0) {
System.out.println("Entrou no if, portanto é par");
// Bloco de código do if
// A variável "i" ainda é válida aqui pois está dentro do escopo do primeiro for
for (int j = -1; j < 2; j++) {
System.out.println("Início do for menor");
System.out.println("Valor do i: " + i);
System.out.println("Valor do j: " + j);
if (j >= 0) break;
}
}
}
}
}
package org.example;
public class While {
public static void main(String[] args) {
int number = 10;
do {
number++;
System.out.println("Valor do número dentro do DO: " + number);
} while (number < 10);
number = 10;
while (number < 10) {
number++;
if (number % 2 != 0) continue;
System.out.printf("\nValor do número dentro do WHILE: %d", number);
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment