Conditional Statements

Conditional Statements

In this part you will learn: 1. C syntax 2. Conditional Statements 3. Operators In this tutorial I will teach you about conditional statements in C. The first program will be a basic one with a most basic conditional statement. The second one is a grade calculator. First Program. Basic Conditional Statements Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>
These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. int main()
  2. {
  3. int a;
  4. char c;
  5. printf("Enter an integer:");
  6. scanf("%d",&a);
  7.  
  8. printf("\nEnter a Character:");
  9. scanf(" %c",&c);
Now at first we declared two variables. A integer variable and a character variable. We took the input from the user in these variables using scanf() function.
  1. if(a==2)
  2. {
  3. printf("\nYou Enter the right number.");
  4. }
  5. else
  6. {
  7. printf("\nYou Enter the wrong number.");
  8. }
  9.  
  10. if(c=='M')
  11. {
  12. printf("\nYou Enter the right character.");
  13. }
  14. else
  15. {
  16. printf("\nYou Enter the wrong character.");
  17. }
  18.  
  19.  
  20. getch();
  21. }
  22.  
This is the conditional statement i.e ‘if()’ and ‘else’ . We write our condition in brackets and in square brackets we write the code that we want to execute when our condition in if() is met. Like in this program I have placed a condition that if a is equal to 2 then the program should output a statement “ you entered the right number.” And if a is not equal to 2 then the ‘else’ part will execute and the program will output a statement “ you entered the wrong number.”. only one condition executes at a time either ‘if’ or else part. In the condition we enter ‘==’ this is the operator in C which means ‘equal to’ similar to ‘=’ in mathematics. Same is the condition with the character variable too. Execute > compile then Execute > run Output picture viewer
Grade Calculator Basic Step: Open Dev C++ then File > new > source file and start writing the code below.
  1. #include<stdio.h>
  2. #include<conio.h>
These two are the most common and basic header files. The first one stdio.h is used to grant us most of the basic functions of C like Input and Output functions. The second one conio.h provides us functions like getch() which is used to pause our screen until we press a button.
  1. int main()
  2. {
  3. int m;
  4.  
  5. printf("Enter you marks of any subject (out of 100) :");
  6. scanf("%d",&m);
At first we just declared an integer variable. Then afterwards we took input from the user in our integer variable. The user should enter his marks of any subject out of 100.
  1. if(m>=90 && m<=100)
  2. {
  3. printf("\nYour grade is = A*");
  4. }
  5. else if(m>=80 && m<=90)
  6. {
  7. printf("\nYour grade is = A");
  8. }
  9.  
  10. else if(m>=70 && m<=80)
  11. {
  12. printf("\nYour grade is = B");
  13. }
  14. else if(m>=60 && m<=70)
  15. {
  16. printf("\nYour grade is = C");
  17. }
  18. else if(m>=50 && m<=60)
  19. {
  20. printf("\nYour grade is = D");
  21. }
  22. else if(m>=45 && m<=50)
  23. {
  24. printf("\nYour grade is = E");
  25. }
  26. else if(m<45 && m>=0)
  27. {
  28. printf("\nYou are Fail");
  29. }
  30. else
  31. {
  32. printf("\nInvalid number");
  33. }
  34.  
  35. getch();
  36. }
In this part of code me place many statements. These statement compare the marks of the student with the grading criteria that we have set. You can set any condition you like. The program first checks the marks of the students with first condition. If it is a match with condition the program executes the code in the brackets and ends the program. Otherwise it checks the ‘else if() ‘ conditions. Note that only one statement executes in the whole program. It can be either if() or else if or else. In the conditions we have used few operators that might be new to you. The first is ‘>=’ operator this means “greater or equal to”. The second is ‘ compile then Execute > run Output picture viewerpicture viewer

Add new comment