What is Control Flow Statement in C: Control flow statements in C are crucial components that dictate the sequence in which the instructions are executed. They allow developers to control the flow of a program based on specific conditions, thereby enabling decision-making, repetition, and branching. Understanding these statements is essential for writing efficient and logical code.
C Programming Video Tutorial Lecture-4
Table of Contents
Introduction of Control Flow Statement in C
Control flow statements are fundamental to programming in C. They allow for decision-making, repeating actions, and directing the flow of a program’s execution. Understanding how to effectively use these statements enables you to write more complex and efficient programs. Each control flow statement has its own specific use case, and choosing the right one depends on the particular logic and requirements of your program. Let’s explore the different types of control flow statements in C in depth:
- Conditional Statement or Selection Statement or Decision Control Statement
- Loop Control Statement or Iterative Statement or Looping Statement
- Jumping Statement
- Other Control Flow Statement
Conditional Statement in C
Conditional statements allow a program to make decisions and execute certain blocks of code based on whether a condition is true or false. It is also known as selection statement or decision control statement. The primary conditional statements in C are:
if statement
The if statement evaluates a condition enclosed in parentheses. If the condition is true (non-zero), the block of code following the if statement is executed.
Syntax:
if (condition) {
// Code to execute if condition is true
}
Example:
int a = 10;
if (a > 5) {
printf(“a is greater than 5\n”);
}
if… else statement
The if…else statement adds an alternative block of code that executes when the condition is false.
Syntax:
if (condition) {
// Code to execute if condition is true
} else {
// Code to execute if condition is false
}
Example:
int a = 4;
if (a > 5) {
printf(“a is greater than 5\n”);
} else {
printf(“a is not greater than 5\n”);
}
if…else if…else Ladder statement
This is used when there are multiple conditions to be checked. The conditions are evaluated in sequence, and the block of code for the first true condition is executed.
Syntax:
if (condition1) {
// Code to execute if condition1 is true
} else if (condition2) {
// Code to execute if condition2 is true
} else {
// Code to execute if all conditions are false
}
Example:
int a = 10;
if (a > 10) {
printf(“a is greater than 10\n”);
} else if (a == 10) {
printf(“a is equal to 10\n”);
} else {
printf(“a is less than 10\n”);
}
switch statement
The switch statement is used for handling multiple conditions based on the value of a single variable. It is often used as an alternative to an if…else if…else ladder when all conditions depend on the same variable.
switch (expression) {
case constant1:
// Code to execute if expression equals constant1
break;
case constant2:
// Code to execute if expression equals constant2
break;
…
default:
// Code to execute if expression doesn’t match any case
}
Example:
char grade = ‘B’;
switch (grade) {
case ‘A’:
printf(“Excellent!\n”);
break;
case ‘B’:
printf(“Well done!\n”);
break;
case ‘C’:
printf(“You passed\n”);
break;
default:
printf(“Invalid grade\n”);
}
Looping Statement in C
Looping statements in C allow a set of instructions to be executed repeatedly based on a condition. It is also known as iterive statement or repeatative statement. The primary looping constructs in C are:
for loop
he for loop is used when the number of iterations is known beforehand. It consists of three parts: initialization, condition, and increment/decrement.
Syntax:
for (initialization; condition; increment/decrement) {
// Code to be executed repeatedly
}
Example:
for (int i = 0; i < 5; i++) {
printf(“i = %d\n”, i);
}
while loop
The while loop is used when the number of iterations is not known beforehand, and the loop should continue as long as a condition is true.
Syntax:
while (condition) {
// Code to be executed repeatedly
}
Example:
int i = 0;
while (i < 5) {
printf(“i = %d\n”, i);
i++;
}
do…while loop
do…while Loop: The do…while loop is similar to the while loop, except that it guarantees the loop will execute at least once, as the condition is evaluated after the loop body.
Syntax:
do {
// Code to be executed repeatedly
} while (condition);
Example:
int i = 0;
do {
printf(“i = %d\n”, i);
i++;
} while (i < 5);
Jumping Statement in C
Jump statements allow control to jump to a different part of the program. In C, the primary jump statements are:
break statement
The break statement is used to terminate the nearest enclosing loop or switch statement prematurely.
Syntax:
break;
Example:
for (int i = 0; i < 10; i++) {
if (i == 5) {
break;
}
printf(“i = %d\n”, i);
}
continue statement
The continue statement skips the rest of the code inside the loop for the current iteration and jumps to the next iteration.
Syntax:
continue;
Example:
for (int i = 0; i < 5; i++) {
if (i == 2) {
continue;
}
printf(“i = %d\n”, i);
}
goto statement
The goto statement allows jumping to a labeled part of the code. It should generally be avoided because it can make code difficult to understand and maintain.
Syntax:
goto label;
…
label: // Code to execute
Example:
int a = 10;
if (a > 5) {
goto label;
}
printf(“This line will be skipped.\n”);
label:
printf(“Goto statement executed.\n”);
return statement
The return statement is used to terminate a function and optionally return a value to the function’s caller.
Syntax:
return expression;
Example:
int sum(int a, int b) {
return a + b;
}
Other Control Flow Statement in C
Apart from the basic control flow statements, there are other constructs like:
exit() function
The exit() function terminates the program entirely. It’s a standard library function that requires including .
Syntax:
exit(status);
Example:
#include <stdio.h>
int main() {
printf(“Exiting program.\n”);
exit(0);
}
What is the Difference Between for and while loop in C?
Feature | for loop | while loop |
---|---|---|
Syntax | for (initialization ; Condition ; increament/decrement){//write your code here} | while (Condition){// Write your code here} |
Use Case | Best suited when the number of iterations is known beforehand. | Best suited when number of iterations is not known beforehand and depends on a condition. |
Initialization | Initialization of the loop variables happens within the loop’s declaration. | Initialization must occur separately before the loop starts. |
Condition Evaluation | Condition is evaluated at the beginning of each iteration | Condition is evaluated at the beginning of each iteration |
Increment/decrement | typically specified in the loop’s declration, making it easy to manage. | Increment or decrement must be managed explicitly within the loop body. |
Readability | Often considered more compact and readable for simple counting loops. | May be less readable if initialization and increment or decrement are placed far from the condition. |
Flexibility | Less flexible for loops where the control variable needs to change in unconventional ways. | More flexible for loops with complex conditions or where multiple variables control the loop. |
Loop Control Statements | Both break and continue statements work the same way as in while loops. | Both break and continue statements work the same way as in for loop. |
Typical use cases | Iterating over a range of numbers, iterating with a known number of steps, etc. | Iterating until a condition is met, waiting for user input, reading data until EOF, etc. |
Scope of Loop | The loop variable declared in the for loop header is local to the loop. | The loop variable (if declared before the loop) is available even after the loop exits. |
Example | for(int i = 0; i<6; i++){ printf(“%d\n”, i); } | int i =0; while (i<7){ printf(“%d\n”, i); i++; } |
Infinite Loop | for(;;) can be used to create an infinite loop. | while (1) can be used to create an infinite loop. |
Frequently Asked Questions (FAQs)
What are control flow statements in C programming?
What is the difference between if and switch statements?
When should I use a for loop instead of a while loop?
What is an infinite loop, and how can it occur?
How do break and continue statements work in loops?
What is the purpose of the goto statement, and should I use it?
What is a do…while loop, and how is it different from a while loop?
Can I nest loops in C programming?
What happens if I don’t use a break statement in a switch case?
Is it possible to exit from a loop prematurely in C?
How can I create an infinite loop in C?
What is the scope of the loop control variable in a for loop?
How does the return statement affect control flow?
Can control flow statements be combined?
What are some common pitfalls to avoid with control flow statements?
Learn More, Subscribe to Our YouTube Channel
Subscribe “JNG ACADEMY” for more learning best content.
Important Links
C Programming Language Lecture-01
C Programming Language Lecture-02
C Programming Language Lecture-03
C Programming Language Lecture-04
C Programming Language Tutorial YouTube Playlist
Thank You So Much, Guys… For Visitinig our Website and YouTube.