Wetalked about the ‘if and else’ statements for decision-making. In cases, where there are more conditions to be checked, it is better and more convenient to use the switch statements, for example, the operations on a calculator. In a calculator, we need to check whether the operation is addition, subtraction, multiplication, or division.
We talked about the ‘if and else’ statements for decision-making. In cases, where there are more conditions to be checked, it is better and more convenient to use the switch statements, for example, the operations on a calculator. In a calculator, we need to check whether the operation is addition, subtraction, multiplication, or division.
Take a look at the calculator program
Enter first number: 5 Enter the operator: + Enter second number: 6 5.00 + 6.00 = 11.00
After taking the inputs, we made a switch statement.
The lines to what to do according to the value of the sign variable are written between the curly braces of the switch statement.
The ‘case’ keyword checks the value of the sign. If the value matches, it will execute the statements written after the colon. In this case, if the value becomes ‘+’, the code after the colon will be executed else it will not.
What about the ‘break’ keyword? The ‘break’ keyword is used to stop the checks. When the program execution reaches the ‘break’, it jumps out of the curly braces of the switch statement to the next line after the curly braces and prints the result. As this is a calculator program, if the first condition is matched, it is not needed to check the second condition and if the second condition is matched, it is not needed to check the third condition and on. Even if the ‘break’ statement is not given, the program execution will run without any trouble if there is no default condition. If the default condition is present, the statements after it will be executed. If the ‘break’ is not used, the program has to execute the remaining checks without any actual need. It’s just redundant. It is a principle in programming to avoid redundancy as far as possible.
It’s like telling the computer, “_Hey buddy, if you got a condition as true, just don’t check the others. It is just unnecessary_”.
In case there is a need for checking the other conditions too, the break can be avoided.
The ‘default’ will always execute the statements after it. So, if the ‘break’ is not used, the program will finally reach the ‘default’ and execute the statements after it and the statements in the above condition blocks will not be executed. If there is the ‘break’, as the program jumps out of the checks to the next line after the curly braces of the switch statement, the statements after the ‘default’ will not be executed.
Note: Since there are no more conditions to check after the default, the ‘break’ statement is not given.
And the ‘exit(0)’ code which is taken from the <stdlib.h>, just tells the computer to stop the execution of the program there. The reason to stop the program there is that we do not want to display the result if the operator is invalid.
Wondered what the 0.2 is doing here? As you know %f is the format specifier for floating-point numbers. The ‘0.2’ tells that the number displayed should have only two decimal places. So, if the value is 2.01234, only ‘2.01’ will be displayed.
You might have noticed the space in the character format specifier.
scanf(" %c", &sign);
If space is not there, the program will not run correctly. The reason behind this will be explained in a later article.
So, instead of writing separate ‘if and else’ statements for each of the conditions for checking the value of the ‘sign’, the switch statement is a more convenient one.
C Language is an evergreen language and is used widely across different industries, This C programming is a must for students and working professionals to become a great Software Engineer especially when they are working in Software Development Domain. Great Learning brings you this live session on "Introduction to C". In this live session, we will be covering major concepts in C Programming such as Different Variables, Different Data Types that are being used, its Operators, Flow control statements, Structure, and lot more.
This article is the first part of a series of engineering articles designed to explain how you can create a lightweight scripting language based on C++ and commonly available technology.
C is an evergreen language and is widely used across different industries. This language is a must for students and working professionals who want to become good Software Engineers, especially when they are working in the Software Development Domain. Great Learning brings you this live session on "C Programming for Beginners". In this live session, we will be covering major concepts of C, starting with the syntax of the language. Then we will be covering topics such as comments, tokens, variables, etc. We will then look at different data types and operators used for various mathematical operations, post which we will conclude our session with in-built libraries and conditional statements wherein we will talk about if, else if, nested if, and else block, and how multiple conditions can be replaced by a switch case. We will also understand the concepts of loops wherein we will talk about while loops, for loops and do-while loops including their similarities and differences.
in this article, I’ll show you How you can compile your C/C++ code in windows. To compile C/C++ code we need GCC/G++ to compile the code but windows don’t have like Linux terminal or mac terminal. So what we can do, to compile your code in vs code we need an extension called C/C++ Compile Run extension.
Learning C: Input and Output and Two Program Templates. Besides demonstrating how to perform input and output in C, I will also be demonstrating two templates that are related to those topics — Prompt, Then Read and Input, Process, Output (IPO). The IPO template, in particular, is important because practically every C program you write will use this template.