Tutorial Playlist
In computer programming, statements are executed based on predefined rules, and logical conditions determine the control flow. However, there are situations where it becomes necessary to execute custom logic. This is where control statements come in. Control statements allow us to define and execute customised logic based on specific conditions.
With a control statement, the program enters a block of statements and executes them only if the specified logic is met. This ability to control the order of statement execution is why these constructs are called control statements. They are commonly used to determine the sequence in which statements should be executed.
Let’s delve into a deep understanding of conditional statements in C with examples and why we need them.
Control Statements are crucial in guiding the computer to execute specific logical statements. They allow us to determine whether a particular set of statements should be executed or not based on certain conditions. Control statements are used to direct the flow of execution, ensuring that statements are executed only when specific conditions are met.
In simpler terms, control statements in C help us control the program’s flow and decide which statements need to be executed under specific conditions.
In the C programming language, the control passes from one instruction to another. This flow of control is known as subsequent control flow. In C programming, it is common for programmers to need to repeat sets of instructions or skip certain instructions based on logical conditions. In such cases, they utilise control structures in C, also called decision-making or conditional statements.
Conditional statements in C allow programmers to make decisions based on specific conditions. If no condition exists around the statements, these conditional statements are executed sequentially. However, when a condition is applied to a block of statements, the flow of execution can be altered based on the evaluated result of the condition in the program. This process is commonly referred to as decision-making in the C language.
There are 4 types of control statements in C -
The switch statement in C enables multi-way branching based on the value of the switch expression.
When the switch statement is encountered, the control is transferred to the specific case label that matches the value of the switch expression. The statements associated with that case label are then executed. If none of the cases match the switch expression, the default statement is executed instead.
The syntax for conditional control statements in C is -
if (condition) { |
Here is control statements in c example programs -
#include <stdio.h> |
Output:
The given number is positive. |
In C, simple if statements are used to perform specific operations when a condition is true. If the condition in the if statement evaluates to true, the statements within the if block are executed. However, if the condition is false, the control is transferred to the statements outside the if block.
The syntax for a simple if statement is -
if (condition) { |
An example to help you understand better -
#include <stdio.h> |
Output:
The value is positive. |
In certain scenarios, statements must be executed based on true or false conditions. To handle such situations, if-else statements are utilised. If the condition specified in the if statement is true, the statements within the if block are executed. Conversely, if the condition evaluates to false, the statements within the else block are executed.
The syntax for if-else statements is -
if (condition) { |
Let’s understand this better with an example -
#include <stdio.h> |
Output:
The value is positive. |
Nested if-else statements include additional if or else statements within an existing if or else block. In this structure, if the condition of the outer if statement is true, the statements within its corresponding if block are executed. This block may also contain an inner if statement. If the condition of the inner if statement is true, the statements within its if block is executed. However, if the condition of the inner if statement is false, the statements within its else block are executed instead.
On the other hand, if the condition of the outer if statement is false, the statements within its else block are executed. This else block may also contain another if statement. If the condition of this inner if statement is true, the statement within its if block is executed. Otherwise, if the condition of the inner if statement is false, the statements within its else block are executed.
The syntax for nested if-else statements is -
if (condition1) { |
Example -
#include <stdio.h> |
Output:
The given number is positive. |
The else-if ladder statements in C allow the testing of multiple conditions. Each else-if condition is evaluated sequentially, and if any condition is true, the statements within that if block are executed. If none of the conditions are true, the statements within the else block are executed.
Using the else-if ladder structure, programmers can handle multiple conditions and execute specific sets of statements based on the first true condition encountered.
The syntax for else-if statements is -
if (condition1) { |
Example -
#include <stdio.h> |
Output -
The given number is positive. |
The while loop in C is called an entry loop because it evaluates the condition before executing the statements within its body. If the while loop condition is initially false, the statements within the loop will not be executed.
The syntax for the while loop is -
while (condition) { |
Example -
#include <stdio.h> |
Output:
Count: 0 |
A do-while loop in C is often called an exit loop because the statements within its body are executed first, and then the condition is checked. If the condition of the while loop is true, the body of the loop will be executed repeatedly until the condition becomes false. Once the condition is false, the control will transfer outside the do-while loop, and the statements following the do-while loop will be executed.
The syntax for the do-while loop is -
do { |
Example -
#include <stdio.h> |
Output -
Count: 0 |
A for loop in C is often called a pretest loop because the condition is checked before executing the statements within its body. The for loop syntax consists of three expressions: initialization (expression1), conditional expression (expression2), and updation (expression3). Variables can be initialised within the for statement itself.
The syntax for For Loop is -
for (expression1; expression2; expression3) { |
Example -
#include <stdio.h> |
Output:
Count: 0 |
The goto statement in C transfers the flow of control within a program. It allows jumping to a specific part of the program. The label used in the goto statement serves as a marker to direct the program's branch to a specified point.
The syntax for Goto Statement in C is -
goto label; |
Example:
#include <stdio.h> |
Conditional control statements in C provide the means to make decisions and control the program's flow based on specific conditions. They enable executing different code blocks depending on the evaluation of conditions, offering flexibility and adaptability in programming. Using these statements judiciously can help programmers create efficient and dynamic programs that respond intelligently to varying scenarios.
Students are also encouraged to study the Graduate Certificate Programme in Data Science upGrad from upGrad, which will help them gain an insight into the vast world of data science. Studying and upgrading skills like programming, predictive analytics, advanced tech, and more will help them become experts.
So don’t let this opportunity pass on, enroll now!
1. In what situations do we use the nested if statement?
The nested if statement is used when checking additional conditions within an if or else block. It allows for more complex decision-making by nesting multiple if-else statements.
2. What are the control statements available in C?
The control statements in C include if, if-else, nested if-else, switch, while, do-while, for, and goto. These statements control the flow of execution based on conditions or looping requirements.
3. How is the conditional operator used in C?
The conditional operator in C, denoted as "? :", is a ternary operator that allows for inline conditional expressions. It evaluates a condition and returns one of two values based on whether true or false.
PAVAN VADAPALLI
Popular
Talk to our experts. We’re available 24/7.
Indian Nationals
1800 210 2020
Foreign Nationals
+918045604032
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enrolling. upGrad does not make any representations regarding the recognition or equivalence of the credits or credentials awarded, unless otherwise expressly stated. Success depends on individual qualifications, experience, and efforts in seeking employment.
upGrad does not grant credit; credits are granted, accepted or transferred at the sole discretion of the relevant educational institution offering the diploma or degree. We advise you to enquire further regarding the suitability of this program for your academic, professional requirements and job prospects before enr...