Control Statements in C: An Overview
Hello there, budding programmer! Today, we’re going to dive into the world of control statements in C. These little gems are the backbone of your C programs, directing the flow of execution and making your code dance to your tune. So, let’s get started, shall we?
Table of Contents
What are Control Statements?
In the simplest terms, control statements in C help your computer decide whether to run a certain piece of code or not. They’re like the traffic lights of your program, guiding the flow of execution based on certain conditions.
There are three main types of control statements in C:
- Selection statements: These decide which block of code to execute based on a condition. The
if
,if-else
, andswitch
statements fall into this category. - Iteration statements: Also known as loops, these statements repeat a block of code until a condition is met. The
for
,while
, anddo-while
loops are your go-to iteration statements. - Jump statements: These statements transfer control from one part of the program to another. The
break
,continue
, andgoto
statements are examples of jump statements.
Examples of Control Statements
Let’s take a look at some examples of these control statements in action.
The if
Statement
The if
statement is the simplest form of control statement. It checks a condition, and if the condition is true, it executes a block of code.
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("The number is positive\n");
}
return 0;
}
CIn this example, the if
statement checks if the number is greater than zero. Since num
is 10, the condition is true, so the program prints “The number is positive”.
The for
Loop
The for
loop is a powerful iteration statement that lets you execute a block of code a certain number of times.
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("%d\n", i);
}
return 0;
}
CIn this example, the for
loop runs five times, printing the numbers 0 through 4.
The switch
Statement
The switch
statement is a multi-way branch that lets you choose between several blocks of code based on the value of an expression.
#include <stdio.h>
int main() {
int num = 10;
switch (num) {
case 0:
printf("The number is zero\n");
break;
case 10:
printf("The number is ten\n");
break;
default:
printf("The number is not zero or ten\n");
}
return 0;
}
CIn this example, the switch
statement checks the value of num
. Since num
is 10, it executes the second case
and prints “The number is ten”.
Code Examples
Now that we’ve seen some basic examples, let’s dive into some more complex code examples that use multiple control statements.
Example 1: A Simple Calculator
#include <stdio.h>
int main() {
char operator;
double num1, num2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%lf %lf", &num1, &num2);
switch (operator) {
case '+':
printf("%.1lf + %.1lf = %.1lf", num1, num2, num1 + num2);
break;
case '-':
printf("%.1lf - %.1lf = %.1lf", num1, num2, num1 - num2);
break;
case '*':
printf("%.1lf * %.1lf = %.1lf", num1, num2, num1 * num2);
break;
case '/':
if (num2 != 0.0)
printf("%.1lf / %.1lf = %.1lf", num1, num2, num1 / num2);
else
printf("Error! Division by zero is not allowed.");
break;
default:
printf("Error! Invalid operator.");
}
return 0;
}
CIn this example, the switch
statement checks the operator entered by the user. Depending on the operator, it performs the corresponding arithmetic operation. If the operator is not one of ‘+’, ‘-‘, ‘*’, or ‘/’, it prints an error message.
Example 2: A Number Guessing Game
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
int main() {
int secretNumber, guess, attempts = 0;
// initialize random number generator
srand(time(0));
secretNumber = rand() % 100 + 1;
printf("Welcome to the number guessing game!\n");
printf("Guess a number between 1 and 100: ");
// game loop
do {
scanf("%d", &guess);
attempts++;
if (guess < secretNumber) {
printf("Too low! Try again: ");
} else if (guess > secretNumber) {
printf("Too high! Try again: ");
}
} while (guess != secretNumber);
printf("Congratulations! You found the number in %d attempts.\n", attempts);
return 0;
}
CIn this example, the program generates a random number between 1 and 100. The player then has to guess the number. The do-while
loop continues until the player guesses the correct number. If the guess is too low or too high, the program gives a hint and asks for another guess.
Wrapping Up
Control statements in C are powerful tools that allow you to control the flow of your program based on conditions and loops. By understanding and using these statements effectively, you can create complex and efficient programs.
Frequently Asked Questions
-
How many control statements are there in C?
There are three types of control statements in C: selection, iteration, and jump statements.
-
What are the 3 types of control structures in C?
The three types of control structures in C are selection statements (if, if-else, switch), iteration statements (for, while, do-while), and jump statements (break, continue, goto).
-
What are controlling statements?
Control statements are used in programming languages to control the flow of execution of the program based on certain conditions or loops.
-
What are examples of control statements?
Examples of control statements in C include
if
,if-else
,for
,while
,do-while
,switch
,break
,continue
, andgoto
.
Related Tutorials
If you found this tutorial helpful, you might also enjoy these related tutorials: