0% found this document useful (0 votes)
8 views11 pages

C Control Structures Explained

Control structures in C dictate the flow of execution in a program and include selection, iteration, and jump statements. Selection statements like if, if-else, and switch allow for decision-making, while iteration statements like for, while, and do-while enable code repetition. Jump statements such as break, continue, and return facilitate control transfer, and the document also provides examples of common C programs utilizing these structures.

Uploaded by

Sruthy K Joseph
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)
8 views11 pages

C Control Structures Explained

Control structures in C dictate the flow of execution in a program and include selection, iteration, and jump statements. Selection statements like if, if-else, and switch allow for decision-making, while iteration statements like for, while, and do-while enable code repetition. Jump statements such as break, continue, and return facilitate control transfer, and the document also provides examples of common C programs utilizing these structures.

Uploaded by

Sruthy K Joseph
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 in C

Control structures determine the flow of execution of a C program. They control which
statements are executed and how many times.

Types of Control Structures in C

1. Selection (Decision-Making) Statements

Used to choose between alternatives.

a) if statement
if (condition)
{
// statements
}

b) if–else statement
if (condition)
{
// true block
}
else
{
// false block
}

c) else–if ladder
if (mark >= 90)
grade = 'A';
else if (mark >= 75)
grade = 'B';
else
grade = 'C';

d) switch statement
Used when there are multiple choices.

switch(choice)
{
case 1: printf("One"); break;
case 2: printf("Two"); break;
default: printf("Invalid");
}

2. Iteration (Looping) Statements

Used to repeat a block of code.

a) for loop
for(int i = 1; i <= 5; i++)
{
printf("%d ", i);
}

b) while loop
int i = 1;
while(i <= 5)
{
printf("%d ", i);
i++;
}

c) do–while loop

Executes at least once.

int i = 1;
do
{
printf("%d ", i);
i++;
} while(i <= 5);
3. Jump (Branching) Statements

Used to transfer control unconditionally.

Statement Purpose

break Exit loop or switch

continue Skip current iteration

goto Jump to labeled statement

return Exit from function

Example:

if (i == 3)
break;

4. Conditional Operator (?:)

A compact form of if–else.

max = (a > b) ? a : b;

Summary Table
Control Structure Statements

Selection if, if–else, else–if, switch

Iteration for, while, do–while

Jump break, continue, goto,


return

★​ Control structures in C are statements that control the order and flow of
execution of a program.
Below are simple and commonly asked C programs using control structures, suitable
for GXEST204 – Programming in C Lab and exams.

1. Program to check whether a number is even or odd


(if–else)
#include <stdio.h>
int main()
{
int n;
printf("Enter a number: ");
scanf("%d", &n);

if (n % 2 == 0)
printf("Even number");
else
printf("Odd number");

return 0;
}

2. Program to find the largest of two numbers (if–else)


#include <stdio.h>

int main()
{
int a, b;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);

if (a > b)
printf("%d is greater", a);
else
printf("%d is greater", b);

return 0;
}

3. Program to find the largest of three numbers (else–if


ladder)
#include <stdio.h>
int main()
{
int a, b, c;
printf("Enter three numbers: ");
scanf("%d %d %d", &a, &b, &c);

if (a > b && a > c)


printf("%d is largest", a);
else if (b > c)
printf("%d is largest", b);
else
printf("%d is largest", c);

return 0;
}

4. Program to display numbers from 1 to n (for loop)


#include <stdio.h>

int main()
{
int n;
printf("Enter n: ");
scanf("%d", &n);

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


printf("%d ", i);

return 0;
}

5. Program to find the sum of first n natural numbers


(while loop)
#include <stdio.h>

int main()
{
int n, sum = 0, i = 1;
printf("Enter n: ");
scanf("%d", &n);

while (i <= n)
{
sum += i;
i++;
}

printf("Sum = %d", sum);


return 0;
}

6. Program to print multiplication table (do–while loop)


#include <stdio.h>

int main()
{
int n, i = 1;
printf("Enter a number: ");
scanf("%d", &n);

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

return 0;
}
7. Program using switch case (Simple calculator)
#include <stdio.h>

int main()
{
int a, b;
char op;

printf("Enter operator (+, -, *, /): ");


scanf(" %c", &op);

printf("Enter two numbers: ");


scanf("%d %d", &a, &b);

switch (op)
{
case '+': printf("Result = %d", a + b); break;
case '-': printf("Result = %d", a - b); break;
case '*': printf("Result = %d", a * b); break;
case '/': printf("Result = %d", a / b); break;
default: printf("Invalid operator");
}

return 0;
}

8. Program using break and continue


#include <stdio.h>

int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
continue;
if (i == 9)
break;
printf("%d ", i);
}
return 0;
}

9. Program to check whether a number is prime (for


loop + if)

#include <stdio.h>

int main()

int n, i, flag = 0;

printf("Enter a number: ");

scanf("%d", &n);

if (n <= 1)

flag = 1;

for (i = 2; i <= n / 2; i++)

if (n % i == 0)

{
flag = 1;

break;

if (flag == 0)

printf("Prime number");

else

printf("Not a prime number");

return 0;

10. Program to reverse a number (while loop)

#include <stdio.h>

int main()

int n, rev = 0, rem;

printf("Enter a number: ");


scanf("%d", &n);

while (n != 0)

rem = n % 10;

rev = rev * 10 + rem;

n = n / 10;

printf("Reversed number = %d", rev);

return 0;

★​ Control structures include decision-making, looping, and jump statements used


to control program flow.

You might also like