C Programming Language Free Course Part-2

78 / 100

C Programming Language Free Course Part-2

C Programming
C Programming

Control Structures

Control structures are used to alter the flow of execution in a program based on certain conditions. These include conditional statements and loops.

1. if Statement

The if statement evaluates a condition. If the condition is true, the block of code inside the if statement is executed.

Example:

if (condition) {
        // Code to execute if condition is true
}

2. if-else" Statement

The if-else statement allows for two paths of execution. If the condition is true, the if block executes; otherwise, the else block executes.

Example:

if (condition) {
      // Code to execute if condition is true
} else {
     // Code to execute if condition is false
}

3. if-else if Ladder

This is used when there are multiple conditions to evaluate. It checks each condition in sequence until it finds one that is true.

Example:

if (condition1) {
      // Code to execute if condition1 is true
} else if (condition2) {
    // Code to execute if condition2 is true
} else if (condition3) {
   // Code to execute if condition3 is true
} else {
// Code to execute if none of the above conditions are true
}

4. Nesting of if

Nesting refers to placing one if statement inside another. This allows for more complex decision-making.

Example:

if (condition1) {
      if (condition2) {
              // Code to execute if both condition1 and condition2 are true
      }
}

Loops

Loops allow a block of code to be executed repeatedly based on a condition. The main types of loops in C are for, while, and do-while.

1. for Loop

The for loop is used when the number of iterations is known. It has an initializer, a condition, and an increment/decrement.

Example:

for (initialization; condition; increment) {
        // Code to execute repeatedly
}

Example:

for (int i = 0; i < 10; i++) {
       printf("%d\n", i);
}

2. while Loop

The while loop repeats as long as the condition is true. The condition is checked before the loop body is executed.

while (condition) {
          // Code to execute repeatedly
}

Example:

int i = 0;
while (i < 10) {
      printf("%d\n", i);
      i++;
}

3. do-while Loop

The do-while loop is similar to the while loop, but the condition is checked after the loop body is executed, ensuring that the loop body runs at least once.

Example:

do {
   // Code to execute
} while (condition);

Example:

int i = 0;
do {
    printf("%d\n", i);
       i++;
} while (i < 10);

Jump Statements

Jump statements are used to alter the flow of control by jumping to another part of the program.

1. break

The break statement is used to exit a loop or switch statement prematurely.

Usage in Loops:

Example:

for (int i = 0; i < 10; i++) {
        if (i == 5) {
                 break; // Exit the loop when i is 5
         }
         printf("%d\n", i);
}

Usage in Switch:

switch (expression) {
        case 1:
                 // Code
                 break;
         case 2:
                // Code
                 break;
         default:
                    // Default code
}

2. continue

The continue statement skips the current iteration of a loop and moves to the next iteration.

Example:

for (int i = 0; i < 10; i++) {
       if (i % 2 == 0) {
               continue; // Skip the rest of the loop body for even numbers
         }
         printf("%d\n", i);
}

3. goto

The goto statement allows you to jump to a labeled statement in the program. It is generally discouraged as it can make code harder to follow.

Example:

int i = 0;
start:
        printf("%d\n", i);
        i++;
if (i < 5)
{
                         goto start;
}

4. exit

The exit function terminates program execution immediately. It is part of the standard library and requires including <stdlib.h>.

Example:

#include <stdlib.h>
int main() {
       printf("Program will exit now.\n");
       exit(0); // Exits the program
}

Switch Statement

The switch statement is a multi-way branch statement that provides an efficient way to transfer control to different parts of code based on the value of an expression.

switch (expression) {
  case constant1:
     // Code for case 1
        break;
   case constant2:
        // Code for case 2
          break;
// More cases
        default:
                  // Code if no case matches
}

Example:

int day = 3;
switch (day) {
        case 1:
                printf("Monday\n");
                break;
        case 2:
                 printf("Tuesday\n");
                 break;
         case 3:
                  printf("Wednesday\n");
                  break;
          default:
                  printf("Invalid day\n");
}

Types of Loops

Loops can be categorized based on how they are controlled:

  • Entry-Controlled Loops: The condition is checked before the loop body is executed. (for and while loops)
  • Exit-Controlled Loops: The loop body is executed at least once, and the condition is checked after the loop body. (do-while loop)

Example Programs

Example 1: Print Even Numbers

This program prints even numbers from 0 to 10 using a for loop.

#include <stdio.h>
int main() {
       for (int i = 0; i <= 10; i++) {
               if (i % 2 == 0) {
                       printf("%d\n", i);
            }
        }
return 0;
}

Example 2: Factorial Calculation

This program calculates the factorial of a number using a while loop.

 

#include <stdio.h>
int main() {
       int n, factorial = 1;
       printf("Enter a number: ");
       scanf("%d", &n);
     
int i = 1;
      while (i <= n) {
              factorial *= i;
              i++;
        }
    printf("Factorial of %d is %d\n", n, factorial);
       return 0;
}

Example 3: Menu Selection Using Switch

This program demonstrates using a switch statement to implement a simple menu.

#include <stdio.h>
int main() {
       int choice;
       printf("Menu:\n");
       printf("1. Add\n");
       printf("2. Subtract\n");
       printf("3. Multiply\n");
       printf("4. Divide\n");
       printf("Enter your choice: ");
       scanf("%d", &choice);
      switch (choice) {
              case 1:
                       printf("You selected Add\n");
                       break;
              case 2:
                       printf("You selected Subtract\n");
                       break;
               case 3:
                       printf("You selected Multiply\n");
                        break;
                case 4:
                       printf("You selected Divide\n");
                       break;
                 default:
                          printf("Invalid choice\n");
        }
         return 0;
}

Understanding these control structures and loops is crucial for writing efficient and effective C programs. They provide the necessary tools to control the flow of a program and to repeat tasks, allowing for complex logic and operations.

Read more-C Programming Language Free Course

WhatsAppEmailGmailCopy LinkXWordPressAmazon Wish ListLinkedInFacebookSnapchatSlashdotTelegramViberYahoo MailTrelloTwitterMessengerBlogger

Leave a comment

Exit mobile version