Linguagem C: Variáveis 
Autor: Blog Talento Feminino | Publicado: 2015-11-21T16:39:00-02:00





Olá pessoas, o post de hoje é pra mostrar as variáveis de Algoritmo em Programação em C



Declaração de uma variável

Primeiramente vamos saber o que é uma variável: são espaços de memória reservados que guardam valores durante a execução de um programa.

&nbsp;O tipo de variável informa a quantidade de memória, em bytes, que a variável ocupará.
Em Algoritmo, são as seguintes variáveis:
Caracter: utilizadas para armazenar letras e outros símbolos existentes em textos. (Em C: char)
Inteiro: pode armazenar valores: -10, 10. Ocupa 16 bits. (Em C: int)
Real: como o próprio nome já diz, pode ser usado para armazenar valores: 13.50. No caso, se utiliza pontos no lugar da virgula.) (Em C: float).




Leitura de uma variável do tipo

&nbsp;Antes de colocar a variável, voce deve declarar.

int ( %d)
float (%f)
double (%lf)
char (%c)

Exemplo:

&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;#include &lt;stdio.h&gt;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;main()
&nbsp; &nbsp; &nbsp; &nbsp; {
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;int a;
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;printf("O valor de a = %d\n", a);
&nbsp; &nbsp; &nbsp; &nbsp; &nbsp;}

Escreva / Leia em C

Escreta: Printf
Leia: Scanf
EscrevaL: \n

Comando Se / Senao em C

se: If
senao: Else

Pseudocódigo
algoritmo adicao
&nbsp;&nbsp;&nbsp;&nbsp;declare num1, num2, soma inteiro&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Informe os valores: ')&nbsp;&nbsp;&nbsp;&nbsp;leia (num1,num2)&nbsp;&nbsp;&nbsp;&nbsp;soma &lt;-- num1 + num2&nbsp;&nbsp;&nbsp;&nbsp;se&nbsp;(soma &gt; 10)&nbsp;então&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Soma maior que dez')&nbsp;&nbsp;&nbsp;&nbsp;fim sefim algoritmo
C++#include &lt;iostream.h&gt;void main(){&nbsp;&nbsp;&nbsp;&nbsp;int num1, num2,soma; &nbsp;&nbsp;&nbsp;// declaração de variáveis&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Informe os valores:";&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt;num1&gt;&gt;num2; &nbsp;&nbsp;&nbsp;// leitura dos valores&nbsp;&nbsp;&nbsp;&nbsp;soma=num1+num2;&nbsp;&nbsp;&nbsp;&nbsp;if&nbsp;(soma &gt; 10) &nbsp;&nbsp;&nbsp;// teste de seleção&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Soma maior que 10";}


Comando Enquanto em C


Wile

Comando de repetição

do: Vai fazer a releitura do programa, no caso de erro. Depois que estiver tudo certo, então ele executa o Wile.
Wile: executa o programa.

while&nbsp;– número de repetição indefinida de vezes;
do...while&nbsp;– número de repetição indefinida de vezes, executa pelo menos uma vez;
for&nbsp;– número de repetição definida de vezes.&nbsp;

Exemplo:


Pseudocódigo

Algoritmo multiplica

&nbsp;&nbsp;&nbsp;&nbsp; declare valor1, valor2, produto inteiro

&nbsp;&nbsp;&nbsp;&nbsp; declare resp caractere

&nbsp;&nbsp;&nbsp;&nbsp; resp&lt;-- 'sim'

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;enquanto&nbsp;(resp = 'sim')&nbsp;faça

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; escreva ('Primeiro numero: ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leia (valor1)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; escreva ('Segundo numero: ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leia (valor2)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; produto&lt;-- valor1 * valor2

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; escreva ('Resultado: ', produto)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; escreva ('Deseja continuar? (sim/nao): ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; leia (resp)

&nbsp;&nbsp;&nbsp;&nbsp; fim enquanto

fim algoritmo




C++

#include &lt;iostream.h&gt;

void main( )

{

&nbsp;&nbsp;&nbsp;&nbsp; int num1, num2, prod;

&nbsp;&nbsp;&nbsp;&nbsp; char resp = 's';

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;while&nbsp;(resp == 's')

&nbsp;&nbsp;&nbsp;&nbsp; {

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;"\nInsira dois numeros: ";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;num1&gt;&gt;num2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; prod=num1*num2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;"Produto: "&lt;&lt;prod;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cout&lt;&lt;"\nDeseja continuar? (s/n): ";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; cin&gt;&gt;resp;

&nbsp;&nbsp;&nbsp;&nbsp; }

}




Comando Caso


Exemplo:


Pseudocódigo

algoritmo calculadora;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;declare valor1, valor2 real

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;declare oper caractere

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Calculadora eletrônica')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Primeiro valor: ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leia (valor1)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Operador (+,-,*,/): ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leia (oper)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Segundo valor: ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leia (valor2)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escolha&nbsp;oper

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '+' :&nbsp;escreva ('Soma = ', (valor1 + valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '-'&nbsp; :&nbsp;escreva ('Subtração = ', (valor1 - valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '*', 'x'&nbsp;: escreva ('Multiplicação = ',(valor1 * valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '/', ':' :&nbsp;escreva ('Divisão = ', (valor1 / valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;senão

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Operação inválida')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fim escolha

fim algoritmo




C++

#include &lt;iostream.h&gt;

void main()

{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float n1, n2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char op;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Introduza a operação (1o.numero operador 2o.numero): ";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt;n1&gt;&gt;op&gt;&gt;n2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(op)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '+':&nbsp; cout&lt;&lt;"Soma: "&lt;&lt;n1 + n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '-':&nbsp; cout&lt;&lt;"Subtração: "&lt;&lt;n1 - n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '*':&nbsp;&nbsp;&nbsp;// símbolos opcionais (* ou x) para

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ‘x':&nbsp;&nbsp;&nbsp;// o usuário comandar a multiplicação.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Multiplicação: "&lt;&lt;n1 * n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '/':&nbsp;&nbsp;&nbsp;// símbolos opcionais (/ ou :) para

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ':':&nbsp;&nbsp;&nbsp;// o usuário comandar a divisão.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Divisão: "&lt;&lt;n1 / n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Operador invalido";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

}



Comando Para / Fimpara

for
Exemplo: for (cont= 0; cont &lt;5; cont++)

Exemplo:


Pseudocódigo

para&nbsp;(var=inicio,&nbsp;até&nbsp;limite&nbsp;passo&nbsp;incr ou decr)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; comandos


fim para


&nbsp;

C++

for&nbsp;(inicio; condição; incr ou decr)


&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp; {comandos;}

Define Max 5 &nbsp;o programa vai executar sempre a quantidade de número definido pelo programador, sem a necessidade de mudar o número de cada código.

Comando Escolha/ Fimescolha

switch /&nbsp;default:
&nbsp;Exemplo:


Pseudocódigo

algoritmo calculadora;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;declare valor1, valor2 real

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;declare oper caractere

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Calculadora eletrônica')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Primeiro valor: ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leia (valor1)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Operador (+,-,*,/): ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leia (oper)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Segundo valor: ')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;leia (valor2)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escolha&nbsp;oper

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '+' : escreva ('Soma = ', (valor1 + valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '-'&nbsp; : escreva ('Subtração = ', (valor1 - valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '*', 'x' : escreva ('Multiplicação = ',(valor1 * valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;caso '/', ':' : escreva ('Divisão = ', (valor1 / valor2))

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;senão

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;escreva ('Operação inválida')

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;fim escolha

fim algoritmo




C++

#include &lt;iostream.h&gt;

void main()

{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;float n1, n2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;char op;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Introduza a operação (1o.numero operador 2o.numero): ";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cin&gt;&gt;n1&gt;&gt;op&gt;&gt;n2;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;switch&nbsp;(op)

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;{

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '+':&nbsp; cout&lt;&lt;"Soma: "&lt;&lt;n1 + n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '-':&nbsp; cout&lt;&lt;"Subtração: "&lt;&lt;n1 - n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '*':&nbsp;&nbsp;&nbsp;// símbolos opcionais (* ou x) para

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ‘x':&nbsp;&nbsp;&nbsp;// o usuário comandar a multiplicação.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Multiplicação: "&lt;&lt;n1 * n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case '/':&nbsp;&nbsp;&nbsp;// símbolos opcionais (/ ou :) para

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;case ':':&nbsp;&nbsp;&nbsp;// o usuário comandar a divisão.

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Divisão: "&lt;&lt;n1 / n2; break;

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;default:

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;cout&lt;&lt;"Operador invalido";

&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;}

}



Bom é isso, espero ter ajudado voces.


Fonte:&nbsp;

http://www.univasf.edu.br/~leonardo.campos/Arquivos/Disciplinas/Alg_Prog_I_2007_1/Aula_02.pdf

http://bianchi.pro.br/c++/c++p2.php