0% found this document useful (0 votes)
20 views31 pages

Control Structures Final Notes

Uploaded by

unknownuser8856
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)
20 views31 pages

Control Structures Final Notes

Uploaded by

unknownuser8856
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
You are on page 1/ 31

Unit II Control Structures

●​ In C programming, a control structure decides how the flow of


program execution happens.​

●​ They tell the computer what to do next based on conditions or


repetitions.

Types of Control Structures


1. Conditional Statements

• if statement: Executes a block of code if a specified condition is true.


• if-else statement: Executes one block of code if the condition is true,
another if it is false.
• switch-case: Selects and executes one block of code among many options
based on the value of a variable.
• Nested if statements: An if or if-else statement inside another if or if-else
statement.

2. Loops and Iteration


• for loop: Repeats a block of code a specific number of times.
• while loop: Repeats a block of code as long as the condition is true.
• do-while loop: Executes the block of code once, then repeats as long as
the condition is true.

3. Jump Control Statements


• break: Exits the loop immediately, skipping the remaining iterations.
• continue: Skips the current iteration and proceeds with the next iteration
of the loop.
• Nested loops: A loop inside another loop; used for multi-dimensional data
processing.
1. if Statement
Explanation:

●​ The if statement is a decision-making control structure.​

●​ The if statement executes a block of code if a specified condition is


true.

Syntax:​
if (condition) {​
// code to execute if condition is true​
}

Example 1:​
int a = 10;​
if (a > 5) {

printf("a is greater than 5");​
}
====================================================
Algorithm:​
1. Start​
2. Read a​
3. Check if a > 5​
4. If true, print message​
5. End

Flowchart:​
Input:​
a = 10

Output:​
a is greater than 5

Example 2:
Check if a Number is Positive
#include <stdio.h>

int main() {
int num = 10;

if (num > 0) {
printf("The number is positive.\n");
}

return 0;
}

Example 3:
Check if a Number is Even
#include <stdio.h>

int main() {
int num = 8;

if (num % 2 == 0) {
printf("The number is even.\n");
}

return 0;
}

4.Check if a Number is a Multiple of 5


5.Check if Two Numbers are Equal
6.Check if a Character is an Uppercase Letter
7.Check if a Person is Eligible to Vote
2. if-else Statement
Explanation:

●​ The if-else statement is a decision-making control structure.


●​ It lets the program choose between two possible actions —one when
the condition is true, and another when the condition is false.​

●​ Executes one block of code if the condition is true, and another block
if it is false.

Syntax:​
if (condition) {​
// true block​
} else {​
// false block​
}

Example:​
int a = 3;​
if (a > 5) {​
printf("a is greater than 5");​
} else {​
printf("a is less than or equal to 5");​
}
Algorithm:​
1. Start​
2. Read a​
3. Check if a > 5​
4. If true, print first message​
5. Else, print second message​
6. End
Flowchart:​
Input:​
a=3

Output:​
a is less than or equal to 5

Example 2: Check if a Number is Positive or Negative


#include <stdio.h>

int main() {
int num = -10;

if (num >= 0) {
printf("The number is positive.\n");
} else {
printf("The number is negative.\n");
}

return 0;
}

Example 3: Check if a Number is Even or Odd


#include <stdio.h>

int main() {
int num = 7;

if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}

return 0;
}
Example 4: Check if a Person is Eligible to Vote
#include <stdio.h>

int main() {
int age = 16;

if (age >= 18) {


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

return 0;
}

Example 5: Find the Greater of Two Numbers


#include <stdio.h>

int main() {
int a = 25, b = 30;

if (a > b) {
printf("a is greater than b.\n");
} else {
printf("b is greater than or equal to a.\n");
}

return 0;
}

Example 6: Check if a Character is a Vowel or Not


Example 7: Check if Number is Zero or Non-Zero
else if:
●​ The else if statement in C is used to check multiple conditions.
●​ It comes between if and else and allows you to test several
conditions one by one.
●​ Only one block executes in an if–else if–else chain.​

●​ You can have any number of else if statements.​

●​ else is optional.​

Syntax:

if (condition1) {
// code block if condition1 is true
}
else if (condition2) {
// code block if condition2 is true
}
else if (condition3) {
// code block if condition3 is true
}
else {
// code block if all conditions are false
}
Flow:

1.​ condition1 is evaluated first.​

2.​ If condition1 is true, its block executes and the rest is skipped.​

3.​ If condition1 is false, it checks condition2, and so on.​

4.​ If none of the conditions are true, the else block executes (optional).

#include <stdio.h>

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

if (marks >= 90) {


printf("Grade: A\n");
}
else if (marks >= 75) {
printf("Grade: B\n");
}
else if (marks >= 50) {
printf("Grade: C\n");
}
else {
printf("Grade: F\n");
}

return 0;
}
Nested if statements

●​ A nested if statement is an if or if-else statement


placed inside another if or else block.
●​ It is used when you want to check multiple
conditions where some conditions depend on
others.

Syntax:

if (condition1) {

// code block for condition1

if (condition2) {

// code block for condition2

} else {

// code block if condition2 is false

} else {

// code block if condition1 is false


}

Flow:

1.​First, condition1 is evaluated.​

2.​If condition1 is true, it enters the first block and


checks condition2.​

3.​Depending on condition2, it executes the


corresponding inner block.​

4.​If condition1 is false, it goes to the else block of


the outer if.​

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

if (num > 0) {
printf("The number is positive.\n");
if (num % 2 == 0)
{
printf("It is also even.\n");
}
else
{
printf("It is odd.\n");
}
}
else if (num < 0) {
printf("The number is negative.\n");
}
else {
printf("The number is zero.\n");
}

return 0;
}

3. switch-case Statement
Explanation:

●​ The switch statement is a decision-making control structure.​


It allows you to choose among multiple options based on the value
of an expression.
●​ Think of it as a cleaner alternative to many if-else if statements.
●​ Used to select one block of code to be executed among many
options.

Syntax:​
switch(expression) {​
case value1:​
// code​
break;​
case value2:​
// code​
break;​
default:​
// code​
}

Example:​
int day = 2;​
switch(day) {​
case 1: printf("Monday"); break;​
case 2: printf("Tuesday"); break;​
default: printf("Invalid");​
}
Algorithm:​
1. Start​
2. Read day​
3. Match value with cases​
4. Execute matched case block​
5. End

Flowchart:​
Input:​
day = 2

Output:​
Tuesday
Example 1: Simple Calculator Using switch-case
#include <stdio.h>

int main() {
char operator = '+';
int a = 10, b = 5;

switch (operator) {
case '+':
printf("Result: %d\n", a + b);
break;
case '-':
printf("Result: %d\n", a - b);
break;
case '*':
printf("Result: %d\n", a * b);
break;
case '/':
if (b != 0)
printf("Result: %d\n", a / b);
else
printf("Division by zero is not allowed.\n");
break;
default:
printf("Invalid operator.\n");
}

return 0;
}

Example 3: Grade System


#include <stdio.h>

int main() {
char grade = 'B';

switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good job.\n");
break;
case 'C':
printf("You passed.\n");
break;
case 'D':
printf("You need to work harder.\n");
break;
case 'F':
printf("Fail.\n");
break;
default:
printf("Invalid grade.\n");
}

return 0;
}

Example 4: Menu-Driven Program


#include <stdio.h>

int main() {
int choice;

printf("Menu:\n");
printf("1. Start\n");
printf("2. Settings\n");
printf("3. Exit\n");
printf("Enter your choice: ");
scanf("%d", &choice);

switch (choice) {
case 1:
printf("Starting the program...\n");
break;
case 2:
printf("Opening settings...\n");
break;
case 3:
printf("Exiting...\n");
break;
default:
printf("Invalid choice.\n");
}

return 0;
}

H.W Example 5: Check Vowel or Consonant Using switch

Else If Vs Switch

Feature else if switch


Type of conditions Can use any Can only compare
relational or logical constant values
condition (integer, char, enum)
Number of branches Unlimited, checks Checks all cases for
conditions one by one equality
Execution style Stops at first true Jumps directly to
condition matching case (needs
break to stop
fall-through)
Use case Complex conditions Fixed discrete values
with ranges or of a variable
expressions
Performance May be slower for Faster for large
many conditions number of constant
cases
Default else is optional default is optional
======================================================

4. while Loop
Explanation:

●​ The while loop is an entry-controlled loop that repeatedly executes


a block of code as long as a condition is true.
●​ “Entry-controlled” means the condition is checked first, and if false
initially, the loop may never run.​

●​ Repeats a block of code as long as a specified condition is true.

Syntax:​
while (condition) {​
// code block​
}

Example:​
int i = 1;​
while (i <= 5) {​
printf("%d ", i);​
i++;​
}
Algorithm:​
1. Start​
2. Initialize i = 1​
3. Check if i <= 5​
4. Print i​
5. Increment i​
6. Repeat until condition false​
7. End

Flowchart:​
Input:​
Output:​
12345

1. Print numbers from 1 to 10


#include <stdio.h>

int main() {
int i = 1;
while (i <= 10) {
printf("%d\n", i);
i++;
}
return 0;
}

2. Sum of first 10 natural numbers


#include <stdio.h>

int main() {
int i = 1, sum = 0;
while (i <= 10) {
sum = sum + i;
i++;
}
printf("Sum = %d", sum);
return 0;
}

3. Reverse counting from 10 to 1


#include <stdio.h>

int main() {
int n = 10;
while (n >= 1) {
printf("%d\n", n);
n--;
}
return 0;
}

4. Factorial of a number
#include <stdio.h>

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

while (n > 0) {
fact = fact * n;
n--;
}
printf("Factorial = %d", fact);
return 0;
}

H.W 5. Print digits of a number

5. for Loop
Explanation:

●​ The for loop is an entry-controlled loop used to execute a block of


code a specific number of times.
●​ It’s most useful when the number of iterations is known
beforehand.​

●​ Used for repeating a block of code a specific number of times.

Syntax:​
for (initialization; condition; increment) {​
// code block​
}

Explanation of Components:

1.​ Initialization: Sets the starting value (executed once before the loop
starts).​

2.​ Condition: Checked before each iteration. Loop executes only if


true.​

3.​ Update: Changes the loop variable after each iteration (like
increment/decrement).

Example:​
for (int i = 1; i <= 5; i++) {​
printf("%d ", i);​
}

Algorithm:​
1. Start​
2. Loop i from 1 to 5​
3. Print i in each iteration​
4. End

Flowchart:​
Input:

Output:​
12345

1. Print numbers from 1 to 10


#include <stdio.h>
int main() {
for(int i = 1; i <= 10; i++) {
printf("%d\n", i);
}
return 0;
}

2. Sum of first 10 natural numbers


#include <stdio.h>
int main() {
int sum = 0;
for(int i = 1; i <= 10; i++) {
sum = sum + i;
}
printf("Sum = %d", sum);
return 0;
}

3. Print multiplication table of 5


#include <stdio.h>
int main() {
for(int i = 1; i 1<= 0; i++) {
printf("5 x %d = %d\n", i, 5*i);
}
return 0;
}

4. Factorial of a number
#include <stdio.h>
int main() {
int n, fact = 1;
printf("Enter a number: ");
scanf("%d", &n);

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


fact = fact * i;
}
printf("Factorial = %d", fact);
return 0;
}

5. Reverse counting from 10 to 1


#include <stdio.h>
int main() {
for(int i = 10; i >= 1; i--) {
printf("%d\n", i);
}
return 0;
}

H.W
1. Check if a number is Prime
2. Print a Star Triangle (Pattern)
3. Reverse a Number
4. Fibonacci Series
5. Count Digits of a Number

===================================================

6. do-while Loop
Explanation:

●​ The do-while loop is an exit-controlled loop that executes a block


of code at least once, and then repeats it as long as the condition is
true.
●​ “Exit-controlled” means the condition is checked after the loop
body, so the loop always runs at least once.
●​ Executes a block of code once, then repeats as long as the condition is
true.
Syntax:​
do {​
// code block​
} while (condition);

Example:​
int i = 1;​
do {​
printf("%d ", i);​
i++;​
} while (i > 5);

Algorithm:​
1. Start​
2. Initialize i = 1​
3. Print i​
4. Increment i​
5. Check condition​
6. Repeat if true​
7. End

Flowchart:​
Input:​
Output:​
12345

Example 1: Print numbers from 1 to 5


#include <stdio.h>
int main() {
int i = 1;
do {
printf("%d\n", i);
i++;
} while (i <= 5);
return 0;
}
Example 2: Sum of first 10 natural numbers
#include <stdio.h>
int main() {
int i = 1, sum = 0;

do {
sum += i;
i++;
} while (i <= 10);

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


return 0;
}

Example 3: Print even numbers up to 20


#include <stdio.h>
int main() {
int i = 2;

do {
printf("%d ", i);
i += 2;
} while (i <= 20);

return 0;
}

Example 4: Reverse count from 5 to 1


#include <stdio.h>

int main() {
int i = 5;

do {
printf("%d\n", i);
i--;
} while (i >= 1);

return 0;
}

Example 5: Take user input until they enter 0


#include <stdio.h>

int main() {
int num;

do {
printf("Enter a number (0 to stop): ");
scanf("%d", &num);
} while (num != 0);

printf("Loop ended.\n");
return 0;
}

======================================================

Loop Control Statements

●​ Loop control statements are used to alter the normal flow of


loops (for, while, do-while).​

●​ They allow you to exit a loop early, skip an iteration, or jump


to another part of the code.
1.Break
Explanation:

●​ The break statement is used to exit a loop prematurely.


●​ When encountered inside a loop (for, while, or do-while), it causes the
loop to terminate immediately.
●​ It is often used when a specific condition is met that makes continuing
the loop unnecessary.

Syntax:​
break;

Example:​
int i;​
for (i = 1; i <= 10; i++) {​
if (i == 5)​
break;​
printf("%d ", i);​
}

Algorithm:​
1. Start​
2. Loop i from 1 to 10​
3. If i == 5, break​
4. Else, print i​
5. End

Flowchart:​
Input:​
Output:​
1234

===================================================
2.Continue
Explanation:​
The continue statement skips the current iteration of a loop and continues
with the next one.

●​ When encountered, it causes the remaining code inside the loop for
the current iteration to be skipped.
●​ It is useful for skipping specific cases within a loop without exiting it
entirely.

Syntax:​
continue;

Example:​
int i;​
for (i = 1; i <= 10; i++) {​
if (i == 3)​
continue;​
printf("%d ", i);​
}

Algorithm:​
1. Start​
2. Loop i from 1 to 5​
3. If i == 3, continue​
4. Else, print i​
5. End

Flowchart:​
Input:​
Output:​
1245

===================================================

Nested loops
Explanation:​
A nested loop is a loop inside another loop.
●​ The inner loop is executed completely every time the outer loop runs
one iteration.
●​ It is commonly used for working with multidimensional data
structures like matrices or for pattern generation.

Syntax:​
for (int i = 1; i <= n; i++) {​
for (int j = 1; j <= m; j++) {​
// inner loop block​
}​
}

Example:​
int i, j;​
for (i = 1; i <= 2; i++) {​
for (j = 1; j <= 3; j++) {​
printf("%d %d\n", i, j);​
}​
}

Algorithm:​
1. Start​
2. Outer loop i from 1 to 2​
3. Inner loop j from 1 to 3​
4. Print i and j​
5. End

Flowchart:​
Input:​
Output:​
1 1​
1 2​
1 3​
2 1​
2 2​
23

Example 1: Print a 3x3 grid of numbers


#include <stdio.h>
int main() {
int i, j;

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


for (j = 1; j <= 3; j++) {
printf("%d ", j);
}
printf("\n"); // move to next line after
inner loop
}

return 0;
}

👉 Output:
1 2 3
1 2 3
1 2 3

Example 2: Print a multiplication table


#include <stdio.h>
int main() {
int i, j;
for (i = 1; i <= 5; i++) {
for (j = 1; j <= 10; j++) {
printf("%d\t", i * j);
}
printf("\n");
}

return 0;
}

👉 Output (Partial):
1​ 2​ 3​ 4​ 5​ 6​ 7​ 8​ 9​ 10
2​ 4​ 6​ 8​ 10​ 12​ 14​ 16​ 18​ 20
3​ 6​ 9​ 12​ 15​ 18​ 21​ 24​ 27​ 30
...

Example 3: Print a star pattern (triangle)


#include <stdio.h>

int main() {
int i, j;

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


for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}
👉 Output:
*
* *
* * *
* * * *
* * * * *

Example 4: Print reverse star pattern


#include <stdio.h>

int main() {
int i, j;

for (i = 5; i >= 1; i--) {


for (j = 1; j <= i; j++) {
printf("* ");
}
printf("\n");
}

return 0;
}

👉 Output:
* * * * *
* * * *
* * *
* *
*
===================================================

You might also like