- Blog Categories
- Software Development
- Data Science
- AI/ML
- Marketing
- General
- MBA
- Management
- Legal
- Software Development Projects and Ideas
- 12 Computer Science Project Ideas
- 28 Beginner Software Projects
- Top 10 Engineering Project Ideas
- Top 10 Easy Final Year Projects
- Top 10 Mini Projects for Engineers
- 25 Best Django Project Ideas
- Top 20 MERN Stack Project Ideas
- Top 12 Real Time Projects
- Top 6 Major CSE Projects
- 12 Robotics Projects for All Levels
- Java Programming Concepts
- Abstract Class in Java and Methods
- Constructor Overloading in Java
- StringBuffer vs StringBuilder
- Java Identifiers: Syntax & Examples
- Types of Variables in Java Explained
- Composition in Java: Examples
- Append in Java: Implementation
- Loose Coupling vs Tight Coupling
- Integrity Constraints in DBMS
- Different Types of Operators Explained
- Career and Interview Preparation in IT
- Top 14 IT Courses for Jobs
- Top 20 Highest Paying Languages
- 23 Top CS Interview Q&A
- Best IT Jobs without Coding
- Software Engineer Salary in India
- 44 Agile Methodology Interview Q&A
- 10 Software Engineering Challenges
- Top 15 Tech's Daily Life Impact
- 10 Best Backends for React
- Cloud Computing Reference Models
- Web Development and Security
- Find Installed NPM Version
- Install Specific NPM Package Version
- Make API Calls in Angular
- Install Bootstrap in Angular
- Use Axios in React: Guide
- StrictMode in React: Usage
- 75 Cyber Security Research Topics
- Top 7 Languages for Ethical Hacking
- Top 20 Docker Commands
- Advantages of OOP
- Data Science Projects and Applications
- 42 Python Project Ideas for Beginners
- 13 Data Science Project Ideas
- 13 Data Structure Project Ideas
- 12 Real-World Python Applications
- Python Banking Project
- Data Science Course Eligibility
- Association Rule Mining Overview
- Cluster Analysis in Data Mining
- Classification in Data Mining
- KDD Process in Data Mining
- Data Structures and Algorithms
- Binary Tree Types Explained
- Binary Search Algorithm
- Sorting in Data Structure
- Binary Tree in Data Structure
- Binary Tree vs Binary Search Tree
- Recursion in Data Structure
- Data Structure Search Methods: Explained
- Binary Tree Interview Q&A
- Linear vs Binary Search
- Priority Queue Overview
- Python Programming and Tools
- Top 30 Python Pattern Programs
- List vs Tuple
- Python Free Online Course
- Method Overriding in Python
- Top 21 Python Developer Skills
- Reverse a Number in Python
- Switch Case Functions in Python
- Info Retrieval System Overview
- Reverse a Number in Python
- Real-World Python Applications
- Data Science Careers and Comparisons
- Data Analyst Salary in India
- Data Scientist Salary in India
- Free Excel Certification Course
- Actuary Salary in India
- Data Analyst Interview Guide
- Pandas Interview Guide
- Tableau Filters Explained
- Data Mining Techniques Overview
- Data Analytics Lifecycle Phases
- Data Science Vs Analytics Comparison
- Artificial Intelligence and Machine Learning Projects
- Exciting IoT Project Ideas
- 16 Exciting AI Project Ideas
- 45+ Interesting ML Project Ideas
- Exciting Deep Learning Projects
- 12 Intriguing Linear Regression Projects
- 13 Neural Network Projects
- 5 Exciting Image Processing Projects
- Top 8 Thrilling AWS Projects
- 12 Engaging AI Projects in Python
- NLP Projects for Beginners
- Concepts and Algorithms in AIML
- Basic CNN Architecture Explained
- 6 Types of Regression Models
- Data Preprocessing Steps
- Bagging vs Boosting in ML
- Multinomial Naive Bayes Overview
- Bayesian Network Example
- Bayes Theorem Guide
- Top 10 Dimensionality Reduction Techniques
- Neural Network Step-by-Step Guide
- Technical Guides and Comparisons
- Make a Chatbot in Python
- Compute Square Roots in Python
- Permutation vs Combination
- Image Segmentation Techniques
- Generative AI vs Traditional AI
- AI vs Human Intelligence
- Random Forest vs Decision Tree
- Neural Network Overview
- Perceptron Learning Algorithm
- Selection Sort Algorithm
- Career and Practical Applications in AIML
- AI Salary in India Overview
- Biological Neural Network Basics
- Top 10 AI Challenges
- Production System in AI
- Top 8 Raspberry Pi Alternatives
- Top 8 Open Source Projects
- 14 Raspberry Pi Project Ideas
- 15 MATLAB Project Ideas
- Top 10 Python NLP Libraries
- Naive Bayes Explained
- Digital Marketing Projects and Strategies
- 10 Best Digital Marketing Projects
- 17 Fun Social Media Projects
- Top 6 SEO Project Ideas
- Digital Marketing Case Studies
- Coca-Cola Marketing Strategy
- Nestle Marketing Strategy Analysis
- Zomato Marketing Strategy
- Monetize Instagram Guide
- Become a Successful Instagram Influencer
- 8 Best Lead Generation Techniques
- Digital Marketing Careers and Salaries
- Digital Marketing Salary in India
- Top 10 Highest Paying Marketing Jobs
- Highest Paying Digital Marketing Jobs
- SEO Salary in India
- Content Writer Salary Guide
- Digital Marketing Executive Roles
- Career in Digital Marketing Guide
- Future of Digital Marketing
- MBA in Digital Marketing Overview
- Digital Marketing Techniques and Channels
- 9 Types of Digital Marketing Channels
- Top 10 Benefits of Marketing Branding
- 100 Best YouTube Channel Ideas
- YouTube Earnings in India
- 7 Reasons to Study Digital Marketing
- Top 10 Digital Marketing Objectives
- 10 Best Digital Marketing Blogs
- Top 5 Industries Using Digital Marketing
- Growth of Digital Marketing in India
- Top Career Options in Marketing
- Interview Preparation and Skills
- 73 Google Analytics Interview Q&A
- 56 Social Media Marketing Q&A
- 78 Google AdWords Interview Q&A
- Top 133 SEO Interview Q&A
- 27+ Digital Marketing Q&A
- Digital Marketing Free Course
- Top 9 Skills for PPC Analysts
- Movies with Successful Social Media Campaigns
- Marketing Communication Steps
- Top 10 Reasons to Be an Affiliate Marketer
- Career Options and Paths
- Top 25 Highest Paying Jobs India
- Top 25 Highest Paying Jobs World
- Top 10 Highest Paid Commerce Job
- Career Options After 12th Arts
- Top 7 Commerce Courses Without Maths
- Top 7 Career Options After PCB
- Best Career Options for Commerce
- Career Options After 12th CS
- Top 10 Career Options After 10th
- 8 Best Career Options After BA
- Projects and Academic Pursuits
- 17 Exciting Final Year Projects
- Top 12 Commerce Project Topics
- Top 13 BCA Project Ideas
- Career Options After 12th Science
- Top 15 CS Jobs in India
- 12 Best Career Options After M.Com
- 9 Best Career Options After B.Sc
- 7 Best Career Options After BCA
- 22 Best Career Options After MCA
- 16 Top Career Options After CE
- Courses and Certifications
- 10 Best Job-Oriented Courses
- Best Online Computer Courses
- Top 15 Trending Online Courses
- Top 19 High Salary Certificate Courses
- 21 Best Programming Courses for Jobs
- What is SGPA? Convert to CGPA
- GPA to Percentage Calculator
- Highest Salary Engineering Stream
- 15 Top Career Options After Engineering
- 6 Top Career Options After BBA
- Job Market and Interview Preparation
- Why Should You Be Hired: 5 Answers
- Top 10 Future Career Options
- Top 15 Highest Paid IT Jobs India
- 5 Common Guesstimate Interview Q&A
- Average CEO Salary: Top Paid CEOs
- Career Options in Political Science
- Top 15 Highest Paying Non-IT Jobs
- Cover Letter Examples for Jobs
- Top 5 Highest Paying Freelance Jobs
- Top 10 Highest Paying Companies India
- Career Options and Paths After MBA
- 20 Best Careers After B.Com
- Career Options After MBA Marketing
- Top 14 Careers After MBA In HR
- Top 10 Highest Paying HR Jobs India
- How to Become an Investment Banker
- Career Options After MBA - High Paying
- Scope of MBA in Operations Management
- Best MBA for Working Professionals India
- MBA After BA - Is It Right For You?
- Best Online MBA Courses India
- MBA Project Ideas and Topics
- 11 Exciting MBA HR Project Ideas
- Top 15 MBA Project Ideas
- 18 Exciting MBA Marketing Projects
- MBA Project Ideas: Consumer Behavior
- What is Brand Management?
- What is Holistic Marketing?
- What is Green Marketing?
- Intro to Organizational Behavior Model
- Tech Skills Every MBA Should Learn
- Most Demanding Short Term Courses MBA
- MBA Salary, Resume, and Skills
- MBA Salary in India
- HR Salary in India
- Investment Banker Salary India
- MBA Resume Samples
- Sample SOP for MBA
- Sample SOP for Internship
- 7 Ways MBA Helps Your Career
- Must-have Skills in Sales Career
- 8 Skills MBA Helps You Improve
- Top 20+ SAP FICO Interview Q&A
- MBA Specializations and Comparative Guides
- Why MBA After B.Tech? 5 Reasons
- How to Answer 'Why MBA After Engineering?'
- Why MBA in Finance
- MBA After BSc: 10 Reasons
- Which MBA Specialization to choose?
- Top 10 MBA Specializations
- MBA vs Masters: Which to Choose?
- Benefits of MBA After CA
- 5 Steps to Management Consultant
- 37 Must-Read HR Interview Q&A
- Fundamentals and Theories of Management
- What is Management? Objectives & Functions
- Nature and Scope of Management
- Decision Making in Management
- Management Process: Definition & Functions
- Importance of Management
- What are Motivation Theories?
- Tools of Financial Statement Analysis
- Negotiation Skills: Definition & Benefits
- Career Development in HRM
- Top 20 Must-Have HRM Policies
- Project and Supply Chain Management
- Top 20 Project Management Case Studies
- 10 Innovative Supply Chain Projects
- Latest Management Project Topics
- 10 Project Management Project Ideas
- 6 Types of Supply Chain Models
- Top 10 Advantages of SCM
- Top 10 Supply Chain Books
- What is Project Description?
- Top 10 Project Management Companies
- Best Project Management Courses Online
- Salaries and Career Paths in Management
- Project Manager Salary in India
- Average Product Manager Salary India
- Supply Chain Management Salary India
- Salary After BBA in India
- PGDM Salary in India
- Top 7 Career Options in Management
- CSPO Certification Cost
- Why Choose Product Management?
- Product Management in Pharma
- Product Design in Operations Management
- Industry-Specific Management and Case Studies
- Amazon Business Case Study
- Service Delivery Manager Job
- Product Management Examples
- Product Management in Automobiles
- Product Management in Banking
- Sample SOP for Business Management
- Video Game Design Components
- Top 5 Business Courses India
- Free Management Online Course
- SCM Interview Q&A
- Fundamentals and Types of Law
- Acceptance in Contract Law
- Offer in Contract Law
- 9 Types of Evidence
- Types of Law in India
- Introduction to Contract Law
- Negotiable Instrument Act
- Corporate Tax Basics
- Intellectual Property Law
- Workmen Compensation Explained
- Lawyer vs Advocate Difference
- Law Education and Courses
- LLM Subjects & Syllabus
- Corporate Law Subjects
- LLM Course Duration
- Top 10 Online LLM Courses
- Online LLM Degree
- Step-by-Step Guide to Studying Law
- Top 5 Law Books to Read
- Why Legal Studies?
- Pursuing a Career in Law
- How to Become Lawyer in India
- Career Options and Salaries in Law
- Career Options in Law India
- Corporate Lawyer Salary India
- How To Become a Corporate Lawyer
- Career in Law: Starting, Salary
- Career Opportunities: Corporate Law
- Business Lawyer: Role & Salary Info
- Average Lawyer Salary India
- Top Career Options for Lawyers
- Types of Lawyers in India
- Steps to Become SC Lawyer in India
- Tutorials
- Software Tutorials
- C Tutorials
- Recursion in C: Fibonacci Series
- Checking String Palindromes in C
- Prime Number Program in C
- Implementing Square Root in C
- Matrix Multiplication in C
- Understanding Double Data Type
- Factorial of a Number in C
- Structure of a C Program
- Building a Calculator Program in C
- Compiling C Programs on Linux
- Java Tutorials
- Handling String Input in Java
- Determining Even and Odd Numbers
- Prime Number Checker
- Sorting a String
- User-Defined Exceptions
- Understanding the Thread Life Cycle
- Swapping Two Numbers
- Using Final Classes
- Area of a Triangle
- Skills
- Explore Skills
- Management Skills
- Software Engineering
- JavaScript
- Data Structure
- React.js
- Core Java
- Node.js
- Blockchain
- SQL
- Full stack development
- Devops
- NFT
- BigData
- Cyber Security
- Cloud Computing
- Database Design with MySQL
- Cryptocurrency
- Python
- Digital Marketings
- Advertising
- Influencer Marketing
- Performance Marketing
- Search Engine Marketing
- Email Marketing
- Content Marketing
- Social Media Marketing
- Display Advertising
- Marketing Analytics
- Web Analytics
- Affiliate Marketing
- MBA
- MBA in Finance
- MBA in HR
- MBA in Marketing
- MBA in Business Analytics
- MBA in Operations Management
- MBA in International Business
- MBA in Information Technology
- MBA in Healthcare Management
- MBA In General Management
- MBA in Agriculture
- MBA in Supply Chain Management
- MBA in Entrepreneurship
- MBA in Project Management
- Management Program
- Consumer Behaviour
- Supply Chain Management
- Financial Analytics
- Introduction to Fintech
- Introduction to HR Analytics
- Fundamentals of Communication
- Art of Effective Communication
- Introduction to Research Methodology
- Mastering Sales Technique
- Business Communication
- Fundamentals of Journalism
- Economics Masterclass
- Free Courses
- Home
- Blog
- Software Development
- What is Control Statement in C? Types, Uses, and Code Examples
What is Control Statement in C? Types, Uses, and Code Examples
Updated on Mar 03, 2025 | 17 min read
Share:
Table of Contents
A control statement in C is used to control the flow of execution based on certain conditions. These statements allow your code to make decisions, repeat actions, or jump to different parts of the program.
In this article, you’ll learn what a control statement is in c, how each control statement works and how to use them effectively in your C programs for better control and optimization.
What is Control Statement in C and Why Does It Matters?
In C programming, a control statement is a statement that directs the flow of program execution based on certain conditions. These statements are used to determine the order in which other statements are executed.
Control statements help structure your program in a way that allows it to make decisions, repeat actions, or jump to different parts of the code based on specific conditions.
For example, you might use a control statement to check if a user input is valid, or if a loop should continue running. They are essential for building programs that are interactive, flexible, and efficient.
Importance of Control Statement in C
Understanding the types of control statements in C and how to use them effectively is crucial to writing functional and efficient programs. Let’s explore why they matter:
- Direct Execution Flow: Control statements allow you to guide the execution of your program, making it adapt to different conditions and inputs. This is where the decision control statement in C comes into play, allowing your program to make decisions based on specific conditions.
- Conditional Logic: With control statements like if, else, and switch, you can execute certain sections of code only when specific conditions are met. For example, you can check if a number is positive or negative and execute different actions accordingly.
- Repetition of Actions: Control statements like loops (for, while, do-while) let you repeat actions multiple times without writing repetitive code. This is especially useful when you need to perform the same task for multiple data points or iterations.
- Code Readability and Structure: Well-organized control statements improve the clarity and flow of your code, but poorly structured ones can reduce readability and performance. By clearly defining how the program should behave in different situations, they help anyone reading your code understand its flow quickly.
- Optimized Code: By using control statements effectively, you can write more optimized programs that execute only necessary tasks, improving performance and reducing resource consumption.
Now that you understand the basics, let’s dive into the different types of control statements in C and see how each one works with real-world examples.
Types of Control Statement in C: Concepts, Examples, and Implementation
In this section, you’ll explore the main categories of control statements in C. These include decision control statements for making choices, loop control statements for repeating actions, and jump control statements for directing the program flow.
Each category includes examples to show how to use them.
Decision Control Statement in C
In C programming, decision control statements are used to make decisions based on specific conditions. These statements allow the program to choose different paths based on whether a condition is true or false.
They are a core concept in the types of control statements in C, and understanding them will help you write dynamic and interactive programs.
The most common decision control statements in C are the if statement, if-else statement, nested if, and the else-if ladder. Let's break them down one by one.
1. If Statement
The if statement evaluates a condition, and if it’s true, the code block inside it executes. If the condition is false, the program moves on to the next statement.
Syntax:
if (condition) {
// Code to be executed if the condition is true
}
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int number = 5;
if (number > 0) {
printf("The number is positive.\n");
}
return 0;
}
Output:
The number is positive.
Also Read: Control Statements in Java: What Do You Need to Know in 2024
2. If-Else Statement
The if-else statement works similarly to the if statement, but provides an alternative code block to execute if the condition is false.
Syntax:
if (condition) {
// Code if condition is true
} else {
// Code if condition is false
}
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int number = -3;
if (number > 0) {
printf("The number is positive.\n");
} else {
printf("The number is negative or zero.\n");
}
return 0;
}
Output:
The number is negative or zero.
3. Nested If Statement
A nested if statement occurs when one if statement is placed inside another if. This allows you to evaluate multiple conditions.
Syntax:
if (condition1) {
if (condition2) {
// Code to be executed if both conditions are true
}
}
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int age = 20;
if (age >= 18) {
if (age < 21) {
printf("You are an adult but not yet 21.\n");
} else {
printf("You are 21 or older.\n");
}
}
return 0;
}
Output:
You are an adult but not yet 21.
4. Else-If Ladder
The else-if ladder is used when you need to test multiple conditions. It’s a sequence of if statements, each checking a different condition. When a condition is true, the corresponding block of code runs, and the rest are ignored.
Syntax:
if (condition1) {
// Code if condition1 is true
} else if (condition2) {
// Code if condition2 is true
} else if (condition3) {
// Code if condition3 is true
} else {
// Code if none of the conditions are true
}
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int number = 10;
if (number < 0) {
printf("The number is negative.\n");
} else if (number == 0) {
printf("The number is zero.\n");
} else {
printf("The number is positive.\n");
}
return 0;
}
Output:
The number is positive.
5. Switch Statement
The switch statement in C is a decision control statement that allows you to choose between multiple options based on the value of a variable or expression.
It is especially useful when you have a single variable that could match multiple possible values, and you need to execute different blocks of code depending on the matching value.
Syntax:
switch (expression) {
case value1:
// Code to be executed if expression == value1
break;
case value2:
// Code to be executed if expression == value2
break;
case value3:
// Code to be executed if expression == value3
break;
default:
// Code to be executed if expression doesn't match any case
}
- expression: This is the variable or expression that is being evaluated.
- case: Each case represents a possible value of the expression. If a case matches, the corresponding block of code will execute.
- break: The break statement exits the switch block once a match is found and the corresponding code has executed. Without it, the program will continue executing the next case statements even if a match is found (this is known as "fall-through").
- default: This is an optional block that executes if none of the cases match the expression.
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int day = 3;
// Switch statement to print the name of the day based on its number
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
printf("Invalid day\n");
}
return 0;
}
Output:
Wednesday
Explanation:
- In this example, the value of the variable day is 3. The switch statement checks each case in sequence.
- When it reaches case 3, it matches the value of day and executes the corresponding block, printing "Wednesday".
- After executing the code for case 3, the break statement ensures that the program exits the switch block, preventing further cases from being checked.
- If day were outside the range of 1 to 7, the default case would execute, printing "Invalid day".
Also Read: What is a Switch Case in Java & How to Use It?
Now that you've mastered decision-making let's explore how conditional statements in C can fine-tune those choices and add more flexibility to your code.
Conditional Statement in C
In C programming, the conditional statement is a powerful tool that helps control the flow of your program based on conditions. One of the most common types of decision control statements in C is the conditional operator (also called the ternary operator).
This statement is a concise way of making decisions in a single line, which is particularly useful for simple conditions.
The syntax of the conditional operator is as follows:
(condition) ? expression_1 : expression_2;
- condition: The expression that is evaluated. If it’s true (non-zero), expression_1 is executed.
- expression_1: This part is executed if the condition is true.
- expression_2: This part is executed if the condition is false.
Let’s dive deeper with an example to make this concept clearer.
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int num1 = 10, num2 = 5;
// Conditional operator to find the larger number
int larger = (num1 > num2) ? num1 : num2; // If num1 is greater than num2, assign num1 to 'larger'; otherwise, assign num2.
printf("The larger number is: %d\n", larger); // Output the result
return 0;
}
Output:
The larger number is: 10
Explanation:
- Condition: (num1 > num2) checks if num1 is greater than num2.
- If the condition is true (which it is, since 10 > 5), expression_1 (num1) gets assigned to the variable larger.
- If the condition had been false, expression_2 (num2) would have been assigned to larger.
In this case, since num1 is greater than num2, the value of larger becomes 10, which is then printed to the screen.
Why use the conditional statement in C?
- Concise Syntax: The conditional operator helps reduce the number of lines of code when compared to using multiple if statements, especially in simple conditions.
- Quick Decisions: It’s useful for quick, one-line decisions. For example, checking which number is larger or assigning values based on conditions.
- Improved Readability: For small decisions, the conditional operator keeps the code clean and easy to read.
The conditional statement in C is most useful in cases where you need to assign values based on simple conditions. It simplifies your code and makes it easier to understand, especially for simple checks like finding the larger number or choosing between two options.
Now that you’ve made decisions, let’s take control of repetition with loop statements to keep things running smoothly.
Loop Control Statement in C
In C programming, loop control statements are essential when you need to repeat a block of code multiple times based on certain conditions. These loops allow your program to handle repetitive tasks efficiently without having to write redundant code.
Let’s dive into each of them, understand their syntax, and explore examples.
1. While Loop
The while loop continues executing as long as the specified condition remains true. The condition is checked before executing the loop body, so if the condition is false initially, the body may not run at all.
Syntax:
while (condition) {
// Code to be executed as long as the condition is true
}
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int count = 1;
// While loop to print numbers 1 to 5
while (count <= 5) { // Condition: keep running as long as count is <= 5
printf("%d ", count); // Print current value of count
count++; // Increment count by 1
}
return 0;
}
Output:
1 2 3 4 5
Explanation:
- The while loop checks if the condition count <= 5 is true.
- If true, the loop prints the current value of count and increments it by 1.
- The loop runs until count exceeds 5.
Also Read: While Loop in Python [With Syntax and Examples]
2. Do-While Loop
The do-while loop is similar to the while loop, but with one key difference: the condition is checked after the loop body is executed. This means the loop body will always run at least once, even if the condition is false initially.
Syntax:
do {
// Code to be executed
} while (condition);
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int count = 6;
// Do-while loop to print numbers 1 to 5
do {
printf("%d ", count); // Print current value of count
count++; // Increment count by 1
} while (count <= 5); // Condition is checked after loop execution
return 0;
}
Output:
6
Explanation:
- The loop runs once even though the initial value of count is 6, which is greater than 5.
- Since the condition is checked after the loop body, it executes one time before checking if the condition is true.
3. For Loop
The for loop is used when you know the number of iterations beforehand. It combines the initialization, condition-checking, and increment/decrement in a single line. It is ideal for looping through arrays or repeating tasks a set number of times.
Syntax:
for (initialization; condition; increment) {
// Code to be executed
}
Flowchart:
Example Code:
#include <stdio.h>
int main() {
// For loop to print numbers 1 to 5
for (int count = 1; count <= 5; count++) { // Initialization: count = 1, Condition: count <= 5, Increment: count++
printf("%d ", count); // Print current value of count
}
return 0;
}
Output:
1 2 3 4 5
Explanation:
- The loop initializes count to 1.
- It checks the condition count <= 5.
- If true, it prints the value of count and increments it by 1. This process continues until count exceeds 5.
With conditions and loops in place, it’s time to explore how jump statements can quickly change the course of your program.
Jump Statement in C
Jump statements in C allow you to control the flow of your program by skipping parts of code or jumping to specific locations in the program. These statements are part of the types of control statements in C, and they provide you with flexibility when you need to exit loops early, skip iterations, or return from functions.
Let’s break down each of these jump statements and explore how they work with examples.
1. Break Statement
The break statement is used to exit from a loop or switch statement before the loop or switch has completed all iterations or cases. It’s commonly used when a specific condition is met, and you want to stop further processing.
Syntax:
break;
Flowchart:
Example Code:
#include <stdio.h>
int main() {
// For loop that breaks when count reaches 3
for (int count = 1; count <= 5; count++) {
if (count == 3) {
break; // Exit the loop when count is 3
}
printf("%d ", count); // Print current value of count
}
return 0;
}
Output:
1 2
Explanation:
- The for loop runs from 1 to 5, but when count becomes 3, the break statement is executed.
- This exits the loop immediately, so only 1 and 2 are printed.
2. Continue Statement
The continue statement skips the current iteration of a loop and moves to the next iteration. It is often used when a certain condition is met, and you want to skip the rest of the code in that iteration.
Syntax:
continue;
Flowchart:
Example Code:
#include <stdio.h>
int main() {
// For loop that skips printing 3
for (int count = 1; count <= 5; count++) {
if (count == 3) {
continue; // Skip this iteration when count is 3
}
printf("%d ", count); // Print current value of count
}
return 0;
}
Output:
1 2 4 5
Explanation:
- The continue statement is executed when count is 3, causing the loop to skip the rest of the code for that iteration.
- Therefore, 3 is not printed, and the loop continues with the next iteration.
Also Read: Python Break, Continue & Pass Statements [With Examples]
3. Goto Statement
The goto statement is used to jump to a specific label within your program. While it provides direct control over the flow, it should be used sparingly, as it can make your code harder to read and maintain.
Syntax:
goto label;
label:
// Code to be executed after jump
Flowchart:
Example Code:
#include <stdio.h>
int main() {
int num = 5;
// If num is less than 10, jump to 'end' label
if (num < 10) {
goto end; // Jump directly to the end label
}
printf("This will be skipped.\n");
end: // Label for jump
printf("This is where the program jumps to.\n");
return 0;
}
Output:
This is where the program jumps to.
Explanation:
- The goto statement causes the program to jump to the end label, skipping the line that prints "This will be skipped."
- The program then executes the code at the end label.
4. Return Statement
The return statement is used to exit a function and optionally return a value to the calling function. This is commonly used to signal the end of a function's execution and return control to the calling code.
Syntax:
return value;
Flowchart:
Example Code:
#include <stdio.h>
int add(int a, int b) {
return a + b; // Return the sum of a and b
}
int main() {
int result = add(3, 4); // Call the add function
printf("The sum is: %d\n", result); // Output the result
return 0;
}
Output:
The sum is: 7
Explanation:
- The add function returns the sum of a and b using the return statement.
- The main function captures the returned value and prints the result.
Now that you've seen each control statement in action, let's compare them side by side to understand their strengths and when to use each.
Comparison of Control Statements in C
In C programming, each control statement serves a unique purpose, and choosing the right one depends on the task at hand.
Control Statement |
Purpose |
Condition Type |
Use Case |
if-else | Decision-making based on a condition | Single condition (true/false) | Used for simple true/false conditions, like validating input or checking flags. |
switch | Multi-way decision based on a variable's value | Multiple values for one variable | Used for checking multiple possible values of a single variable (e.g., menu selection). |
for loop | Repeating a task a set number of times | Range or counter condition | Iterating through a collection or repeating a task for a fixed number of times. |
while & do-while loop | Repeating a task based on a condition | True/false condition | While is used when the number of iterations is unknown, do-while ensures at least one iteration. |
Now that you know how control statements work, let’s explore their pros and cons to understand when and why to use them effectively.
What are Advantages and Disadvantages of Control Statement in C?
Control statements are essential tools in C programming, allowing you to define the flow of execution. But like any tool, they come with their own set of advantages and disadvantages.
Advantages
Control statements allow your program to make decisions, repeat tasks, or even jump to different parts of the code.
Advantage |
Explanation |
Flexibility | Control statements allow your program to adapt to various conditions dynamically. |
Efficiency | Loops and conditional statements help reduce code redundancy, improving performance. |
Readability | Well-organized control structures make your code clearer and easier to follow. |
Optimal Resource Usage | Efficient loops and decision-making reduce unnecessary computations, optimizing program performance. |
Disadvantages
While control statements bring flexibility and efficiency, they also come with some limitations. Misusing them or choosing the wrong type of control statement can make your code more complex, difficult to maintain, or inefficient.
Disadvantage |
Explanation |
Overuse of if-else | Too many nested or complex if-else statements make your code harder to read, debug, and maintain. |
Complexity | Using multiple nested control statements can result in difficult-to-follow and error-prone code. |
Performance Issues | Inefficient control flow, especially in loops, can slow down the program, especially with large data sets. |
Code Duplication | Repetitive control statements across your program can lead to code duplication, increasing maintenance overhead. |
The right control statement can greatly improve the performance, readability, and efficiency of your program. Just be cautious of their limitations, especially when dealing with complex conditions and large data sets.
How Can upGrad Help You Advance Your Career in Programming?
upGrad is your go-to partner for mastering essential programming concepts for a successful software development career. With over 10 million learners and 200+ industry-driven courses, upGrad equips you with the skills to stay competitive.
Whether you're advancing in C programming, decision-making, or system design, upGrad helps build a solid foundation for your career.
Here are the top courses:
- Data Structures & Algorithms
- Learn Basic Python Programming
- AI-Powered Full Stack Development Course by IIITB
- Core Java Basics
You can also get personalized career counseling with upGrad to guide your career path, or visit your nearest upGrad center and start hands-on training today!
Boost your career with our popular Software Engineering courses, offering hands-on training and expert guidance to turn you into a skilled software developer.
Explore our Popular Software Engineering Courses
Master in-demand Software Development skills like coding, system design, DevOps, and agile methodologies to excel in today’s competitive tech industry.
In-Demand Software Development Skills
Stay informed with our widely-read Software Development articles, covering everything from coding techniques to the latest advancements in software engineering.
Read our Popular Articles related to Software
Frequently Asked Questions
1. How do control statements in C impact program performance?
2. Can you use multiple control statements together in C?
3. What is the role of the switch statement in C?
4. When should I use a while loop over a for loop in C?
5. What is the difference between break and continue in C control statements?
6. How do nested control statements work in C?
7. What are the practical use cases for goto in C?
8. How does the if-else statement work in C?
9. Can you combine different types of control statements in C for complex decisions?
10. How can control statements improve code readability in C?
11. Are control statements in C the same for all programming languages?
Get Free Consultation
By submitting, I accept the T&C and
Privacy Policy
India’s #1 Tech University
Executive PG Certification in AI-Powered Full Stack Development
77%
seats filled
Top Resources