terça-feira, 8 de abril de 2014

If - If Aninhados - Else - Else If - Break - Continue

O if e o switch são dois tipos de comandos de seleção suportados pela linguagem C.Neste código só usei o if, como seleção, e sua forma de uso é:
if ( condição )
comando
else
comando

É aceito desde um único comando, ou até mesmo um bloco, depende de cada caso, más se o comando é vazio, não se faz necessário o uso do else.

Por favor me entenda, que eu não posso falar aqui de cada um dos comandos citados no título do post, iria ficar enfadonho,Só quero deixar claro que pra você conseguir sucesso no seu aprendizado em linguagem C, é de suma importância entender o funcionamento destes comandos que estão sempre juntos desempenhando papéis diferentes porém fundamental, por isso, prefiro mostrar na prática como usá-los.
O código é totalmente didático, e deve ser bem aproveitado por iniciantes em linguagem C.

Veja algumas imagens do programa abaixo:










Veja abaixo o código do Programa:

#include <conio.h>
#include <stdio.h>
#include <stdlib.h>
void Janela5 ( ) {
     int lin, col;
     col = 0;
     for ( lin = 0; lin <= 25 ; lin++ ) {
         for ( col = 0; col <= 80 ; col++ ) {
              gotoxy ( col , lin );
              if ( lin == 2 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( col == 1 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
              if ( lin == 25 ) {
                   textbackground ( LIGHTBLUE );
              }
              if ( col == 80 ) {
                   textbackground ( LIGHTBLUE );
                   printf ( " " );
              }
         }
     }
     textbackground ( BLACK );
}
int continuando ( ) {
     int nr;
     do {
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 16 , 3 );
         printf ( "IF - IF ANINHADOS - ELSE - ELSE IF - BREAK - CONTINUE" );
         textcolor ( YELLOW );
         gotoxy ( 24 , 5 );
         printf ( "Programa desenvolvido por:" );
         textcolor ( LIGHTGRAY );
         gotoxy ( 51 , 5 );
         printf ( "Samuel Lima" );
         textcolor ( LIGHTGREEN );
         gotoxy ( 34 , 7 );
         printf ( "sa_sp10@hotmail.com" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 24 , 9 );
         printf ( "Digite 0 para sair ou 1 para continuar " );
         textcolor ( LIGHTRED );
         scanf ( "%d" , &nr );
         fflush ( stdin );
         fflush ( stdin );
         if ( nr == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 33 , 18 );
              printf ( "O ROCCO AGRADECE!" );
              getche ( );
              exit ( 0 );
         } else if ( nr == 1 ) {
              return 1;
         }
         textcolor ( YELLOW );
         gotoxy ( 26 , 16 );
         printf ( "\aopcao errada!" );
         getche ( );
     } while ( 1 );
     return 1;
}
int main ( ) {
     int gools, renda, publico;
     char vitoria [ 2 ];
     system ( "title IF - IF ANINHADOS - ELSE - ELSE IF - BREAK - CONTINUE" );
     continuando ( );
     do {
         system ( "cls" );
         Janela5 ( );
         textcolor ( LIGHTRED );
         gotoxy ( 18 , 3 );
         printf ( "IF - IF ANINHADOS - ELSE - ELSE IF - BREAK - CONTINUE" );
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 5 );
         puts ( "Informe os gools do seu Time, ou [ 0 ] para sair " );
         textcolor ( LIGHTRED );
         gotoxy ( 70 , 5 );
         scanf ( "%d" , &gools );
         fflush ( stdin );
         if ( gools == 0 ) {
              textcolor ( LIGHTRED );
              gotoxy ( 33 , 18 );
              printf ( "O ROCCO AGRADECE!" );
              getche ( );
              exit ( 0 );
         }
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 7 );
         printf ( "Informe a Renda do Jogo " );
         textcolor ( LIGHTRED );
         gotoxy ( 44 , 7 );
         scanf ( "%d" , &renda );
         fflush ( stdin );
         textcolor ( LIGHTBLUE );
         gotoxy ( 20 , 9 );
         printf ( "Informe o Publico do Jogo " );
         textcolor ( LIGHTRED );
         gotoxy ( 47 , 9 );
         scanf ( "%d" , &publico );
         fflush ( stdin );
         if ( gools <= 2 ) {
              textcolor ( YELLOW );
              gotoxy ( 26 , 11 );
              printf ( "Seu Time ganhou!" );
              getche ( );
              gotoxy ( 26 , 13 );
              if ( renda <= 1000 ) {
                   printf ( "Renda Muito Baixa" );
                   getche ( );
              }
              gotoxy ( 26 , 15 );
              if ( publico <= 1000 ) {
                   printf ( "Publico Muito Pequeno" );
                   getche ( );
              }
              continue;
              gotoxy ( 68 , 5 );
         } else if ( gools <= 4 ) {
              textcolor ( YELLOW );
              gotoxy ( 26 , 11 );
              printf ( "Seu Time ganhou bem" );
              getche ( );
              gotoxy ( 26 , 13 );
              if ( renda <= 10000 ) {
                   printf ( "Renda Baixa" );
                   getche ( );
              }
              gotoxy ( 26 , 15 );
              if ( publico <= 10000 ) {
                   printf ( "Publico Pequeno" );
                   getche ( );
              }
              continue;
              gotoxy ( 68 , 5 );
         } else if ( gools <= 6 ) {
              textcolor ( YELLOW );
              gotoxy ( 26 , 11 );
              printf ( "Seu Time ganhou de goleada" );
              getche ( );
              gotoxy ( 26 , 13 );
              if ( renda <= 20000 ) {
                   printf ( "Renda Razoavel" );
                   getche ( );
              }
              gotoxy ( 26 , 15 );
              if ( publico <= 20000 ) {
                   printf ( "Publico Razoavel" );
                   getche ( );
              }
              continue;
              gotoxy ( 68 , 5 );
         } else if ( gools <= 8 ) {
              textcolor ( YELLOW );
              gotoxy ( 26 , 11 );
              printf ( "Seu Time atropelou" );
              getche ( );
              gotoxy ( 26 , 13 );
              if ( renda <= 30000 ) {
                   printf ( "Boa Renda" );
                   getche ( );
              }
              gotoxy ( 26 , 15 );
              if ( publico <= 30000 ) {
                   printf ( "Bom Publico" );
                   getche ( );
              }
              continue;
              gotoxy ( 68 , 5 );
         } else if ( gools <= 10 ) {
              textcolor ( YELLOW );
              gotoxy ( 26 , 11 );
              printf ( "Seu Time humilhou o adversario!" );
              getche ( );
              gotoxy ( 26 , 13 );
              if ( renda <= 40000 ) {
                   printf ( "Excelente Renda!" );
                   getche ( );
              }
              gotoxy ( 26 , 15 );
              if ( publico <= 40000 ) {
                   printf ( "Excelente Publico!" );
                   getche ( );
              }
              continue;
              gotoxy ( 68 , 5 );
         }
         do {
              system ( "cls" );
              Janela5 ( );
              textcolor ( LIGHTRED );
              gotoxy ( 18 , 3 );
              printf ( "IF - IF ANINHADOS - ELSE - ELSE IF - BREAK - CONTINUE" );
              textcolor ( LIGHTBLUE );
              gotoxy ( 20 , 5 );
              printf ( " %d gools? tem certeza que foi isso tudo? s/n ? " ,
                        gools );
              textcolor ( LIGHTRED );
              gotoxy ( 69 , 5 );
              scanf ( " %c" , vitoria );
              fflush ( stdin );
              if ( *vitoria == ( 's' ) || *vitoria == ( 'S' ) ) {
                   textcolor ( YELLOW );
                   gotoxy ( 26 , 10 );
                   puts ( "Seu Time e um arraso!" );
                   getche ( );
                   gotoxy ( 26 , 12 );
                   if ( renda <= 50000 ) {
                        printf ( "Ele Merece toda essa Renda!" );
                        getche ( );
                   }
                   gotoxy ( 26 , 14 );
                   if ( publico <= 50000 ) {
                        printf ( "Ele Merece todo esse publico!" );
                        getche ( );
                   }
                   break;
              }
              if ( *vitoria == ( 'n' ) || *vitoria == ( 'N' ) ) {
                   textcolor ( YELLOW );
                   gotoxy ( 26 , 10 );
                   puts ( "Houve um engano " );
                   getche ( );
                   gotoxy ( 26 , 12 );
                   if ( renda <= 60000 ) {
                        printf ( "Entendi, Houve um engano" );
                        getche ( );
                   }
                   gotoxy ( 26 , 14 );
                   if ( publico <= 60000 ) {
                        printf ( "Com certeza foi engano" );
                        getche ( );
                   }
                   break;
              } else {
                   textcolor ( YELLOW );
                   gotoxy ( 37 , 10 );
                   printf ( "Opcao errada" );
              }
              getche ( );
              system ( "cls" );
         } while ( 1 );
     } while ( 1 );
}

Nenhum comentário:

Postar um comentário

Observação: somente um membro deste blog pode postar um comentário.