Escolar Documentos
Profissional Documentos
Cultura Documentos
1. SENTENCIA IF
if (condición) sentencia;
EJEMPLO #1
DIVISION DE NÚMEROS ENTEROS
package estructuras.de.seleccion;
/**
*
* @author JRVV
*/
public class IF {
public static void main(String[] args) {
int x = 12;
int y = 0;
int z = 0;
if( y !=0 ) z = x / y;
System.out.println("El resultado es : " + z);
}}
2. SENTENCIA IF ELSE
if (condición) sentencia 1;
else sentencia 2;
EJEMPLO #2
DIVISION DE NÚMEROS ENTEROS, VARIANTE
package estructuras.de.seleccion;
/**
*
* @author JRVV
*/
public class IFELSE {
public static void main(String[] args) {
int x = 12;
int y = 0;
int z = 0;
if( y !=0 ) z = x / y;
else System.out.println("Mensaje! se pretende dividir por 0");
System.out.println("El resultado es : " + z); }}
package estructuras.de.seleccion;
/**
*
* @author JRVV
*/
public class IFELSE2 {
public static void main(String[] args) {
int x = 12;
int y = 2;
int z = 0;
if( y !=0 ) {
z = x / y;
System.out.println("El resultado es : " + z);
} else {
System.out.println("Mensaje! se pretende dividir por 0");
}
}
}
Sintaxis:
if (condición) {
sentencia;
} else {
sentencia;
}