Itp Unit-2
Itp Unit-2
SYLLABUS
1) Control Structures
web-D Page 1
SEM :- 1-1 (R23) introduction to programming UNIT-2
CONTROL STRUCTURES
In sequential logic, the program executes instructions in the order they appear.
It follows a linear path without any branching or repetition.
Example:
#include <stdio.h>
int main() {
printf("Hello, World!\n");
printf("Welcome to C programming.\n");
web-D Page 2
SEM :- 1-1 (R23) introduction to programming UNIT-2
return 0;
}
a. if Statement:
#include <stdio.h>
int main() {
int num = 10;
if (num > 5) {
printf("Number is greater than 5.\n");
}
return 0;
}
b. if-else Statement:
Executes one block of code if the condition is true and another block if it’s false.
Example:
#include <stdio.h>
int main() {
int num = 7;
if (num % 2 == 0) {
printf("Number is even.\n");
} else {
printf("Number is odd.\n");
}
return 0;
web-D Page 3
SEM :- 1-1 (R23) introduction to programming UNIT-2
c. switch Statement:
#include <stdio.h>
int main() {
char grade = 'B';
switch (grade) {
case 'A':
printf("Excellent!\n");
break;
case 'B':
printf("Good!\n");
break;
default:
printf("Needs improvement.\n");
}
return 0;
}
a. while Loop:
#include <stdio.h>
int main() {
int i = 1;
web-D Page 4
SEM :- 1-1 (R23) introduction to programming UNIT-2
while (i <= 5) {
printf("%d\n", i);
i++;
}
return 0;
}
b. for Loop:
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
printf("%d\n", i);
}
return 0;
}
while Loop:
#include <stdio.h>
web-D Page 5
SEM :- 1-1 (R23) introduction to programming UNIT-2
int main() {
int i = 0;
while (i < 5) {
printf("VINAY PREM\n");
i++;
}
return 0;
}
Output:
VINAY PREM
VINAY PREM
VINAY PREM
VINAY PREM
VINAY PREM
2. do…while Loop:
do {
// Code to be executed
} while (condition);
Example:
#include <stdio.h>
int main() {
web-D Page 6
SEM :- 1-1 (R23) introduction to programming UNIT-2
int i = 0;
do {
printf("VINAY PREM\n");
i++;
} while (i < 3);
return 0;
}
Output:
VINAY PREM
VINAY PREM
VINAY PREM
The while loop checks the condition before executing the loop body, while the
do...while loop executes the body at least once and then checks the condition. Choose
CONTROL STATEMENTS
1. break Statement:
The break statement is used to terminate the execution of a loop (such as for,
while, or do...while) prematurely.
It is commonly used within loops to exit the loop when a specific condition is met.
Example (using break in a while loop):
#include <stdio.h>
int main() {
int i = 1;
web-D Page 7
SEM :- 1-1 (R23) introduction to programming UNIT-2
Output:
1
2
3
4
2. continue Statement:
The continue statement is used to skip the current iteration of a loop and move to
the next iteration.
It allows you to bypass specific iterations based on a condition.
Example (using continue in a for loop):
#include <stdio.h>
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip iteration when i equals 3
}
printf("%d\n", i);
}
return 0;
web-D Page 8
SEM :- 1-1 (R23) introduction to programming UNIT-2
Output:
1
2
4
5
Both break and continue are powerful tools for controlling the flow of execution within
loops. Use them wisely based on your specific requirements!
web-D Page 9