Control Structures (if, else, switch, loops)
Introduction
Control structures are fundamental constructs in programming languages that enable developers to control the flow of execution based on certain conditions or iterate over a sequence of statements. Among the most commonly used control structures are conditional statements (if, else, switch) and loops (for, while, do-while). This deep dive explores the syntax, semantics, and practical usage of control structures using examples in C, C++, and C# programming languages.
1. Conditional Statements (if, else, switch)
Conditional statements allow the execution of different blocks of code based on the evaluation of certain conditions.
Sample 1: C
#include <stdio.h>
int main() {
int num = 10;
if (num > 0) {
printf("Number is positive\n");
} else {
printf("Number is non-positive\n");
}
char grade = 'A';
switch (grade) {
case 'A':
printf("Excellent\n");
break;
case 'B':
printf("Good\n");
break;
default:
printf("Needs improvement\n");
}
return 0;
}
Sample 2: C++
#include <iostream>
int main() {
int num = 10;
if (num > 0) {
std::cout << "Number is positive" << std::endl;
} else {
std::cout << "Number is non-positive" << std::endl;
}
char grade = 'A';
switch (grade) {
case 'A':
std::cout << "Excellent" << std::endl;
break;
case 'B':
std::cout << "Good" << std::endl;
break;
default:
std::cout << "Needs improvement" << std::endl;
}
return 0;
}
Sample 3: C#
using System;
class Program {
static void Main(string[] args) {
int num = 10;
if (num > 0) {
Console.WriteLine("Number is positive");
} else {
Console.WriteLine("Number is non-positive");
}
char grade = 'A';
switch (grade) {
case 'A':
Console.WriteLine("Excellent");
break;
case 'B':
Console.WriteLine("Good");
break;
default:
Console.WriteLine("Needs improvement");
break;
}
}
}
2. Loops (for, while, do-while)
Loops allow the repetition of a block of code until a certain condition is met.
Sample 4: C
#include <stdio.h>
int main() {
for (int i = 0; i < 5; i++) {
printf("Iteration %d\n", i);
}
int j = 0;
while (j < 5) {
printf("Iteration %d\n", j);
j++;
}
int k = 0;
do {
printf("Iteration %d\n", k);
k++;
} while (k < 5);
return 0;
}
Sample 5: C++
#include <iostream>
int main() {
for (int i = 0; i < 5; i++) {
std::cout << "Iteration " << i << std::endl;
}
int j = 0;
while (j < 5) {
std::cout << "Iteration " << j << std::endl;
j++;
}
int k = 0;
do {
std::cout << "Iteration " << k << std::endl;
k++;
} while (k < 5);
return 0;
}
Sample 6: C#
using System;
class Program {
static void Main(string[] args) {
for (int i = 0; i < 5; i++) {
Console.WriteLine("Iteration " + i);
}
int j = 0;
while (j < 5) {
Console.WriteLine("Iteration " + j);
j++;
}
int k = 0;
do {
Console.WriteLine("Iteration " + k);
k++;
} while (k < 5);
}
}
Sample 7: C# (foreach)
using System;
class Program {
static void Main(string[] args) {
// Array example
int[] numbers = { 1, 2, 3, 4, 5 };
Console.WriteLine("Array Example:");
foreach (int num in numbers) {
Console.WriteLine(num);
}
// List example
var names = new List<string> { "Alice", "Bob", "Charlie", "David" };
Console.WriteLine("\nList Example:");
foreach (string name in names) {
Console.WriteLine(name);
}
// Dictionary example
var dictionary = new Dictionary<int, string>
{
{ 1, "One" },
{ 2, "Two" },
{ 3, "Three" }
};
Console.WriteLine("\nDictionary Example:");
foreach (var pair in dictionary) {
Console.WriteLine($"Key: {pair.Key}, Value: {pair.Value}");
}
}
}
The foreach loop iterates over each element in the specified collection, assigning the current element to the loop variable (num, name, or pair in the examples above) on each iteration. This loop simplifies iteration over collections and is commonly used in C# programming.
Conclusion
Control structures play a crucial role in programming, allowing developers to create flexible and efficient algorithms. By mastering conditional statements and loops, developers can write clean, maintainable, and scalable code. Understanding the syntax and semantics of control structures is essential for building robust software solutions across various programming languages and paradigms.
Leave a Reply