In C programming, control statements help decide how the program runs. They allow the program to make choices, repeat actions, or jump to different parts of the code. These statements make programs more useful by adding decision-making and loops. There are three main types of control statements: decision-making statements (if, if-else, switch), looping statements (for, while, do-while), and jump statements (break, continue, goto, return). Learning these statements will help you write better and more flexible C programs.
Contents:
- What are Control Statements in C?
- Types of Control Statements in C
- if Statement in C
- if-else Statement in C
- else-if Ladder in C
- nested if Statement in C
- Switch Statement in C
- Looping Statements in C
- Jump Statements in C
- Comparison of Jump Statements in C
- FAQs on Control Statements in C
What are Control Statements in C?
Control statements in C are used to control the flow of execution of a program based on certain conditions or loops. They help in making decisions, repeating a set of instructions, or jumping to specific parts of the program.
Types of Control Statements in C
Control statements in C regulate the execution flow of a program. They are categorized into three main types:
1. Decision-Making Statements
These statements evaluate conditions and execute code accordingly.
- if statement
- if-else statement
- else-if ladder
- switch statement
2. Looping Statements (Iteration Statements)
These statements allow the execution of a block of code multiple times.
- for loop
- while loop
- do-while loop
3. Jump Statements
These statements are used to transfer control from one part of the program to another.
- break (terminates a loop or switch statement)
- continue (skips the current iteration of a loop)
- goto (jumps to a labeled statement)
- return (exits from a function and returns a value)
if Statement in C
The if statement is used to execute a block of code when a given condition is true.
Syntax:
if (condition) { // Code executes if condition is true }
Example:
#include <stdio.h> int main() { int score = 85; if (score >= 50) { printf("Congratulations! You have passed the Sanfoundry C Quiz.\n"); } return 0; }
Output:
Congratulations! You have passed the Sanfoundry C Quiz.
This C program checks if a score is 50 or higher. If true, it prints a success message. The if statement controls this check. Since the score is 85, the message is displayed. If the score were lower, nothing would print. The program ends with return 0.
if-else Statement in C
The if-else statement allows a program to execute one block of code when a condition is true and another block when it is false.
Syntax:
if (condition) { // Code executes if condition is true } else { // Code executes if condition is false }
Example:
#include <stdio.h> int main() { int score; printf("Enter your Sanfoundry quiz score: "); scanf("%d", &score); if (score >= 50) { printf("Congratulations! You have passed the Sanfoundry quiz.\n"); } else { printf("Sorry! You have failed the Sanfoundry quiz. Try again!\n"); } return 0; }
Input:
Enter your Sanfoundry quiz score: 40
Output:
Sorry! You have failed the Sanfoundry quiz. Try again!
This program asks for a quiz score and checks if it is 50 or more. If it is, it prints a message saying you passed. If the score is less than 50, it prints a message saying you failed. The if-else statement decides which message to show. After that, the program ends.
else-if Ladder in C
The else-if ladder in C is used when multiple conditions need to be checked sequentially. It allows the program to test several conditions one by one and execute the corresponding block of code when a condition is true. If none of the conditions are true, the else block executes.
Syntax:
if (condition1) { // Code executes if condition1 is true } else if (condition2) { // Code executes if condition2 is true } else if (condition3) { // Code executes if condition3 is true } else { // Code executes if all conditions are false }
Example:
#include <stdio.h> int main() { int marks; printf("Enter your marks in the Sanfoundry exam: "); scanf("%d", &marks); if (marks >= 90) { printf("Grade: A+ (Outstanding Performance!)\n"); } else if (marks >= 80) { printf("Grade: A (Excellent Work!)\n"); } else if (marks >= 70) { printf("Grade: B (Good Job!)\n"); } else if (marks >= 60) { printf("Grade: C (Satisfactory, but needs improvement.)\n"); } else if (marks >= 50) { printf("Grade: D (Just Passed, work harder.)\n"); } else { printf("Grade: F (Failed! Better luck next time.)\n"); } return 0; }
Input:
Enter your marks in the Sanfoundry exam: 75
Output:
Grade: B (Good Job!)
This program takes the user’s marks as input and assigns a grade based on the score. It uses an if-else if-else structure to check the marks and display the corresponding grade. Higher marks get better grades, while lower marks result in a lower grade or failure. The program helps the user know their performance.
nested if Statement in C
A nested if statement is an if statement inside another if statement. It is used when multiple conditions need to be checked in a hierarchical manner.
Syntax
if (condition1) { // Executes if condition1 is true if (condition2) { // Executes if condition2 is also true } }
The inner if statement runs only when the outer if condition is true. Multiple if statements can be nested within each other to check additional conditions.
Example:
#include <stdio.h> int main() { int score; printf("Enter your Sanfoundry quiz score (out of 100): "); scanf("%d", &score); if (score >= 50) { if (score >= 80) { printf("Excellent! You are a quiz master!\n"); } else { printf("Good job! You passed the quiz.\n"); } } else { printf("You failed the quiz. Try again!\n"); } return 0; }
Input 1:
Enter your Sanfoundry quiz score (out of 100): 85
Output 1:
Excellent! You are a quiz master!
Input 2:
Enter your Sanfoundry quiz score (out of 100): 65
Output 2:
Good job! You passed the quiz.
Input 3:
Enter your Sanfoundry quiz score (out of 100): 40
Output 3:
You failed the quiz. Try again!
This program asks the user to enter their quiz score. If the score is 50 or more, they pass. If it is 80 or more, they get an “Excellent!” message. If it is between 50 and 79, they get a “Good job!” message. If the score is below 50, they fail. This program gives clear feedback based on the user’s score.
Switch Statement in C
The switch statement allows a variable to be tested for equality against multiple values (cases). It is an alternative to using multiple if-else statements.
Syntax:
switch (expression) { case value1: // Code to execute if expression == value1 break; case value2: // Code to execute if expression == value2 break; default: // Code to execute if no case matches }
Example:
#include <stdio.h> int main() { char subject; printf("Select a subject (C - C Programming, D - Data Structures, O - Operating Systems): "); scanf(" %c", &subject); switch (subject) { case 'C': printf("You selected C Programming.\n"); break; case 'D': printf("You selected Data Structures.\n"); break; case 'O': printf("You selected Operating Systems.\n"); break; default: printf("Invalid selection! Please enter C, D, or O.\n"); } return 0; }
Input 1:
Select a subject (C - C Programming, D - Data Structures, O - Operating Systems): D
Output 1:
You selected Data Structures.
Input 2:
Select a subject (C - C Programming, D - Data Structures, O - Operating Systems): X
Output 2:
Invalid selection! Please enter C, D, or O.
This program asks the user to choose a subject by entering a letter: ‘C’ for C Programming, ‘D’ for Data Structures, or ‘O’ for Operating Systems. The program then displays the selected subject. If the user enters any other letter, it shows an error message. The switch statement makes the program easy to read and manage.
Looping Statements in C
Looping statements in C are used to execute a block of code multiple times until a specified condition is met. They help in reducing code redundancy and improving efficiency.
1. for Loop
Syntax:
for(initialization; condition; update) { // Code to be executed }
Example:
#include <stdio.h> int main() { char options[4] = {'A', 'B', 'C', 'D'}; printf("Sanfoundry Quiz Options:\n"); for(int i = 0; i < 4; i++) { printf("%c) Option %d\n", options[i], i + 1); } return 0; }
Output:
Sanfoundry Quiz Options: A) Option 1 B) Option 2 C) Option 3 D) Option 4
2. while Loop
Syntax:
while(condition) { // Code to be executed }
Example:
#include <stdio.h> int main() { int timer = 5; printf("You have %d seconds to submit your answer!\n", timer); while(timer > 0) { printf("Time left: %d seconds\n", timer); timer--; } printf("Time’s up! Submitting your last saved answer...\n"); return 0; }
Output:
You have 5 seconds to submit your answer! Time left: 5 seconds Time left: 4 seconds Time left: 3 seconds Time left: 2 seconds Time left: 1 second Time’s up! Submitting your last saved answer...
Jump Statements in C
Jump statements in C are used to transfer control from one point to another in a program. These statements allow programmers to skip certain parts of the code, exit loops, or switch execution flow.
Types of Jump Statements:
1. break Statement in C
Syntax:
break;
Example: Ending a quiz when a wrong answer is selected
#include <stdio.h> int main() { int answers[] = {1, 1, 0, 1}; // 0 means wrong answer int i; for(i = 0; i < 4; i++) { printf("Question %d: ", i + 1); if (answers[i] == 0) { printf("Wrong Answer! Ending quiz.\n"); break; } else { printf("Correct Answer!\n"); } } return 0; }
Output:
Question 1: Correct Answer! Question 2: Correct Answer! Question 3: Wrong Answer! Ending quiz.
2. continue Statement in C
Syntax:
continue;
Example: Skipping a question if it’s marked “unattempted”
#include <stdio.h> int main() { int answers[] = {1, -1, 1, -1}; // -1 means unattempted question for(int i = 0; i < 4; i++) { if (answers[i] == -1) { printf("Question %d was skipped.\n", i + 1); continue; } printf("Question %d: Answered correctly!\n", i + 1); } return 0; }
Output:
Question 1: Answered correctly! Question 2 was skipped. Question 3: Answered correctly! Question 4 was skipped.
3. goto Statement in C
Syntax:
goto label; ... label:
Example: Jumping to the end if a wrong answer is given
#include <stdio.h> int main() { int answer; printf("Enter your answer (1 for correct, 0 for wrong): "); scanf("%d", &answer); if (answer == 0) { goto wrongAnswer; } printf("Correct Answer! Proceeding to the next question.\n"); return 0; wrongAnswer: printf("Wrong Answer! Try again next time.\n"); return 0; }
Input & Output Example:
Enter your answer (1 for correct, 0 for wrong): 0 Wrong Answer! Try again next time.
4. return Statement in C
#include <stdio.h> int getNumber() { return 10; // Returning a value } int main() { int num = getNumber(); printf("The number is: %d\n", num); return 0; }
Output:
The number is: 10
Comparison of Jump Statements in C
Jump Statement | Purpose | Effect on Control Flow |
---|---|---|
break | Exits a loop or switch statement | Stops execution of the current loop or switch and moves to the next statement outside it |
continue | Skips the rest of the current loop iteration | Jumps to the next iteration without executing remaining statements in the loop body |
goto | Jumps to a labeled statement in the program | Causes an unconditional jump, which can make code harder to read and debug |
return | Exits a function and optionally returns a value | Ends function execution and transfers control back to the calling function |
FAQs on Control Statements in C
1. What are control statements in C?
Control statements in C are used to control the flow of execution in a program. They help in decision-making, looping, and jumping within the code.
2. What are the types of control statements in C?
Control statements in C are categorized into three types:
- Decision-Making Statements: if, if-else, nested if, switch
- Looping Statements: for, while, do-while
- Jump Statements: break, continue, goto, return
3. What is the purpose of decision-making statements?
Decision-making statements help a program choose what to do based on conditions. The if statement runs code only if a condition is true. The if-else statement picks between two choices. The switch statement checks a variable against many fixed values.
4. What is the difference between if-else and switch?
The if-else statement is flexible and works with complex conditions. The switch statement is better when checking a variable against many fixed values.
5. Why is the goto statement discouraged in C?
The goto statement is not recommended because it makes code hard to read. But it can be helpful in rare cases, like exiting deep loops.
6. How does the return statement work?
The return statement ends a function and can send a value back. In main(), it stops the program.
7. What happens if break is missing in a switch statement?
If a break is missing in a switch case, the program continues to the next case, which may cause errors. This is called “fall-through”.
Key Points to Remember
Here is the list of key points we need to remember about “Control Statements in C”.
- Control Statements in C help regulate the program flow by making decisions, looping through code, or jumping between sections.
- Decision-Making Statements include if, if-else, else-if ladder, and switch, allowing the program to execute specific code based on conditions.
- Looping Statements (for, while, do-while) repeat a block of code until a condition is met, improving efficiency and reducing redundancy.
- Jump Statements (break, continue, goto, return) control the program flow by skipping, exiting loops, or jumping to specific locations.
- The if Statement executes a block of code only if a given condition is true, while if-else allows different actions for true and false conditions.
- The switch Statement provides an efficient way to handle multiple conditions without using multiple if-else statements.
- Looping Statements execute code multiple times, with for being best for fixed iterations, while for unknown repetitions, and do-while ensuring at least one execution.
- Apply for C Internship
- Watch Advanced C Programming Videos
- Practice Computer Science MCQs
- Check C Books
- Check Computer Science Books