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

Control Structures in C

The document provides an overview of control structures in C programming, detailing control statements that dictate program flow through decision-making, looping, and jumping. It covers types of control statements including if, else, switch, for, while, do-while, break, continue, and goto, along with examples for each. These structures are essential for creating dynamic and efficient programs that can solve real-world problems.

Uploaded by

Rani Koshy
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 views27 pages

Control Structures in C

The document provides an overview of control structures in C programming, detailing control statements that dictate program flow through decision-making, looping, and jumping. It covers types of control statements including if, else, switch, for, while, do-while, break, continue, and goto, along with examples for each. These structures are essential for creating dynamic and efficient programs that can solve real-world problems.

Uploaded by

Rani Koshy
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 statements in C language are instructions that


determine the flow of a program's execution based
on certain conditions or repetitions. They help decide
whether a block of code should run, repeat, or skip. These
statements make programs dynamic by adding decision-making,
looping, and jumping capabilities, which are essential for solving
real-world problems.
• There are three types of control statements in C programming:
1. Decision-Making Statements: These statements allow the
program to make decisions and execute specific blocks of code
based on conditions.
2. Loop Control Statements: These statements repeatedly
execute a block of code as long as a specified condition is true.
3. Jump Statements: These statements transfer the program's
control from one part of the code to another, either conditionally
or unconditionally.
Decision Making
If Statement
#include <stdio.h> Step 1: The condition temperature > 25 is
int main() { evaluated.
int temperature = 30;
Step 2: Since 30 > 25 is true, the program
if (temperature > 25) { enters the if block and prints: It's a hot day!.
printf("It's a hot
day!\n"); Step 3: After executing the if block, the
} program continues to the next statement and
prints: Have a good day!.
printf("Have a good day!");
return 0; If the condition were false, the program would
} directly print: Have a good day!.
if (condition) {
// Code to execute if the condition is
true
}

How If Condition in C Works?


1. Condition Evaluation:
•The condition inside the parentheses () is evaluated first.
•It can be any logical or relational expression, such as a > b or x == y.
The result of the evaluation is either true (non-zero) or false (zero).

2. Decision:
•If the condition evaluates to true, the program executes the code
block inside the curly braces {}.
•If the condition evaluates to false, the program skips the code block
and continues with the next statement after the if.

3. Execution Flow:
•The program does not check or execute anything inside the if block if
the condition is false.
•Only one branch of code executes, ensuring controlled and logical
flow.
If – else Statement
Components:

if Keyword: Indicates the start of the


decision-making structure.
if (condition) { Condition: A logical or relational
// Code to execute if the condition expression placed inside parentheses ()
is true that evaluates to either true (non-zero) or
} else { false (zero).
// Code to execute if the condition
is false If Block: The code block inside the curly
} braces {} following the if keyword, which
executes if the condition is true.

else Keyword: Provides an alternative


code block to execute when the condition
is false.

Else Block: The code block inside the


curly braces {} following the else keyword,
which executes if the condition is false.
If – else Statement
#include <stdio.h>
int main() {
int number;
printf("Enter a number: ");
scanf("%d", &number);

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

return 0;
}
Nested If
#include <stdio.h>
int main() {
int marks;
printf("Enter your marks: ");
scanf("%d", &marks);

if (marks >= 50) { // Outer condition


if (marks >= 90) { // Inner condition
printf("Grade: A\n");
} else {
printf("Grade: B\n");
}
} else {
printf("Grade: F\n");
}

return 0;
}
• switch case Statement
– The switch case in C tests a variable against multiple values (cases) and
executes the matching block of code.

switch (expression) {
case constant1:
// Code to execute if expression
matches constant1
break;
case constant2:
// Code to execute if expression
matches constant2
break;
...
default:
// Code to execute if no case
matches
}

– Use switch when you need to test a variable against a fixed set of
values.
How Switch Case in C Works?
Here is how the switch statement in C language works step by step:
1. Evaluate the Expression
The expression inside the switch statement is evaluated first.
It must result in an integer or character value.
2. Compare with Case Constants
The program compares the value of the expression with each case constant in the
switch block.
If a match is found, the program executes the code under that case.
3. Execute Matching Case
When a match is found, the code under the corresponding case is executed until a
break statement is encountered or the end of the switch block is reached.
4. Break Statement
The break statement in C ends the execution of the current case and exits the
switch block.
Without break, the program continues to execute subsequent cases (fall-through).
5. Default Case
If no case matches the expression, the program executes the optional default block.
If the default block is not present, the program skips the switch block entirely.
6. Continue Program Execution
After executing the matching case or the default case, the program moves to the
next statement outside the switch block.
#include <stdio.h>
int main() {
int day;
printf("Enter a number (1-7): "); case 5:
scanf("%d", &day); printf("Friday\n");
break;
switch (day) { case 6:
case 1: printf("Saturday\n");
printf("Monday\n"); break;
break; case 7:
case 2: printf("Sunday\n");
printf("Tuesday\n"); break;
break; default:
case 3: printf("Invalid day\n");
printf("Wednesday\n"); }
break;
case 4: return 0;
printf("Thursday\n"); }
break;
Input: 3 → Output: Wednesday

Input: 8 → Output: Invalid day


Loop Control Statements in C
• Loop control statements in C are used to repeatedly execute a
block of code as long as a specific condition is true.
1. for Loop
• The for loop in C executes a block of code a specific number of
times, controlled by an initialization, condition, and
increment/decrement.

for (initialization; condition;


increment/decrement) {
// Code to execute in each iteration
}

• When to Use: Use the for loop when the number of


iterations is known beforehand.
for (initialization; condition; increment/decrement) {

// Code to execute in each iteration

Components of the for loop syntax:

1. Initialization:
This step initializes the loop control variable (e.g., int i = 0). It runs only once, at the
start of the loop.

2. Condition:
The condition is a logical expression that determines whether the loop should
continue. If the condition is true, the loop executes; if false, it terminates.

3. Increment/Decrement:
It updates the loop control variable after each iteration. Also, it ensures the loop
progresses towards termination.

4. Loop Body:
It contains the statements to execute repeatedly. The loop body runs every time the
condition evaluates to true.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d ", i);
}
return 0;
}
Output
12345

#include <stdio.h> Output


int main() { 5x1=5
for (int i = 1; i <= 10; i++) { 5 x 2 = 10
printf("5 x %d = %d\n", i, 5 * 5 x 3 = 15
i); 5 x 4 = 20
} 5 x 5 = 25
return 0; 5 x 6 = 30
} 5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50
#include <stdio.h>
int main() {
int num = 5, factorial = 1;
for (int i = 1; i <= num; i++) { Output
factorial *= i; Factorial: 120
}
printf("Factorial: %d", factorial);
return 0;
}

#include <stdio.h>
int main() {
for (;;) { //infinite loop
i = i+1;
printf(“i=%d.\n“,i);
}
return 0; // This line is never reached
}
• while Loop
• The while loop in C executes a block of code
repeatedly as long as the specified condition is true.
#include <stdio.h>

while (condition) { int main() {


// Code to execute
int i = 1;
while the condition is true
} while (i <= 5) {
printf("%d ", i);
i++;
}
return 0;
}
Output
12345

• When to Use: Use the while loop when the number of


iterations is not known and depends on a condition.
while (condition) {
// Code to execute while the
condition is true
}

Components of While Loop Syntax:

• Condition: It is a logical expression that determines whether the loop


will execute. The loop runs as long as the condition evaluates to true.

• Loop Body: It contains the block of code to be executed repeatedly


while the condition is true. This code runs every iteration of the loop.

• Control Variable (optional but commonly used): It is a variable


used to control the loop, typically initialized before the loop starts. It is
updated inside the loop body to ensure the loop eventually terminates.
• do-while Loop
• The do-while loop in C executes a block of code at least once
and then continues to execute as long as the condition is true.

do {
// Code to execute
} while (condition);

• When to Use: Use the do-while loop when you want the code
to execute at least once, regardless of the condition.
do {
// Code to execute
} while (condition);

Components of Do While Loop

•do: Itindicates the start of the loop body. It ensures the code inside the block
executes at least once before the condition is evaluated.

•Loop Body: It contains the statements to execute repeatedly. It executes at


least once, regardless of the condition.

•Condition: It is a logical expression evaluated after each execution of the


loop body. If true, the loop repeats; if false, the loop terminates.

•Semicolon (;): A semicolon must follow the while statement to mark the end
of the loop syntax.
Print Numbers from 1 to 5

#include <stdio.h>
int main() {
int i = 1;
do { Reverse Print from 5 to 1

printf("%d ", i); #include <stdio.h>


i++; int main() {
} while (i <= 5); int i = 5;
return 0; do {
} printf("%d ", i);
i--;
Output
12345
} while (i > 0);
return 0;
} Output:
54321
For at the beginning of each Known no. of iterations
iteration of the loop
While at the beginning of each Unknown no. of iterations
iteration of the loop
Do-while After the first iteration Code must run atleast
once
Jumping Control Statements in C
• Jumping statements in C are used to transfer control of the program from one
point to another. These statements enable conditional or unconditional jumps,
allowing flexibility in program flow.

1. break Statement
• The break statement in C terminates the nearest enclosing loop or switch
statement and transfers control to the next statement after loop.
break;
Components of the Break Statement

Keyword (break): The break keyword is a predefined statement in C. It tells the


program to immediately exit the current loop or switch block.

Semicolon (;): The semicolon is required after the break keyword to terminate the
statement.

Execution Context: The break statement is valid only inside: Loops in C (for,
while, do while) and switch statement in C. It cannot be used outside these
constructs.
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 5; i++) {
if (i == 3) {
break; // Exit the loop when
i is 3
}
printf("%d\n", i);
}
return 0;
}

Output
1
2
2. continue Statement
• The continue statement in C skips the current iteration of the loop and moves to the
next iteration.
• It is used to bypass specific conditions or scenarios within loops without stopping the
entire loop. This ensures that the loop continues to execute for other iterations.

continue;
Components of Continue Statement Syntax:

•Keyword (continue): The continue keyword is used to instruct the program to


skip the rest of the code in the current iteration and move to the next iteration of
the loop.

•Semicolon (;): A semicolon must follow the continue keyword to terminate the
statement.

•Execution Context: The continue statement is valid only within loops (for,
while, do while). It cannot be used outside of loops.
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue;
}
printf("Number: %d\n", i);
}
return 0;
}
Output
1 Explanation:
2 When i == 3, the continue statement skips
4 printing and directly moves to the next
5 iteration.
• goto Statement
• The goto statement in C transfers control to a labeled
statement within the program.

Explanation of Components:
goto label;
• goto: This keyword is used to instruct the
... program to jump to the specified label.
label:
• label: A user-defined identifier marking
// Code to the point to which control should jump. It
execute must end with a colon (:).
#include <stdio.h>
int main() {
int number = 3;
if (number < 5) {
goto small;
}
printf("Number is not small.\n");
return 0;

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


}

Output
Number is small.

You might also like