22.02.2016 Views

Operadores y tipos de variables

Create successful ePaper yourself

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

<strong>Operadores</strong> y <strong>tipos</strong><br />

<strong>de</strong> <strong>variables</strong><br />

YENIFFER CATERINE CARDENAS GALIS<br />

ING.SISTEMAS<br />

UNIVERSIDAD POPULAR DEL CESAR


Los operadores relacionales son<br />

símbolos que se usan para comparar<br />

dos valores. Si el resultado <strong>de</strong> la<br />

comparación es correcto la<br />

expresión consi<strong>de</strong>rada es verda<strong>de</strong>ra,<br />

en caso contrario es falsa.<br />

Ejemplos<br />

Si a = 10, b = 20, c = 30<br />

a + b > c Falso<br />

a - b < c Verda<strong>de</strong>ro<br />

a - b = c Falso<br />

a * b < > c Verda<strong>de</strong>ro<br />

b / a


Los operadores lógicos nos<br />

proporcionan un resultado a partir<br />

<strong>de</strong> que se cumpla o no una cierta<br />

condición para darle a una búsqueda<br />

un or<strong>de</strong>n lógico.<br />

Ejemplo en Matemáticas


Ejemplos en programación<br />

<br />

<br />

#inclu<strong>de</strong> <br />

bool alto = true, bajo = false, blanco = true, negro =<br />

false;<br />

int main (void) {<br />

if (alto && bajo) { cout


#inclu<strong>de</strong> <br />

bool alto = true, bajo = false;<br />

int main (void) {<br />

if (alto) { cout


El álgebra <strong>de</strong> operadores está basada<br />

en el estudio <strong>de</strong> los operadores. En<br />

términos matemáticos, un operador<br />

pue<strong>de</strong> consi<strong>de</strong>rarse un símbolo o<br />

notación abreviada <strong>de</strong> un conjunto<br />

<strong>de</strong>finido, no vacío, <strong>de</strong> operaciones o<br />

<strong>de</strong> acciones a realizar con o sobre un<br />

término <strong>de</strong>nominado operando, el<br />

cual se escribe a la <strong>de</strong>recha y que da<br />

como resultado otro “objeto” <strong>de</strong><br />

igual o diferente naturaleza.


Ejemplos en programación<br />

int ai[] = {2, 3};<br />

int* ptr = ai;<br />

int r1 = +ai[0]; // L.3: más unitario<br />

sobre tipo numérico<br />

int r2 = -ai[1] // L.3: menos<br />

unitario sobre tipo numérico<br />

int r3 = ai[0] + ai[1]; // L.4: 2 + 3<br />

suma binaria (<strong>de</strong> enteros)<br />

int r4 = ai[1] - ai[0] // L.5: 3 - 2 resta<br />

binaria (<strong>de</strong> enteros)<br />

int r5 = ai[0] + -ai[1]; // L.6: 2 +(-3)<br />

suma binaria seguida <strong>de</strong> menos<br />

unitario<br />

int r6 = *ptr + -*++ptr; // L.7: Suma<br />

binaria (<strong>de</strong> enteros) a + (-b)


int x = 10, y = 20;<br />

int z = x + y; // z == 30<br />

int arr[5] = {1, 2, 3, 4, 5};<br />

int* ptr = &arr[0]; // Señala a 1<br />

int x = *(2 + ptr); // x == 3<br />

int x = 7, y = 3;<br />

int r1 = - (y - x); // r1 == 4<br />

int r2 = + (y - x); // r2 == -4<br />

int r3 = - (x - y); // r3 == -4<br />

int r4 = + (x - y); // r4 == 4<br />

int resto = (6 % 4);<br />

cout


Tipos <strong>de</strong> Variables<br />

Numéricas<br />

Ca<strong>de</strong>nas <strong>de</strong> Texto<br />

Arrays<br />

Booleanos


Numéricas<br />

Se utilizan para almacenar valores<br />

numéricos enteros o <strong>de</strong>cimales . En<br />

este caso, el valor se asigna<br />

indicando directamente el número<br />

entero o <strong>de</strong>cimal.<br />

Ejemplos<br />

vari1 = new Number(16);<br />

vari2 = new Number(3.141592);


var1 = new Number(3.141592); var<br />

var2 = variable1.toFixed(); //variable2 = 3 var3<br />

= variable1.toFixed(2); // vari3 = 3.14<br />

var4 = variable1.toFixed(10); // var4 =<br />

3.1415920000<br />

numero1 = new Number(0.235<br />

numero2 = new Number(1.235);<br />

numero1 = new Number(162.295);<br />

numero2 = numero1 * new Number(100);<br />

var iva = 16; // variable tipo entero var total =<br />

234.65; // variable tipo <strong>de</strong>cimal


Ca<strong>de</strong>nas <strong>de</strong> Texto<br />

Se utilizan para almacenar caracteres,<br />

palabras y/o frases <strong>de</strong> texto. Para<br />

asignar el valor a la variable, se<br />

encierra el valor entre comillas dobles<br />

o simples, para <strong>de</strong>limitar su comienzo<br />

y su final.<br />

Ejemplos<br />

char ca<strong>de</strong>na_hola[]="Hola";<br />

char otro_hola[]={'H','o','l','a','\0'}; //<br />

Igual al anterior<br />

char vector[]={'H','o','l','a'}; /* Un<br />

vector <strong>de</strong> 4 elementos, con los<br />

elementos 'H','o','l' y 'a' */<br />

char espacio_ca<strong>de</strong>na[1024]="Una<br />

ca<strong>de</strong>na en C"; char ca<strong>de</strong>na_vacia[]="";


int largo_ca<strong>de</strong>na(char ca<strong>de</strong>na[]) {<br />

int largo=0 while<br />

(ca<strong>de</strong>na[largo]!='\0') largo++;<br />

return largo;<br />

char nombre[20];<br />

printf( "Introduzca su nombre (20<br />

letras máximo): " );<br />

scanf( "%s", nombre ); printf( "\nEl<br />

nombre que ha escrito es:<br />

%s\n", nombre ); }<br />

char nombre[] = "Gandalf"; printf(<br />

"Texto: %s\n", nombre ); printf(<br />

"Tamaño <strong>de</strong> la ca<strong>de</strong>na: %i bytes\n",<br />

sizeof nombre );


Arrays<br />

Un array es una colección <strong>de</strong><br />

<strong>variables</strong>, que pue<strong>de</strong>n ser todas <strong>de</strong>l<br />

mismo tipo o cada una <strong>de</strong> un tipo<br />

diferente. Su utilidad se compren<strong>de</strong><br />

mejor con un ejemplo sencillo: si<br />

una aplicación necesita manejar los<br />

días <strong>de</strong> la semana, se podrían crear<br />

siete <strong>variables</strong> <strong>de</strong> tipo texto.<br />

Ejemplos<br />

var dias = ["Lunes", "Martes",<br />

"Miércoles", "Jueves", "Viernes",<br />

"Sábado", "Domingo"];


var diaSeleccionado = dias[0]; //<br />

diaSeleccionado = "Lunes" var<br />

otroDia = dias[5]; // otroDia =<br />

"Sábado“<br />

int[ ] numero<strong>de</strong>coches = new<br />

int[24];<br />

int r;<br />

r = 2;<br />

numero<strong>de</strong>coches[r] = 23;<br />

System.out.print("El número <strong>de</strong><br />

coches en la hora "+r+" fue "+<br />

numero<strong>de</strong>coches[r]);


int[ ] numero<strong>de</strong>coches = new<br />

int[24];<br />

int a, b;<br />

int r;<br />

a = 8;<br />

b = 4;<br />

r = a / b;<br />

numero<strong>de</strong>coches[r] = 23;<br />

System.out.print("El número <strong>de</strong><br />

coches en la hora "+r+" fue "+<br />

numero<strong>de</strong>coches[r]);


Booleanos<br />

Las <strong>variables</strong> <strong>de</strong><br />

tipo boolean o booleano también se<br />

conocen con el nombre <strong>de</strong> <strong>variables</strong><br />

<strong>de</strong> tipo lógico. Aunque para<br />

enten<strong>de</strong>r realmente su utilidad se<br />

<strong>de</strong>be estudiar la programación<br />

avanzada con JavaScript <strong>de</strong>l<br />

siguiente capítulo, su<br />

funcionamiento básico es muy<br />

sencillo.


Ejemplos<br />

var clienteRegistrado = false;<br />

var ivaIncluido = true;<br />

Dim runningVB As Boolean<br />

' Check to see if program is running<br />

on Visual Basic engine.<br />

If scriptEngine = "VB" Then<br />

runningVB = True End If<br />

• public class Condicional {<br />

public static void main(String<br />

args[]){ int x = 0; int y = 2;<br />

boolean b = ( x != 0 ) && ( ( y / x<br />

) != 0 ); System.out.println(b);


Bibliografía<br />

• http://www.sc.ehu.es/sbweb/fisica/cursoJava/f<br />

undamentos/introduccion/operadores1.htm<br />

• https://es.wikipedia.org/wiki/Operador#Opera<br />

dores_l.C3.B3gicos<br />

• http://matematica.laguia2000.com/general/alg<br />

ebra-<strong>de</strong>-operadores#ixzz40vnsDLtD<br />

• http://librosweb.es/libro/javascript/capitulo_3/<br />

<strong>tipos</strong>_<strong>de</strong>_<strong>variables</strong>.html

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

Saved successfully!

Ooh no, something went wrong!