- C Programming Tutorial
- C - Home
- Basics of C
- C - Introduction
- C - Features
- C - Basics
- C - History
- C - Structure of C Program
- C - Program Structure
- C - Hello World
- C - Compilation Process
- C - Comments
- C - Tokens
- C - Keywords
- C - Identifiers
- C - User Input
- C - Basic Syntax
- C - Data Types
- C - Variables
- C - Integer Promotions
- C - Type Conversion
- C - Type Casting
- C - Booleans
- Constants and Literals in C
- C - Constants
- C - Literals
- C - Escape sequences
- C - Format Specifiers
- Operators in C
- C - Operators
- C - Arithmetic Operators
- C - Relational Operators
- C - Logical Operators
- C - Bitwise Operators
- C - Assignment Operators
- C - Unary Operators
- C - Increment and Decrement Operators
- C - Ternary Operator
- C - sizeof Operator
- C - Operator Precedence
- C - Misc Operators
- Decision Making in C
- C - Decision Making
- C - if statement
- C - if...else statement
- C - nested if statements
- C - switch statement
- C - nested switch statements
- Loops in C
- C - Loops
- C - While loop
- C - For loop
- C - Do...while loop
- C - Nested loop
- C - Infinite loop
- C - Break Statement
- C - Continue Statement
- C - goto Statement
- Functions in C
- C - Functions
- C - Main Function
- C - Function call by Value
- C - Function call by reference
- C - Nested Functions
- C - Variadic Functions
- C - User-Defined Functions
- C - Callback Function
- C - Return Statement
- C - Recursion
- Scope Rules in C
- C - Scope Rules
- C - Static Variables
- C - Global Variables
- Arrays in C
- C - Arrays
- C - Properties of Array
- C - Multi-Dimensional Arrays
- C - Passing Arrays to Function
- C - Return Array from Function
- C - Variable Length Arrays
- Pointers in C
- C - Pointers
- C - Pointers and Arrays
- C - Applications of Pointers
- C - Pointer Arithmetics
- C - Array of Pointers
- C - Pointer to Pointer
- C - Passing Pointers to Functions
- C - Return Pointer from Functions
- C - Function Pointers
- C - Pointer to an Array
- C - Pointers to Structures
- C - Chain of Pointers
- C - Pointer vs Array
- C - Character Pointers and Functions
- C - NULL Pointer
- C - void Pointer
- C - Dangling Pointers
- C - Dereference Pointer
- C - Near, Far and Huge Pointers
- C - Initialization of Pointer Arrays
- C - Pointers vs. Multi-dimensional Arrays
- Strings in C
- C - Strings
- C - Array of Strings
- C - Special Characters
- C Structures and Unions
- C - Structures
- C - Structures and Functions
- C - Arrays of Structures
- C - Self-Referential Structures
- C - Lookup Tables
- C - Dot (.) Operator
- C - Enumeration (or enum)
- C - Structure Padding and Packing
- C - Nested Structures
- C - Anonymous Structure and Union
- C - Unions
- C - Bit Fields
- C - Typedef
- File Handling in C
- C - Input & Output
- C - File I/O (File Handling)
- C Preprocessors
- C - Preprocessors
- C - Pragmas
- C - Preprocessor Operators
- C - Macros
- C - Header Files
- Memory Management in C
- C - Memory Management
- C - Memory Address
- C - Storage Classes
- Miscellaneous Topics
- C - Error Handling
- C - Variable Arguments
- C - Command Execution
- C - Math Functions
- C - String Functions
- C - Static Keyword
- C - Random Number Generation
- C - Command Line Arguments
C Programming - C Decision Making
![]() Share with a Friend |
C Programming - C Decision Making
C Decision Making
In C programming, decision-making statements allow the program to execute different statements based on conditions. These statements evaluate an expression or condition and make decisions accordingly.
Types of Decision Making Statements in C
- if statement
- if-else statement
- else-if ladder
- switch statement
- ternary operator (? :)
- if Statement
The if statement evaluates a condition and, if the condition is true, executes the block of code inside the curly braces {}.
- Syntax:
C
if (condition) {
// Code to be executed if condition is true
}
- Example:
C
int a = 10;
if (a > 5) {
printf("a is greater than 5\n");
}
// Output: a is greater than 5
- if-else Statement
The if-else statement evaluates a condition and executes one block of code if the condition is true and another block if the condition is false.
- Syntax:
C
if (condition) {
// Code to be executed if condition is true
} else {
// Code to be executed if condition is false
}
- Example:
C
int a = 3;
if (a > 5) {
printf("a is greater than 5\n");
} else {
printf("a is less than or equal to 5\n");
}
// Output: a is less than or equal to 5
- else-if Ladder
The else-if ladder is used when you need to test multiple conditions. If the first condition is false, it checks the second condition, and so on.
- Syntax:
C
if (condition1) {
// Code to be executed if condition1 is true
} else if (condition2) {
// Code to be executed if condition2 is true
} else {
// Code to be executed if all the above conditions are false
}
- Example:
C
int a = 10;
if (a > 15) {
printf("a is greater than 15\n");
} else if (a > 5) {
printf("a is greater than 5 but less than or equal to 15\n");
} else {
printf("a is less than or equal to 5\n");
}
// Output: a is greater than 5 but less than or equal to 15
- switch Statement
The switch statement is used to check a variable against a set of values. It is a cleaner alternative to a series of if-else if statements when you need to evaluate the same variable or expression.
- Syntax:
C
switch (expression) {
case value1:
// Code to be executed if expression == value1
break;
case value2:
// Code to be executed if expression == value2
break;
default:
// Code to be executed if none of the cases match
}
- Example:
C
int day = 3;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
default:
printf("Invalid day\n");
}
// Output: Wednesday
- Note :
- break is used to exit the switch block.
- The default case is optional but is used when no cases match.
- Ternary Operator (? :)
The ternary operator is a shorthand for the if-else statement. It allows you to assign a value based on a condition in a concise way.
- Syntax:
C
condition ? expression_if_true : expression_if_false;
- Example:
C
int a = 10, b = 5;
int max = (a > b) ? a : b; // If a > b, max gets the value of a, else b
printf("Max value: %d\n", max); // Output: Max value: 10
Decision-Making Example:
C
#include <stdio.h>
int main() {
int a = 10, b = 20, choice;
printf("Enter your choice (1 for addition, 2 for subtraction): ");
scanf("%d", &choice);
if (choice == 1) {
printf("Addition result: %d\n", a + b);
} else if (choice == 2) {
printf("Subtraction result: %d\n", a - b);
} else {
printf("Invalid choice\n");
}
return 0;
}
Output (example):
Enter your choice (1 for addition, 2 for subtraction): 1
Addition result: 30
Summary of Decision Making in C
- if statement: Executes a block of code if a condition is true.
- if-else statement: Executes one block of code if the condition is true, and another block if false.
- else-if ladder: Allows testing multiple conditions sequentially.
- switch statement: Selects one of many code blocks to execute based on the value of an expression.
- Ternary operator (? :): A concise way to implement a simple if-else statement.
Decision-making statements are fundamental to controlling the flow of a program based on conditions in C. They enable the program to make choices and execute the relevant code accordingly.
