C Language | If , If-else and For Loop Statements

Friday 21 December 2012


I was playing a game with some kids called "is it hot", when I thought of putting it into a program. This game is mostly played with little kids. All you have to do is assign numbers in each temperature. For example, 1=hot, 2=cold and 3=warm. It is played by saying random numbers but the kids must take note that every time you say 1 or 2 or 3 they must answer you with hot, cold or warm. If you say other numbers except the three they should say, "out of order". This is kind of game is fun if the kids are in groups because they need to work with their teams to win.

As for me, I made a program out of this game to show you how you can put up If Statement, If-else Selection Statement and For Loop Statement in one program. There are certain programs that needs a strong foundation to be more productive especially programs with a lot of selection, decision making and repetitive statements. This example I have is one of those programs. Check out the codes below.


#include<stdio.h>
#include<conio.h>

main()
{
      int a=1, b=1; 
      int count;
      
      printf("Enter how many counts: ");
      scanf("%d",&count);
      
      if(count>30) {
      printf("\nYou have exceeded the maximum input of 30");
      getch();
      return 0;
      }
   
      for(count<30;b<=count;b++) {
      printf("\nEnter Numbers: ");
      scanf("%d",&a);
      
      if (a==1)
      {
      printf("Hot\n");
      }
      else if (a==2) 
      {
      printf("Cold\n");
      }
      else if (a==3) 
      {
      printf("Warm\n");
      }
      else 
      {
      printf("Out of Range\n");
      }}
               getch();
               }
               


This program operates like this. You have there declared variables a and count under the data type identifier int which will stand as your storage memory for the user inputs. Variable b will stand as your initial count within your looping statement. First, it will ask the user how many times the user want to enter a number, then it will be stored in memory count.This will act as the final count for your looping statement which variable b should satisfy. As seen above, there is an if statement that has a condition if the entered number/integer exceeds thirty (30), the program must print a warning and the program will terminate. That is if the user enters a number more than thirty (30), if not the program will now execute the looping statement. If the user entered five (5) and variable b which is initiated with one (1) then it satisfies the condition. That means the printf statement will be executed asking the user to input a number. After entering a number, it will now go through the if-else statement where it will select which condition the entered value would satisfy, then it will print the result. If the user entered other value which is not in the list it would go straight to the else statement which is the last selection and will print, "out of range". Then, variable b will be incremented. If it satisfies count in the condition statement it will do the same thing again. If variable b bears false in the condition that is the moment the looping statement will terminate. Then results will be posted. Afterwards, you can terminate the program. You can check out the sample output below.






Hope you learned something. We would like to hear from you soon. Thanks!

2 comments:

Please do leave a comment. It's gonna be a friendly conversation between programmers. Hope you do find our blog helpful. Thank you!

Related Posts Plugin for WordPress, Blogger...