Skip to main content

Decision Control Structure in C Language

 In the C programming language, decision control structures allow you to control the flow of execution based on specific conditions. These structures help make decisions and choose different paths of execution within your program. There are three main decision control structures in C: if statement, if-else statement, and switch statement. Let's explore each one:

1. if Statement:
   The if statement allows you to execute a block of code if a condition is true. It has the following syntax:
   ```c
   if (condition) {
       // code to execute if the condition is true
   }
   ```
   Example:
   ```c
   int x = 5;
   if (x > 0) {
       printf("x is positive");
   }
   ```

2. if-else Statement:
   The if-else statement provides an alternative block of code to execute if the condition is false. It has the following syntax:
   ```c
   if (condition) {
       // code to execute if the condition is true
   }
   else {
       // code to execute if the condition is false
   }
   ```
   Example:
   ```c
   int x = -5;
   if (x > 0) {
       printf("x is positive");
   }
   else {
       printf("x is non-positive");
   }
   ```

3. switch Statement:
   The switch statement allows you to choose from multiple options based on the value of an expression. It has the following syntax:
   ```c
   switch (expression) {
       case value1:
           // code to execute if expression matches value1
           break;
       case value2:
           // code to execute if expression matches value2
           break;
       // more cases...
       default:
           // code to execute if expression does not match any case
   }
   ```
   Example:
   ```c
   int choice = 2;
   switch (choice) {
       case 1:
           printf("Option 1 selected");
           break;
       case 2:
           printf("Option 2 selected");
           break;
       default:
           printf("Invalid option");
   }
   ```

These decision control structures allow you to write code that can make decisions based on conditions and choose different paths of execution. By utilizing these structures, you can control the flow of your program and make it more dynamic and responsive to different scenarios.

 

Certainly! Here are examples of decision control structures in C, starting from basic to more advanced scenarios:

1. Basic if Statement:
   ```c
   int x = 10;
   if (x > 0) {
       printf("x is positive");
   }
   ```

2. if-else Statement:
   ```c
   int x = -5;
   if (x > 0) {
       printf("x is positive");
   }
   else {
       printf("x is non-positive");
   }
   ```

3. Nested if-else Statement:
   ```c
   int x = 10;
   if (x > 0) {
       if (x % 2 == 0) {
           printf("x is a positive even number");
       }
       else {
           printf("x is a positive odd number");
       }
   }
   else {
       printf("x is non-positive");
   }
   ```

4. if-else if-else Ladder:
   ```c
   int marks = 85;
   if (marks >= 90) {
       printf("Grade: A");
   }
   else if (marks >= 80) {
       printf("Grade: B");
   }
   else if (marks >= 70) {
       printf("Grade: C");
   }
   else if (marks >= 60) {
       printf("Grade: D");
   }
   else {
       printf("Grade: F");
   }
   ```

5. switch Statement:
   ```c
   int choice = 2;
   switch (choice) {
       case 1:
           printf("Option 1 selected");
           break;
       case 2:
           printf("Option 2 selected");
           break;
       default:
           printf("Invalid option");
   }
   ```

6. Advanced switch Statement:
   ```c
   char grade = 'B';
   switch (grade) {
       case 'A':
       case 'a':
           printf("Excellent");
           break;
       case 'B':
       case 'b':
           printf("Good");
           break;
       case 'C':
       case 'c':
           printf("Average");
           break;
       case 'D':
       case 'd':
           printf("Below Average");
           break;
       default:
           printf("Invalid grade");
   }
   ```

These examples demonstrate various scenarios where decision control structures in C can be used to handle different conditions and make appropriate decisions based on those conditions. By utilizing these structures effectively, you can write more flexible and responsive programs.

Comments

Popular posts from this blog

Tokens, Identifiers, Data Types, Sequence Control, Subprogram Control, Arrays, Structures, Union, String, Pointers, Functions, File Handling, Command Line Argumaents, Preprocessors in C with example

 Let's discuss each concept briefly and provide examples for better understanding: 1. Tokens: Tokens are the smallest building blocks in C programming. They include keywords, identifiers, constants, strings, operators, and punctuators. Example: ```c #include <stdio.h> int main() {     int num = 42;  // 'int', 'main', 'return', '42', '=', ';' are tokens     printf("Hello, World!");  // 'printf', '(', ')', 'Hello, World!', ';', are tokens     return 0;  // 'return', '0', ';' are tokens } ``` 2. Identifiers: Identifiers are names used to identify variables, functions, or other user-defined entities. Example: ```c int age = 30;  // 'age' is an identifier (variable name) void displayMessage() {  // 'displayMessage' is an identifier (function name)     // function body } ``` 3. Data Types: Data types define the type of data that can be stored in ...

Place holder and control character in c language

 In the C programming language, placeholders and control characters are used to format and control the output of text in console-based programs. They are special characters or sequences of characters that have specific meanings. Here are the placeholders and control characters commonly used in C: 1. Placeholders:    - %d: Used to display signed integers.      Example: printf("The value is %d", 10);    - %u: Used to display unsigned integers.      Example: printf("The value is %u", 10);    - %f: Used to display floating-point numbers.      Example: printf("The value is %f", 3.14);    - %c: Used to display characters.      Example: printf("The character is %c", 'A');    - %s: Used to display strings (sequence of characters).      Example: printf("The string is %s", "Hello");    - %p: Used to display memory addresses (pointers)...

what is requirements for web automation in python

 To perform web automation in Python, you'll need the following requirements: 1. Python: Make sure you have Python installed on your system. You can download the latest version from the official Python website (https://www.python.org/). 2. Web Driver: Web automation involves interacting with web browsers, so you'll need the appropriate web driver for the browser you want to automate. The most commonly used web drivers are:    - Chrome: ChromeDriver (https://sites.google.com/a/chromium.org/chromedriver/downloads)    - Firefox: GeckoDriver (https://github.com/mozilla/geckodriver/releases)    - Edge: Microsoft Edge WebDriver (https://developer.microsoft.com/en-us/microsoft-edge/tools/webdriver/) 3. Selenium: Selenium is a popular Python library for automating web browsers. Install it using pip: ```bash pip install selenium ``` 4. Browser: Install the web browser you want to automate (e.g., Chrome, Firefox, Edge). 5. IDE or Text Editor: Choose a code edito...