Documente Academic
Documente Profesional
Documente Cultură
Sentencia IF.
a)
if (cond) {
//aquí todas las sentencias en caso de que cond sea verdad
}
else
{
// aquí todas las sentencias cuando la cond sea falsa
}
if (cond) {
// cuando la respuesta es verdad
}
== igual
> mayor
< menor
¡= diferente
>= mayor igual
<= menor igual
También se puede programar haciendo un nido de if ‘s. Es decir, un if dentro de otro if.
import java.io.*;
public class ej5
{
public static void main(String args[])throws IOException
{BufferedReader in;
2
Ejemplo 6. Este programa pide al usuario un número entero luego le indica si es positivo
o negativo.
import java.io.*;
public class ej6
{
public static void main(String args[])throws IOException
{BufferedReader in;
in=new BufferedReader(new InputStreamReader(System.in));
System.out.println("ingrese un # entero");
int a=Integer.parseInt(in.readLine());
if(a%2==0)
{System.out.println("El numero es par");}
else
{System.out.println("El numero es impar");}
}
}
Ejemplo 7. Este ejemplo pide un número al usuario luego le indica si es cero, negativo o
positivo. Se resalta con amarillo el if interno, en este nido de if ‘s.
import java.io.*;
public class ej7
{
public static void main (String args[])throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Ingrese un numero: ");
int d = Integer.parseInt(in.readLine());
if (d==0)
{System.out.print(" el numero es cero ");}
else
{
3
Ejemplo 9 Programa que lee un par x,y luego indica en qué cuadrante se encuentra el
número. Observe que en este ejemplo no se utiliza else.
import java.io.*;
public class ej9
{
public static void main (String args[])throws IOException
{
BufferedReader in;
in = new BufferedReader (new InputStreamReader (System.in));
System.out.println("Ingrese x ");
int x = Integer.parseInt(in.readLine());
System.out.println("Ingrese y ");
int y = Integer.parseInt(in.readLine());
if ((x>0) && (y>0))
4