Вы находитесь на странице: 1из 6

INSTRUCCIONES CONDICIONALES

Sentencia IF

Sintaxis: if (condicion_logica)
<instrtrucciones>

falso
condicion_logica

verdadero

instrucciones

Ejemplo : Leer una edad y reportar si es mayor de edad.

INICIO
IMPRIMIR (ingrese edad)
LEER edad
SI (edad >= 18)
IMPRIMIR (mayor de edad)
FIN SI
FIN

import java.io.*;
class ejemplo1
{
public static void main(String arg[]) throws IOException
{
BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Ingrese edad : ");
int edad = Integer.parseInt(leer.readLine( ));
if(edad >= 18)
{
System.out.println("Es mayor de edad");
}
}
}
Sentencia IF ELSE

Sintaxis: if (condicion_logica)
<instrucciones_v>
else
<instrucciones_f>

condicion_logica
verdadero Falso

intrucciones_v instrucciones_f

INICIO
IMPRIMIR (ingrese edad)
LEER edad
SI (edad >= 18)
IMPRIMIR (mayor de edad)
SINO
IMPRIMIR (menor de edad)
FINSI
FIN

import java.io.*;
class ejemplo2
{
public static void main(String arg[]) throws IOException
{
BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Ingrese edad : ");
int edad = Integer.parseInt(leer.readLine( ));
if(edad >= 18)
{
System.out.println("Es mayor de edad");
}
else
{
System.out.println("Es menor de edad");
}
}
}
IF Anidados

Sintaxis: if (condicion_logica_1)
<instrucciones_1>
else {
if (condicion_logica_2)
<instrucciones_2>
else {
...
}
}

verdadero falso
condicion_logica_1

verdadero falso
instrucciones_1 condicion_logica_2

instrucciones_2

Ejemplo: Ingresar un nmero y reportar si este es positivo, negativo o neutro

INICIO
IMPRIMIR (ingrese numero)
LEER numero
SI (numero > 0)
IMPRIMIR (positivo)
SINO
SI (numero < 0)
IMPRIMIR (negativo)
SINO
IMPRIMIR (neutro)
FINSI
FINSI
FIN
import java.io.*;
class ejemplo3
{
public static void main(String arg[]) throws IOException
{
BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Ingrese numero : ");
int numero = Integer.parseInt(leer.readLine( ));
if(numero > 0)
{
System.out.println("Es positivo");
}
else
{
if(numero < 0)
{
System.out.println("Es negativo");
}
else
{
System.out.println("Es neutro");
}
}
}
}

Sentencia IF-ELSE-IF

Sintaxis: if (condicion_logica_1)
<instrucciones_1>
else if (condicion_ligica_2)
<instrucciones_2>
...
else if (condicion_logica_N-1)
<instrucciones_N-1>
else
<instrucciones_N>

Ejemplo: calcular los descuentos que otorga una tienda segn el consumo: si el consumo es menor
de S/.30 el descuento es del 10%, si el consumo es hasta S/.60 el descuento es del 15%, si el
consumo es hasta S/.100 el descuento es del 20% y si el consumo mayor de S/.100 el descuento es
del 30%
INICIO
IMPRIMIR (ingrese consumo)
LEER consumo
SI (consumo <= 30)
IMPRIMIR (descuento = 10%consumo)
SINO SI (consumo > 30 y consumo <= 60)
IMPRIMIR (descuento = 15%consumo)
SINO SI (consumo > 60 y consumo <= 100)
IMPRIMIR (descuento = 20%consumo)
SINO
IMPRIMIR (descuento = 30%consumo)
FINSI
FIN

import java.io.*;
class ejemplo4
{
public static void main(String arg[]) throws IOException
{
BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
System.out.print("Ingrese consumo : ");
float consumo = Float.parseFloat(leer.readLine( ));
if(consumo <= 30)
{
System.out.println("El descuento es: "+consumo*0.10);
}
else if(consumo > 30 && consumo <= 60)
{
System.out.println("El descuento es: "+consumo*0.15);
}
else if(consumo > 60 && consumo <= 100)
{
System.out.println("El descuento es: "+consumo*0.20);
}
else
{
System.out.println("El descuento es: "+consumo*0.30);
}
}
}

Sentencia SWITCH

switch (selector)
{
case <etiqueta1>: <sentencias1>;
break;
case <etiqueta2>: <sentencias2>;
break;
...
case <etiquetan>: <sentenciasn>;
break;
default: <sentenciasd>; // opcional
}
INICIO
Resultado = 0
IMPRIMIR (ingrese n1)
LEER n1
IMPRIMIR (ingrese n2)
LEER n2
IMPRIMIR (suma...[1])
IMPRIMIR (resta.[2])
IMPRIMIR (multiplicacin[3])
IMPRIMIR (division..[4])
IMPRIMIR (ingrese opcion : )
LEER opcion
SELECCION (opcion)
CASO 1 : resultado = n1 + n2
CASO 2 : resultado = n1 - n2
CASO 3 : resultado = n1 * n2
CASO 4 : resultado = n1 / n2
OTROCASO : IMPRIMIR (opcion no valida)
FINSELECCION
IMPRIMIR (resultado)
FIN

import java.io.*;
class ejemplo5
{
public static void main(String arg[]) throws IOException
{
BufferedReader leer = new BufferedReader(new InputStreamReader(System.in));
float resultado = 0;
System.out.print("Ingrese primer numero : ");
float n1 = Float.parseFloat(leer.readLine( ));
System.out.print("Ingrese segundo numero : ");
float n2 = Float.parseFloat(leer.readLine( ));
System.out.println(" SUMA.............[1]");
System.out.println(" RESTA............[2]");
System.out.println(" MULTIPLICACION...[3]");
System.out.println(" DIVISION.........[4]");
System.out.print("Ingrese su opcion : ");
int opcion = Integer.parseInt(leer.readLine( ));
switch (opcion)
{
case 1 : resultado = n1 + n2;
break;
case 2 : resultado = n1 - n2;
break;
case 3 : resultado = n1 * n2;
break;
case 4 : resultado = n1 / n2;
break;
default : System.out.println("opcion no valida");
}
System.out.println("resultado = " + resultado);
}
}

Вам также может понравиться