C Programming Language Tutorial
Variables and Data Types
Input/Output
Looping and Selection Structures
Array
Functions
Preprocessing Command
Pointer
Structure
File Operations
Important Knowledge
In the C programming language, selection and looping constructs are used to create conditional logic and perform repetitive tasks. These constructs include if-else
statements, switch
statements, for
loops, while
loops, and do-while
loops.
if-else
statements allow a program to make decisions based on the value of a particular condition, and execute different code based on the result of the condition.
switch
statements provide an alternative way to express a series of if-else
statements that test a single variable against a set of possible values.
for
loops are used to execute a block of code a specified number of times, with a loop index that can be used to control the loop.
while
loops are used to execute a block of code while a particular condition is true, and continue to execute the block of code until the condition becomes false.
do-while
loops are similar to while
loops, but they execute the block of code at least once, even if the condition is initially false.
These constructs are used in a wide variety of programming applications, and are essential for creating programs that are flexible, efficient, and maintainable. By using these constructs effectively, programmers can create programs that make decisions based on input or other factors, and perform different actions based on those decisions. Additionally, they can perform repetitive tasks in a controlled and efficient way, leading to more efficient and effective programs.
Using if Statements for Conditional Branching in C:
#include <stdio.h> int main() { int x = 10; // Using if statement for conditional branching if (x > 0) { printf("Positive\n"); } else if (x < 0) { printf("Negative\n"); } else { printf("Zero\n"); } return 0; }
The if
statement allows conditional execution of code based on a specified condition.
Switch Statements and Case Handling in C Language:
#include <stdio.h> int main() { int day = 3; // Switch statement for handling different cases switch (day) { case 1: printf("Monday\n"); break; case 2: printf("Tuesday\n"); break; case 3: printf("Wednesday\n"); break; default: printf("Unknown day\n"); } return 0; }
switch
statements provide an efficient way to handle multiple cases based on the value of an expression.
C Code Examples Demonstrating Selection Structures:
#include <stdio.h> int main() { int age = 25; // Using selection structures if (age < 18) { printf("You are a minor\n"); } else { printf("You are an adult\n"); } return 0; }
Selection structures, such as if
statements, help control the flow of a program based on conditions.
Using for Loops for Iteration in C:
#include <stdio.h> int main() { // Using for loop for iteration for (int i = 1; i <= 5; ++i) { printf("%d ", i); } return 0; }
for
loops provide a concise way to iterate over a range of values.
While Loops and Do-While Loops in C Programming:
#include <stdio.h> int main() { int i = 1; // Using while loop while (i <= 5) { printf("%d ", i); ++i; } // Using do-while loop do { printf("%d ", i); ++i; } while (i <= 10); return 0; }
while
loops and do-while
loops provide alternative ways for iteration.
Nested Selection and Looping Structures in C:
#include <stdio.h> int main() { int x = 5; // Nested selection and looping structures if (x > 0) { for (int i = 1; i <= x; ++i) { printf("%d ", i); } } else { printf("Non-positive\n"); } return 0; }
Nesting if
statements within loops or vice versa allows for more complex control flow.
Comparing Selection and Looping Structures in C:
#include <stdio.h> int main() { int sum = 0; // Using selection and looping structures for (int i = 1; i <= 5; ++i) { if (i % 2 == 0) { sum += i; } } printf("Sum of even numbers: %d\n", sum); return 0; }
Combining selection and looping structures allows for intricate control flow and computations.