Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
14 views

C Programming Control Statements

The document provides an overview of control statements in C programming, which manage the flow of execution through conditional, looping, and branching statements. It details various types of conditional statements such as if, if-else, nested if, and switch, as well as looping constructs like while, do-while, and for loops. Additionally, it covers branching statements like break and continue that alter the normal flow of execution.

Uploaded by

techstack901
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)
14 views

C Programming Control Statements

The document provides an overview of control statements in C programming, which manage the flow of execution through conditional, looping, and branching statements. It details various types of conditional statements such as if, if-else, nested if, and switch, as well as looping constructs like while, do-while, and for loops. Additionally, it covers branching statements like break and continue that alter the normal flow of execution.

Uploaded by

techstack901
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/ 17

Lec - 2

C Programming
Control Statements
BY Aniruddha Manmode
Index
- What is Control Statements
- Conditional Control Statements
- If statement
- If-else statement
- If-else-if statement
- Nested If statement
- Switch statement
- Looping Control Statements
- while loop
- do while loop
- for loop
- Branching (Jump) Control Statements
- break statement
- continue statement
2
What is Control Statements ?

Control statements in C are used to manage the flow of execution of a program. They
allow you to make decisions, repeat actions, and jump to different parts of your code
based on certain conditions.

There are three main types of control statements:

1. Conditional Control Statements

These statements allow you to execute a block of code only when certain conditions are met.

Examples:

- if statement
- if-else statement
- if-else-if statement
- Nested If
- Switch-case
If statement

These statements execute specific blocks of code based on a condition. The condition is a logical
expression that evaluates to either True or False. Based on this evaluation, the program decides which
code block to execute.

Example -

#include <stdio.h>

int main()
{
int x = 10;

if (x > 5)
{
printf("x is greater than 5\n");
}

return 0;
}
If-else statement

Executes one block of code if the condition is True, and another block if it is False.

Example -

#include <stdio.h>

int main() {
int x = 3;

if (x > 5) {
printf("x is greater than 5\n");
} else {
printf("x is less than or equal to 5\n");
}

return 0;
}
If-else-if statement
Example -

#include <stdio.h>
int main() {
int num = 10;
// Outer if statement
if (num > 0) {
printf("The number is positive.\n");
// Nested if statement
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
} else {
printf("The number is not positive.\n");
}
return 0;
}
Nested If statement
Example -

#include <stdio.h>
int main() {
int num = 10;
// Outer if statement
if (num > 0) {
printf("The number is positive.\n");
// Nested if statement
if (num % 2 == 0) {
printf("The number is even.\n");
} else {
printf("The number is odd.\n");
}
} else {
printf("The number is not positive.\n");
}
return 0;
}
Switch Statement
Example -

#include <stdio.h>
A switch statement is a
int main() {
control structure in programming int num;
that evaluates an expression and
// Get a number from the user
executes one of several possible printf("Enter a number (1-3): ");
blocks of code based on the value scanf("%d", &num);
of the expression. // Switch statement to check the number
switch (num) {
case 1:
It is often used to replace printf("You entered One.\n");
multiple if-else-if conditions. break;
case 2:
printf("You entered Two.\n");
Each possible value of the break;
expression is defined by a case, case 3:
printf("You entered Three.\n");
and the default block executes if break;
none of the cases match. default:
printf("Invalid number!\n");
break;
}
return 0;
}
2. Looping Control Statements
Looping control statements allow you to repeat a block of code multiple times.

Loops are control structures that allow you to execute a block of code repeatedly as long as a specified
condition is true.

Types:

- while loop
- do-while loop
- for loop
while loop

Example -
do-while loop

Example -
for loop

Example -
Nested for loop

Example -
3. Branching (Jump) Control Statements
These allow you to change the normal flow of execution by jumping out of loops or skipping certain
code of block.

Examples:

- break (exits the loop or switch-case)


- continue (skips the current block of code)
break statement
Example -
Continue statement

Example -

#include <stdio.h>

int main() {
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue; // Skip the rest of the loop when i is 5
}
printf("%d\n", i);
}
return 0;
}

You might also like