C Programming for Beginners : Learn by Building a Calculator

Suraj Das
4 min readNov 2, 2021

--

Photo by Amol Tyagi on Unsplash

Conditional Statements

What are conditional statements ?

It is a statement which allows a given line of code to get executed iff the condition within the statement is true.

If Statement

For example : if the letter is a then say apple. If not, say banana.

We can program the above line like this :

oii apple

Breakdown of the Code

  • The ‘ == ’ at the line 7 is a condition checker. You can read it as ‘ is equal to’.
    It is different than ‘ = ’ sign because here it is used to assign and declare some value to a variable.
  • ‘==’ returns true as the letter variable is a. And that’s why the code within the if statement got executed and the code within the else statement is passed.

Try experimenting with the if else statement on your own and get comfortable with it. Try changing the letter a at line 7 to letter b or whatever :)

Else If Statement

What if you need to check for more than one condition ?

We can implement the else if statement.

Example :

cherry

Sometimes the if-else statements look like sh*t

Consider a program where you get a compliment based on your grade.
For example, if your grade is A : you get Perfect as a compliment. But if your grade is F : you get You suck as a compliment. Wait! Is that a compliment ?

So here’s the deal. You will be giving your grade as an input right ? So, what if you enter something else like G as your grade ? You would have to add an else statement which tells you to enter a valid grade.

Your program should look something like this :

Enter your grade : f (it's not capital F)
enter a valid grade

If enter a valid grade :

Enter your grade : A
perfect

Great! The program works fine. No problem with it.

But the problem is with the readability and aesthetics. There is too many else if statements. One can take too much time fixing the error if a bug appears within these statement.

So here come the Switch Case statement to help us write cleaner code.

Switch Case statement

Using switch case statement, we can write the above program like this :

Yess! It is much more readable now.

After switching to the variable grade, it checks each and every case.
If none of the cases returns true, it executes the default code.

Rules for implementing Switch Case Statement

  1. The expression provided in the switch should result in a constant value otherwise it would not be valid.
  2. Duplicate cases are not allowed.

Why is there a break after each case statement ?

The break statement is used inside the switch to terminate a statement sequence. When a break statement is reached, the switch terminates, and the flow of control jumps to the next line following the switch statement.

|| in C

‘||’ in C Programming means ‘or’.
It is used in programming like this : true or false gives true ; i.e. ; 1||0 gives you 1.

condition1 || condition2 = true  
// iff at least one of the two conditions return true

Yayy! Now, we’ve learnt enough to make the calculator.

Calculator Program

Read the program only once (but carefully) and try to build it on your own.
Don’t be afraid to make errors.
Trust me! You will learn more by making errors.

Enter operation : (+, -, *, /) : *
Enter 1st number : 3
Enter 2nd number : 4
Total is : 12.00

Outro

Hurray! You’ve now completed a good beginner friendly project.

Have any doubt ?
DM me on Instagram

Peace.

Index of C Programming lessons :

  1. Getting Started within 5 minutes
  2. Learning Fundamentals Made Easy
  3. Circumference and Area of Circle
  4. scanf() vs fgets()
  5. Hypotenuse Calculator
  6. Learn by Building a Calculator
  7. Functions
  8. More on Functions
  9. Length of an Array
  10. String Functions
  11. The While Loop

--

--