0% found this document useful (0 votes)
4 views7 pages

Control Structures

Control structures in C dictate the flow of execution in programs, enabling decision-making, looping, and branching. They are categorized into Decision-Making, Loop, and Jump Control Structures, each with specific types such as if statements, while loops, and break statements. These constructs allow for the creation of complex and dynamic programs by controlling how code is executed based on conditions and iterations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
4 views7 pages

Control Structures

Control structures in C dictate the flow of execution in programs, enabling decision-making, looping, and branching. They are categorized into Decision-Making, Loop, and Jump Control Structures, each with specific types such as if statements, while loops, and break statements. These constructs allow for the creation of complex and dynamic programs by controlling how code is executed based on conditions and iterations.
Copyright
© All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd

Control structures

Control structures in C are constructs that dictate the flow of execution in a program. They
enable decision-making, looping, and branching, allowing for more complex and dynamic
programs.

Categories of Control Structures in C

1. Decision-Making Control Structure


2. Loop Control Structure
3. Jump Control Structure

1. Decision making Control structure/Statement

These control structures make decisions based on certain conditions. The execution path is
determined by the outcome of the condition.

Types of Decision-Making Control Structures:

1. if Statement
2. if-else Statement
3. else-if Ladder
4. Nested if Statement
5. switch Statement

a. if Statement

 Executes a block of code only if a specified condition is true.

Example:

int x = 10;

if (x > 0) {

printf("x is positive\n");

b. if-else Statement

 Executes one block of code if the condition is true, and another block if the condition is
false.

Example:
int x = -5;

if (x > 0) {

printf("x is positive\n");

} else {

printf("x is non-positive\n");

c. else-if Ladder

 Allows for checking multiple conditions in sequence.

Example:

int score = 85;

if (score >= 90) {

printf("Grade: A\n");

} else if (score >= 75) {

printf("Grade: B\n");

} else if (score >= 50) {

printf("Grade: C\n");

} else {

printf("Grade: F\n");

d. Nested if Statement

 An if statement placed inside another if statement.

Example:

int a = 10, b = 20;


if (a > 0) {

if (b > 0) {

printf("Both a and b are positive\n");

e. switch Statement

 Selects one of many code blocks to execute based on the value of an expression.

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("Other day\n");

2. Loop Control Structure


These control structures repeat a block of code multiple times.

Types of Loop Control Structures:

1. while Loop
2. do-while Loop
3. for Loop
4. Nested Loops

a. while Loop

 Repeats a block of code as long as the condition is true.

Example:

int i = 1;

while (i <= 5) {

printf("%d ", i);

i++;

b. do-while Loop

 Executes the block of code once before checking the condition, then continues as long as
the condition is true.

Example:

int i = 1;

do {

printf("%d ", i);

i++;

} while (i <= 5);

c. for Loop

 Repeats a block of code a specific number of times.


Example:

We calculate the sum of the first n natural numbers:


#include <stdio.h>

int main() {

int n, sum = 0;

// Asking for user input

printf("Enter a positive integer: ");

scanf("%d", &n);

// Summing numbers from 1 to n

for(int i = 1; i <= n; ++i) {

sum += i; }

// Output the result

printf("The sum of the first %d natural numbers is: %d\n", n, sum);

return 0;}

d. Nested Loops

 One loop inside another loop.

Example:

for (int i = 1; i <= 3; i++) {

for (int j = 1; j <= 2; j++) {

printf("i = %d, j = %d\n", i, j);

3. Jump Control Structure

These structures change the flow of control directly, often used to break out of loops or skip
certain sections of code.
Types of Jump Control Structures:

1. break Statement
2. continue Statement
3. goto Statement
4. return Statement

a. break Statement

 Exits a loop or switch statement immediately.

Example:

for (int i = 1; i <= 5; i++) {

if (i == 3) {

break; // Exits the loop when i equals 3

printf("%d ", i);

b. continue Statement

 Skips the current iteration of a loop and continues with the next iteration.

Example:

for (int i = 1; i <= 5; i++) {

if (i == 3) {

continue; // Skips the rest of the code when i equals 3

printf("%d ", i);

c. goto Statement

 Transfers control to the labeled statement. Its usage is generally discouraged.


Example:

int a = 10;

if (a > 0) {

goto positive;

printf("This won't be printed\n");

positive:

printf("a is positive\n");

d. return Statement

 Exits from a function and optionally returns a value.

Example:

int add(int x, int y) {

return x + y; // Returns the sum

You might also like