0% found this document useful (0 votes)
5 views

Control Structure in C_Programming

Uploaded by

sharmaprabhujeet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
5 views

Control Structure in C_Programming

Uploaded by

sharmaprabhujeet
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 29

TABLE OF CONTENTS

1.DECISION CONTROL STRUCTURE................................................................................................................. 2


2. IF STATEMENT ........................................................................................................................................... 2
3. ELSE STATEMENT ....................................................................................................................................... 2
4. NESTED IF-ELSE STATEMENT ...................................................................................................................... 3

5. SWITCH CASE ............................................................................................................................................. 5


6. USE OF LOGICAL OPERATORS ..................................................................................................................... 6
7. THE ELSE IF CLAUSE.................................................................................................................................... 7
8. THE ! OPERATOR....................................................................................................................................... 8

Final Summary.............................................................................................................................................9

1. IF STATEMENT ....................................................................................................................................................9

2. ELSE STATEMENT .............................................................................................................................................11

3. NESTED IF-ELSE STATEMENT ..............................................................................................................................14

4. SWITCH-CASE STATEMENT ..............................................................................................................................18

5. LOGICAL OPERATORS .........................................................................................................................................22

6. ELSE-IF CLAUSE ...............................................................................................................................................25

1
1.DECISION CONTROL STRUCTURE

Decision control structures allow you to control the flow of your program based on certain conditions. The most
common decision control structures are if, else, else if, and switch statements.

2. IF STATEMENT

The if statement checks a condition and executes the code inside it if the condition is true.

Syntax:

if (condition) {

// Code to execute if the condition is true

Example:

#include <stdio.h>

int main() {

int number = 10;

if (number > 5) {

printf("Number is greater than 5.\n");

return 0;

Summary:

In this example, the if statement checks if number is greater than 5. Since 10 is greater than 5, the code inside
the if block executes, printing "Number is greater than 5."

3. ELSE STATEMENT

The else statement runs a block of code if the if condition is false.

2
Syntax:

if (condition) {

// Code to execute if the condition is true

} else {

// Code to execute if the condition is false

Example:

#include <stdio.h>

int main() {

int number = 3;

if (number > 5) {

printf("Number is greater than 5.\n");

} else {

printf("Number is less than or equal to 5.\n");

return 0;

Summary:

Here, since the number is 3, the condition in the if statement is false, so the code inside the else block is
executed, printing "Number is less than or equal to 5."

4. NESTED IF-ELSE STATEMENT

You can place one if or else statement inside another, creating a nested structure. This is useful for more
complex decision-making.

Syntax:

if (condition1) {

3
if (condition2) {

// Code if both conditions are true

} else {

// Code if condition1 is true but condition2 is false

} else {

// Code if condition1 is false

Example:

#include <stdio.h>

int main() {

int number = 8;

if (number > 5) {

if (number < 10) {

printf("Number is between 5 and 10.\n");

} else {

printf("Number is greater than or equal to 10.\n");

} else {

printf("Number is 5 or less.\n");

return 0;

Summary:

This program checks if number is greater than 5 and then checks if it's less than 10. Since number is 8, the
inner if block prints "Number is between 5 and 10."

4
5. SWITCH CASE

A switch statement is an alternative to a long chain of if-else statements when comparing the same
variable to different values.

Syntax:

switch (variable) {

case value1:

// Code to execute if variable equals value1

break;

case value2:

// Code to execute if variable equals value2

break;

default:

// Code to execute if none of the cases match

Example:

#include <stdio.h>

int main() {

int day = 2;

switch (day) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

case 3:

printf("Wednesday\n");

5
break;

default:

printf("Another day\n");

return 0;

Summary:

The switch statement compares day with predefined values. Since day is 2, it matches case 2 and prints
"Tuesday."

6. USE OF LOGICAL OPERATORS

Logical operators are used to combine multiple conditions in decision-making.

• &&: Logical AND


• ||: Logical OR
• !: Logical NOT

Example:

#include <stdio.h>

int main() {

int a = 5, b = 10;

if (a > 0 && b > 0) {

printf("Both numbers are positive.\n");

if (a > 0 || b < 0) {

printf("At least one condition is true.\n");

6
if (!(a > 10)) {

printf("a is not greater than 10.\n");

return 0;

Summary:

This example demonstrates logical operators. The first if uses && to check if both a and b are positive. The
second if uses || to check if at least one condition is true. The third if uses ! to negate the condition.

7. THE ELSE IF CLAUSE

The else if clause allows you to check multiple conditions sequentially.

Syntax:

if (condition1) {

// Code if condition1 is true

} else if (condition2) {

// Code if condition1 is false and condition2 is true

} else {

// Code if both conditions are false

Example:

#include <stdio.h>

int main() {

int number = 10;

if (number > 10) {

printf("Number is greater than 10.\n");

} else if (number == 10) {

7
printf("Number is equal to 10.\n");

} else {

printf("Number is less than 10.\n");

return 0;

Summary:

This example uses else if to check if number is greater than, equal to, or less than 10. Since number is
equal to 10, it prints "Number is equal to 10."

8. THE ! OPERATOR

The ! operator inverts the logical state of its operand. If a condition is true, ! makes it false, and vice versa.

Example:

#include <stdio.h>

int main() {

int isRaining = 0; // 0 means false

if (!isRaining) {

printf("It's not raining.\n");

return 0;

Summary:

In this example, !isRaining evaluates to true because isRaining is false (0). Hence, the program prints
"It's not raining."

8
Final Summary

• Decision Control Structure: Helps in controlling the flow of execution based on conditions.
• If Statement: Executes code when a specific condition is true.
• Else Statement: Executes code when the if condition is false.
• Nested If-Else: Allows more complex decision-making by embedding if-else inside other if-
else statements.
• Switch-Case: An alternative to if-else for checking a variable against multiple values.
• Logical Operators: Combine multiple conditions for more complex logic.
• Else-If Clause: Allows multiple sequential conditions to be checked.
• ! Operator: Inverts a condition’s truth value.

1. IF STATEMENT

Example 1 (Basic):

#include <stdio.h>

int main() {

int number = 5;

if (number == 5) {

printf("Number is 5.\n");

return 0;

Explanation: If number equals 5, it prints "Number is 5." Simple and straightforward.

Example 2 (Comparing Two Numbers):

#include <stdio.h>

int main() {

9
int a = 3, b = 5;

if (a < b) {

printf("a is less than b.\n");

return 0;

Explanation: Here, we check if a is less than b. Since 3 is less than 5, it prints "a is less than b."

Example 3 (Checking Even or Odd):

#include <stdio.h>

int main() {

int number = 4;

if (number % 2 == 0) {

printf("Number is even.\n");

return 0;

Explanation: We check if a number is even by using the modulus operator (%). If the remainder is 0, the
number is even.

Example 4 (Multiple Conditions):

#include <stdio.h>

10
int main() {

int number = 15;

if (number % 3 == 0) {

printf("Number is divisible by 3.\n");

return 0;

Explanation: This checks if the number is divisible by 3. If true, it prints the message.

Example 5 (Logical Condition):

#include <stdio.h>

int main() {

int a = 10, b = 20;

if (a < 15 && b > 15) {

printf("Both conditions are true.\n");

return 0;

Explanation: We combine two conditions with the && operator. The message is printed only if both conditions
are true.

2. ELSE STATEMENT

Example 1 (Basic Else):

#include <stdio.h>

11
int main() {

int number = 3;

if (number == 5) {

printf("Number is 5.\n");

} else {

printf("Number is not 5.\n");

return 0;

Explanation: If the condition is false, the else part executes, printing "Number is not 5."

Example 2 (Odd or Even Check):

#include <stdio.h>

int main() {

int number = 7;

if (number % 2 == 0) {

printf("Number is even.\n");

} else {

printf("Number is odd.\n");

return 0;

Explanation: If the number is not even, it prints "Number is odd."

12
Example 3 (Simple Voting Check):

#include <stdio.h>

int main() {

int age = 17;

if (age >= 18) {

printf("You are eligible to vote.\n");

} else {

printf("You are not eligible to vote.\n");

return 0;

Explanation: This checks if the user is old enough to vote. If not, it prints a different message.

Example 4 (Comparing Two Values):

#include <stdio.h>

int main() {

int a = 12, b = 10;

if (a > b) {

printf("a is greater than b.\n");

} else {

printf("a is not greater than b.\n");

13
return 0;

Explanation: Compares a and b and executes the appropriate block.

Example 5 (Discount Check):

#include <stdio.h>

int main() {

float price = 150.0;

if (price > 100.0) {

printf("You get a discount!\n");

} else {

printf("No discount available.\n");

return 0;

Explanation: Checks whether the price qualifies for a discount.

3. NESTED IF-ELSE STATEMENT

Example 1 (Basic Nesting):

#include <stdio.h>

int main() {

int number = 5;

if (number > 0) {

14
if (number < 10) {

printf("Number is between 0 and 10.\n");

return 0;

Explanation: If number is greater than 0, the nested condition checks if it's also less than 10.

Example 2 (Grading System):

#include <stdio.h>

int main() {

int score = 85;

if (score >= 60) {

if (score >= 85) {

printf("Grade: A\n");

} else {

printf("Grade: B\n");

} else {

printf("Grade: F\n");

return 0;

Explanation: This nested structure provides a simple grading system based on the score.

15
Example 3 (Temperature Check):

#include <stdio.h>

int main() {

int temp = 75;

if (temp > 60) {

if (temp < 80) {

printf("The temperature is moderate.\n");

} else {

printf("The temperature is hot.\n");

} else {

printf("The temperature is cold.\n");

return 0;

Explanation: Nested conditions determine if the temperature is cold, moderate, or hot.

Example 4 (Multiple Nested Conditions):

#include <stdio.h>

int main() {

int number = 15;

if (number > 0) {

if (number < 50) {

if (number % 5 == 0) {

16
printf("Number is positive, less than 50, and divisible by
5.\n");

return 0;

Explanation: This example shows deeper nesting with multiple checks.

Example 5 (Age Check with Nested Conditions):

#include <stdio.h>

int main() {

int age = 25;

if (age > 18) {

if (age < 30) {

printf("You are in your twenties.\n");

} else {

printf("You are above thirty.\n");

} else {

printf("You are under 18.\n");

return 0;

Explanation: Nested checks based on age group.

17
4. SWITCH-CASE STATEMENT

The switch-case is used to handle multiple possible conditions of a single variable without using multiple
if-else statements. Each case represents a possible value.

Example 1 (Basic):

#include <stdio.h>

int main() {

int day = 2;

switch (day) {

case 1:

printf("Monday\n");

break;

case 2:

printf("Tuesday\n");

break;

default:

printf("Another day\n");

return 0;

Steps:

1. We check the value of day.


2. If day equals 2, "Tuesday" is printed.
3. If day doesn't match any case, the default block runs.

Example 2 (More Days):

18
#include <stdio.h>

int main() {

int day = 4;

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;

default:

printf("Another day\n");

return 0;

Steps:

1. The switch checks if day equals 4.


2. It prints "Thursday" because the condition is met.
3. Always use break to avoid running the next cases.

Example 3 (Calculator):

19
#include <stdio.h>

int main() {

char operator = '+';

int a = 5, b = 3;

switch (operator) {

case '+':

printf("%d + %d = %d\n", a, b, a + b);

break;

case '-':

printf("%d - %d = %d\n", a, b, a - b);

break;

default:

printf("Operator not recognized\n");

return 0;

Steps:

1. The switch checks which operator is used.


2. If it's +, the addition is performed and printed.
3. If it's another operator, the default block executes.

Example 4 (More Operations):

#include <stdio.h>

int main() {

char operator = '*';

int a = 5, b = 3;

20
switch (operator) {

case '+':

printf("%d + %d = %d\n", a, b, a + b);

break;

case '-':

printf("%d - %d = %d\n", a, b, a - b);

break;

case '*':

printf("%d * %d = %d\n", a, b, a * b);

break;

default:

printf("Operator not recognized\n");

return 0;

Steps:

1. operator is *, so multiplication is performed.


2. Additional cases for + and - are handled as well.

Example 5 (Grades System):

#include <stdio.h>

int main() {

char grade = 'B';

switch (grade) {

case 'A':

printf("Excellent\n");

21
break;

case 'B':

printf("Good\n");

break;

case 'C':

printf("Fair\n");

break;

default:

printf("Invalid grade\n");

return 0;

Steps:

1. grade is checked.
2. If it's B, it prints "Good."
3. The default block handles any grade not listed.

5. LOGICAL OPERATORS

Logical operators are used to combine conditions. The main operators are && (AND), || (OR), and ! (NOT).

Example 1 (Basic AND):

#include <stdio.h>

int main() {

int a = 10, b = 20;

if (a > 5 && b > 15) {

printf("Both conditions are true.\n");

22
}

return 0;

Steps:

1. The if checks if a > 5 AND b > 15.


2. Both conditions are true, so the message is printed.

Example 2 (Basic OR):

#include <stdio.h>

int main() {

int a = 3, b = 20;

if (a < 5 || b > 25) {

printf("At least one condition is true.\n");

return 0;

Steps:

1. The if checks if a < 5 OR b > 25.


2. Since a < 5 is true, the message is printed.

Example 3 (Using NOT):

#include <stdio.h>

int main() {

23
int isRaining = 0;

if (!isRaining) {

printf("It's not raining.\n");

return 0;

Steps:

1. The ! operator negates isRaining.


2. Since isRaining is 0 (false), !isRaining is true.

Example 4 (Combining AND and OR):

#include <stdio.h>

int main() {

int a = 10, b = 5;

if (a > 5 && (b < 10 || b == 5)) {

printf("Complex condition is true.\n");

return 0;

Steps:

1. The if checks a combination of AND and OR conditions.


2. Since both parts are true, it prints the message.

Example 5 (Advanced Combination):

24
#include <stdio.h>

int main() {

int a = 5, b = 10, c = 15;

if ((a > 0 && b < 20) || !(c > 20)) {

printf("Advanced condition is true.\n");

return 0;

Steps:

1. This combines multiple logical operators.


2. The condition is true, so the message is printed.

6. ELSE-IF CLAUSE

The else if clause allows multiple sequential conditions to be checked in order.

Example 1 (Basic):

#include <stdio.h>

int main() {

int number = 10;

if (number > 10) {

printf("Number is greater than 10.\n");

} else if (number == 10) {

printf("Number is equal to 10.\n");

} else {

25
printf("Number is less than 10.\n");

return 0;

Steps:

1. The first condition is false, so it moves to else if.


2. The second condition is true, so "Number is equal to 10" is printed.

Example 2 (Temperature Check):

#include <stdio.h>

int main() {

int temp = 75;

if (temp > 90) {

printf("It's hot outside.\n");

} else if (temp > 60) {

printf("It's warm outside.\n");

} else {

printf("It's cold outside.\n");

return 0;

Steps:

1. Checks if the temperature is hot, warm, or cold.


2. Prints the appropriate message based on conditions.

26
Example 3 (Grade Evaluation):

#include <stdio.h>

int main() {

int score = 85;

if (score >= 90) {

printf("Grade: A\n");

} else if (score >= 80) {

printf("Grade: B\n");

} else {

printf("Grade: C\n");

return 0;

Steps:

1. Checks the score and assigns a grade.


2. Prints "Grade: B" since the score is 85.

Example 4 (Multiple Levels):

#include <stdio.h>

int main() {

int number = 25;

if (number > 50) {

printf("Number is large.\n");

} else if (number > 20) {

printf("Number is medium.\n");

27
} else if (number > 10) {

printf("Number is small.\n");

} else {

printf("Number is very small.\n");

return 0;

Steps:

1. Multiple else if conditions check different ranges for number.


2. Since number is 25, "Number is medium" is printed.

Example 5 (Advanced Grading System):

#include <stdio.h>

int main() {

int score = 55;

if (score >= 90) {

printf("Grade: A\n");

} else if (score >= 80) {

printf("Grade: B\n");

} else if (score >= 70) {

printf("Grade: C\n");

} else if (score >= 60) {

printf("Grade: D\n");

} else {

printf("Grade: F\n");

return 0;

28
}

Steps:

1. The if condition checks if the score is 90 or above, in which case it prints "Grade: A."
2. If not, it moves to the next condition to check if the score is 80 or above for a "Grade: B."
3. This continues until the last else block, which prints "Grade: F" if none of the previous conditions are
met.

Explanation: This program evaluates the score and assigns a letter grade. If the score is 90 or higher, the
student gets an A, and so on down the list until a failing grade of F is assigned if the score is below 60.

29

You might also like