CTU 07103 Programming App & Info Sec - Session 3 - Control Flow
CTU 07103 Programming App & Info Sec - Session 3 - Control Flow
Control Flow
int main() {
int num1, num2, temp;
return 0;
}
Write a program to print the sum of the first n natural numbers using a for loop.
#include <stdio.h>
int main() {
int n, sum = 0;
printf("Enter the value of n: ");
scanf("%d", &n);
for (int i = 1; i <= n; i++) {
sum += i;
}
printf("Sum of first %d natural numbers: %d\n", n, sum);
return 0;
}
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);
printf("Factorial of %d is %d\n", num, factorial(num));
return 0;
}
int main() {
while (1) {
printf("This is an infinite loop.\n");
}
return 0;
}
int main() {
int i = 5;
// while loop
while (i < 5) {
printf("This will not print because the condition is false.\n");
i++;
}
i = 5;
// do-while loop
do {
printf("This will print at least once.\n");
i++;
} while (i < 5);
return 0;
}
int main() {
int i, j;
return 0;
}
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
break; // Breaks the loop when i equals 3
}
printf("%d ", i);
}
printf("\n");
int main() {
return 0;
}
int main() {
int target = 4;
return 0;
}
int main() {
for (int i = 1; i <= 5; i++) {
if (i == 3) {
continue; // Skip the iteration when i is 3
}
printf("%d ", i);
}
return 0;
}
int main() {
int number;
return 0;
}
int main() {
int number;
return 0;
}
int main() {
int number;
return 0;
}
int main() {
// Nested for loop to print a 3x3 grid of numbers
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d ", i * j); // Printing product of i and j
}
printf("\n");
}
return 0;
}
P a g e | 10 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
• Explanation: The if-else construct can be used to check if a number is even or odd by dividing
the number by 2. If the remainder is 0, the number is even; otherwise, it is odd. The % (modulus)
operator is used to determine the remainder.
Example:
#include <stdio.h>
int main() {
int number;
return 0;
}
int main() {
int number = 2;
switch (number) {
case 1:
printf("One\n");
case 2:
printf("Two\n");
case 3:
printf("Three\n");
default:
printf("Invalid number\n");
}
return 0;
P a g e | 11 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
}
int main() {
int i = 10;
return 0;
}
int main() {
int number = 5;
switch (number) {
case 1:
printf("One\n");
break;
case 2:
printf("Two\n");
break;
default:
printf("Invalid number\n");
}
return 0;
}
P a g e | 12 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
int main() {
// Nested for loop to print a 3x3 grid of numbers
for (int i = 1; i <= 3; i++) {
for (int j = 1; j <= 3; j++) {
printf("%d ", i * j); // Printing product of i and j
}
printf("\n"); // Move to the next line after each row
}
return 0;
}
int main() {
int score;
P a g e | 13 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
if (score >= 90) {
printf("Grade: A\n");
} else if (score >= 80) {
printf("Grade: B\n");
} else if (score >= 70) {
printf("Grade: C\n");
} else if (score >= 60) {
printf("Grade: D\n");
} else {
printf("Grade: F\n");
}
return 0;
}
int main() {
char department;
int experience;
if (department == 'A') {
printf("Enter years of experience: ");
scanf("%d", &experience);
if (experience >= 5) {
printf("You are a Senior in Department A.\n");
} else {
printf("You are a Junior in Department A.\n");
}
} else if (department == 'B') {
printf("Enter years of experience: ");
scanf("%d", &experience);
if (experience >= 3) {
P a g e | 14 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
printf("You are a Senior in Department B.\n");
} else {
printf("You are a Junior in Department B.\n");
}
} else {
printf("Invalid department.\n");
}
return 0;
}
int main() {
int counter = 0;
return 0;
}
int main() {
int counter = 0;
do {
printf("This is iteration %d of the do-while loop.\n", counter + 1);
counter++;
} while (counter < 3);
P a g e | 15 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
return 0;
}
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
break; // Exit the loop when i equals 5
}
printf("%d ", i);
}
return 0;
}
2. continue Statement:
The continue statement skips the current iteration of the loop and moves to the next iteration. It is
useful when specific conditions require skipping parts of the loop body.
Example of continue:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
return 0;
}
P a g e | 16 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
The switch construct in C is a decision-making control statement that allows a variable to be tested for
equality against a list of constants. It simplifies code by replacing multiple if-else if statements when
testing a single variable for multiple possible values.
Syntax of switch:
switch (expression) {
case constant1:
// Code block
break;
case constant2:
// Code block
break;
...
default:
// Default code block
}
Example Code:
#include <stdio.h>
int main() {
int day;
switch (day) {
case 1:
printf("Monday\n");
break;
case 2:
printf("Tuesday\n");
break;
case 3:
printf("Wednesday\n");
break;
case 4:
printf("Thursday\n");
break;
case 5:
printf("Friday\n");
break;
case 6:
printf("Saturday\n");
break;
case 7:
printf("Sunday\n");
break;
default:
P a g e | 17 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
printf("Invalid input!\n");
}
return 0;
}
do-while loop used to ensure user input validation until correct input
is provided
The do-while loop is ideal for input validation because it ensures that the user is prompted at least once
and continues to prompt until valid input is provided.
Example Code:
#include <stdio.h>
int main() {
int number;
do {
printf("Enter a number between 1 and 10: ");
scanf("%d", &number);
return 0;
}
for loop in C
The for loop in C is a control flow statement that is used for iterating over a sequence of values. It is
particularly useful when the number of iterations is known beforehand.
Syntax of for Loop:
for (initialization; condition; increment/decrement) {
// Code block to be executed
P a g e | 18 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
}
Components:
1. Initialization: A variable is initialized before the loop starts. This is executed only once.
2. Condition: The loop runs as long as this condition is true. It is checked before every iteration.
3. Increment/Decrement: Updates the loop variable after each iteration.
Example Code:
#include <stdio.h>
int main() {
int i;
return 0;
}
int main() {
int grade;
switch (grade) {
case 1:
case 2:
printf("Poor performance.\n");
break;
case 3:
printf("Average performance.\n");
break;
case 4:
case 5:
printf("Excellent performance.\n");
break;
default:
P a g e | 19 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
printf("Invalid grade entered.\n");
}
return 0;
}
int main() {
int i, j;
return 0;
}
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i == 5) {
P a g e | 20 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
break; // Exit the loop when i equals 5
}
printf("%d ", i);
}
return 0;
}
2. continue Statement:
The continue statement skips the remaining part of the current iteration and moves to the next iteration.
Example of continue:
#include <stdio.h>
int main() {
int i;
for (i = 1; i <= 10; i++) {
if (i % 2 == 0) {
continue; // Skip even numbers
}
printf("%d ", i);
}
return 0;
}
infinite loop in C
An infinite loop is a loop that runs indefinitely because its terminating condition is never met. This can be
achieved using:
1. A while loop with a condition that always evaluates to true.
2. A for loop with no condition.
1. Using a while Loop:
#include <stdio.h>
int main() {
while (1) { // Condition is always true
printf("This is an infinite loop.\n");
}
return 0;
}
int main() {
for (;;) { // No initialization, condition, or increment
P a g e | 21 RAJIV KUMAR
CTU 07103 Programming App & Info Sec
Control Flow
printf("This is an infinite loop.\n");
}
return 0;
}
int main() {
int input;
while (1) {
printf("Enter a number (0 to exit): ");
scanf("%d", &input);
if (input == 0) {
break; // Exit loop when input is 0
}
printf("You entered: %d\n", input);
}
return 0;
}
P a g e | 22 RAJIV KUMAR