100% satisfaction guarantee Immediately available after payment Both online and in PDF No strings attached 4.2 TrustPilot
logo-home
Other

Control Structures in C: If-Else, Switch, and Loops Explained with Examples

Rating
-
Sold
-
Pages
7
Uploaded on
20-01-2025
Written in
2024/2025

This document provides a comprehensive guide to control structures in C programming, including if-else statements, switch-case, and loops (for, while, do-while). It explains how to use these control structures to manage program flow and make decisions. Perfect for first-year and second-year Computer Science students, this guide includes simple examples to help you understand the logic behind control flow and decision-making in C.

Show more Read less
Institution
Module









Whoops! We can’t load your doc right now. Try again or contact support.

Written for

Institution
Module

Document information

Uploaded on
January 20, 2025
Number of pages
7
Written in
2024/2025
Type
Other
Person
Unknown

Subjects

Content preview

Control Structures in C
Control structures in C are used to control the flow of execution in a program.
They allow you to make decisions, repeat actions, and control the sequence of
operations. The main types of control structures in C are decision-making, loops,
and branching.

1. Decision-Making Statements
Decision-making statements are used to make decisions in your program, allowing
it to choose different paths based on conditions.

1.1 if Statement

The if statement allows you to execute a block of code only if a specific condition
is true.

#include <stdio.h>

int main() {
int num = 10;

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

return 0;
}

In the example above, if the condition num > 0 is true, the code inside the if block
will execute.

1.2 if-else Statement

The if-else statement allows you to execute one block of code if the condition is
true and another block if the condition is false.

, #include <stdio.h>

int main() {
int num = -5;

if (num > 0) {
printf("Number is positive.\n");
} else {
printf("Number is negative or zero.\n");
}

return 0;
}

1.3 if-else if Ladder

The if-else if ladder allows you to test multiple conditions and execute different
blocks of code based on which condition is true.

#include <stdio.h>

int main() {
int num = 0;

if (num > 0) {
printf("Number is positive.\n");
} else if (num < 0) {
printf("Number is negative.\n");
} else {
printf("Number is zero.\n");
}

return 0;
}
£4.24
Get access to the full document:

100% satisfaction guarantee
Immediately available after payment
Both online and in PDF
No strings attached

Get to know the seller
Seller avatar
rileyclover179

Also available in package deal

Get to know the seller

Seller avatar
rileyclover179 US
Follow You need to be logged in order to follow users or courses
Sold
0
Member since
10 months
Number of followers
0
Documents
252
Last sold
-

0.0

0 reviews

5
0
4
0
3
0
2
0
1
0

Recently viewed by you

Why students choose Stuvia

Created by fellow students, verified by reviews

Quality you can trust: written by students who passed their exams and reviewed by others who've used these revision notes.

Didn't get what you expected? Choose another document

No problem! You can straightaway pick a different document that better suits what you're after.

Pay as you like, start learning straight away

No subscription, no commitments. Pay the way you're used to via credit card and download your PDF document instantly.

Student with book image

“Bought, downloaded, and smashed it. It really can be that simple.”

Alisha Student

Frequently asked questions