Author Archives: Diegops

NESTED IF AND ELSE.

 

DIEGO PLASCENCIA SANABRIA      A01229988

Los if y los else, toman una desicion entre dos alternativas diferentes, pero a veces  hay mas de dos alternativas para elegir, en estas ocaciones se anidan los else y los if.

Aqui hay un ejemplo de como se deberia escribir un programa con else/if cuando se tienen distintas alternativas (mas de dos):

if ( condition1 )
statement1 ;
else if ( condition2 )
statement2 ;
. . .
else if ( condition-n )
statement-n ;
else
statement-e ;

Aqui otro ejemplo sobre lo mismo pero ahora usando constantes:

if (x < 0.25)
  count1++;
else if (x >= 0.25 && x < 0.5)
  count2++;
else if (x >= 0.5 && x < 0.75)
  count3++;
else
  count4++;

Gracias por leerlo, espero haberte ayudado,
si quieres mas informacion puedes visitar este link:
http://www.macs.hw.ac.uk/~pjbk/pathways/cpp1/node99.html

SWITCH

 

DIEGO PLASCENCIA SANABRIA      A01229988

El swich case se usa cuando se necesita evaluar muchas condiciones a la vez.

La instruccion switch es de desicion multiple, donde el compilador busca en una lista de constantes y cuando encuentra el valor de igualdad de constante y variable, ejecuta las instrucciones asociadas.

Las constantes SOLO pueden ser de tipo INT o CHAR.

SIEMPRE despues de cada instruccion, se escribe un // break;

Aqui hay un ejemplo de como se hace:

switch(var int o char)

{

case const1: instrucción(es);

break;

case const2: instrucción(es);

break;

case const3: instrucción(es);

break; ………………

default: instrucción(es);

};

GRACIAS POR LEERLO, ESPERO HABER AYUDADO, PARA MAS INFORMACION VISITA ESTE LINK:

https://www.programacionfacil.com/cpp/instruccion_switch

Untitled

 

DIEGO PLASCENCIA SANABRIA  A01229988

An if statement can be followed by an optional statement else, it executes when the value for the if is false.

Syntax:

if(boolean_expression 1) {

// Executes when the boolean expression 1 is true }

else if( boolean_expression 2) {

// Executes when the boolean expression 2 is true }

else if( boolean_expression 3) {

// Executes when the boolean expression 3 is true }

else {

// executes when the none of the above condition is true. }

Here is a flow diagram:

thank you for reading it, I hope that it helped you. for more info and examples check this link:

http://www.tutorialspoint.com/cplusplus/cpp_if_else_statement.htm

CONDITIONALS

DIEGO PLASCENCIA SANABRIA        A01229988

The if statement allows you to control if a program enters a section of code or not based on whether a given condition is true or false.  A true statement is one that evaluates to a nonzero numbeR and a false statement evaluates to zero. Example:

 the check 0 == 2 evaluates to 0. The check 2 == 2 evaluates to a 1.

Structure of a statement:

if (statement is TRUE)

         execute the line of code

 Relational operators:

5>4     greater than                   5 > 4 is TRUE

4<5     less than                        4 < 5 is TRUE

4>=4    greater than or equal    4 >= 4 is TRUE

3<=4    less than or equal         3 <= 4 is TRUE

5==5    equal to                         5 == 5 is TRUE

 

5!=4    not equal to                    5 != 4 is TRUE

example:

 if (8>4){

          cout<<“YA SABES CONDICIONALES”<<endl;

De aqui saque la informacion y hay aun mas informacion por si te interesa saber mas:

http://www.cprogramming.com/tutorial/c/lesson2.html

FUNCTIONS

 #TC1017  

DIEGO PLASCENCIA A01229988

PROGRAMA CON FUNCIONES DE SUMA, RESTA, MULTIPLICACION, DIVISION Y % CON VARIABLES DEFINIDAS.

<iostream>

using namespace std;

int fsuma(int num1, int num2){   

int resultadosuma;

resultadosuma= num1+num2;

return resultadosuma;}

 

int fresta(int num1, int num2){

int resultadoresta;

resultadoresta= num1-num2;

return resultadoresta;}

 

int fproducto(int num1, int num2){

int resultadoproducto;

resultadoproducto= num1*num2;

return resultadoproducto;}

 

int fdivision(int num1, int num2){

int resultadodivision;

resultadodivision= num1/num2;

return resultadodivision;}

 

int fresiduo(int num1, int num2){

int resultadoresiduo;

resultadoresiduo= num1%num2;

return resultadoresiduo;}

 

int main (){

int suma,resta,producto,division,residuo;

int a=7;

int b=3;

 

suma=fsuma(a,b);

cout<< “EL RESULTADO DE LA SUMA ES: “<<suma<< endl;

 

resta=fresta(a,b);

cout<< “EL RESULTADO DE LA RESTA ES: “<<resta<< endl;

 

producto=fproducto(a,b);

cout<< “EL RESULTADO DEL PRODUCTO ES: “<<producto<< endl;

 

division=fdivision(a,b);

cout<< “EL RESULTADO DE LA DIVISION ES: “<<division<< endl;

 

residuo=fresiduo(a,b);

cout<< “EL RESULTADO DEL RESIDUO ES: “<<residuo<< endl;}

FUNCTIONS

DIEGO PLASCENCIA A01229988

FUNCTON EXAMPLE.

IN AND OUT

DIEGO PLASCENCIA A01229988

FOR PRINTING SOME INFORMATION YOU USE:

cout<< //write the information you will like the user to see.

cin>> //here is where the user will introduce information.

always finish with ;

BASIC TYPES

DIEGO PLASCENCIA A01229988

WHILE PROGRAMING YOU NEED TO USE VARIOUS VARIABLES TO STORE DIFFERENT TYPES OF INFORMATION (FOR MOR INFO VISIT THIS SITE http://www.tutorialspoint.com/cplusplus/cpp_data_types.htm).

THE TYPES ARE:

Type Keyword
Boolean bool
Character char
Integer int
Floating point float
Double floating point double
Valueless void
Wide character wchar_t

AND THESE ARE THEIR CHARACTERISTICS:

Type Typical Bit Width Typical Range
char 1byte -127 to 127 or 0 to 255
unsigned char 1byte 0 to 255
signed char 1byte -127 to 127
int 4bytes -2147483648 to 2147483647
unsigned int 4bytes 0 to 4294967295
signed int 4bytes -2147483648 to 2147483647
short int 2bytes -32768 to 32767
unsigned short int Range 0 to 65,535
signed short int Range -32768 to 32767
long int 4bytes -2,147,483,647 to 2,147,483,647
signed long int 4bytes same as long int
unsigned long int 4bytes 0 to 4,294,967,295
float 4bytes +/- 3.4e +/- 38 (~7 digits)
double 8bytes +/- 1.7e +/- 308 (~15 digits)
long double 8bytes +/- 1.7e +/- 308 (~15 digits)
wchar_t 2 or 4 bytes 1 wide character

CONVENTIONS.

DIEGO PLASCENCIA A01229988

I DID NOT KNOW THAT THERE WERE SO MANY RULES FOR PROGRAMING, IT IS NOT ONLY WRITING THE PROGRAM, YOU MUST FOLLOW A GROUP OF RULES THAT INCLUDE STRUCTURE, NAMING CONVENTIONS,

YOU CAN READ MORE ON THIS LINK:

http://symfony.com/doc/current/contributing/code/standards.html

COMMENTS

DIEGO PLASCENCIA A01229988

COMMENTS ARE USED TO EXPLAIN YOURSELF OR TO REMEMBER THINGS OF YOUR PROGRAM. THERE ARE MANY WAYS TO COMMENT ON C++

(YOU USE “/*” TO START THE COMMENT AND “*/” TO FINISH IT, THIS IS USED NORMALY FOR BIG COMMENTS. EXAMPLES:

/* COMMENT */

/* A

   VERY

    BIG

    COMMENT*/

IF YOU WILL WRITE A SMALL COMMENT YOU CAN SIMPLY USE DOUBLE “/”. EXAMPLE:

// COMMENT.

HERE IS A GREAT LINK THAT COULD HELP YOU TO UNDERSTAND BETTER THE COMMENTS.

http://www.tutorialspoint.com/cplusplus/cpp_comments.htm