Loops, Qualifiers and Modifiers in C Language
Loops, Qualifiers and Modifiers in C Language
UNDERSTANDING
LOOPS, QUALIFIERS AND MODIFIERS IN C LANGUAGE
Types of Loops in C
1. for Loop
The for loop is an entry-controlled loop, meaning the condition is checked before execution.
Syntax:
// Code to be executed
#include <stdio.h>
int main()
{
for(int i = 1; i <= 5; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:
2. while Loop
The while loop is an entry-controlled loop that executes as long as the condition is true.
Syntax:
while(condition)
// Code to be executed
#include <stdio.h>
int main()
{
int i = 1;
while(i <= 5)
{
printf("%d\n", i);
i++;
}
return 0;
}
Output:
5
3. do-while Loop
The do-while loop is an exit-controlled loop, meaning the loop body executes at least once, even if
the condition is false.
Syntax:
do
// Code to be executed
} while(condition);
#include <stdio.h>
int main()
{
int i = 1;
do
{
printf("%d\n", i);
i++;
} while(i <= 5);
return 0;
}
Output:
Loop Variations
1. Infinite Loops
o Example:
while(1)
}
2. Nested Loops
o Example:
o Output:
i = 1, j = 1
i = 1, j = 2
i = 2, j = 1
i = 2, j = 2
i = 3, j = 1
i = 3, j = 2
Jumping Statements:
Jumping statements in C are used to transfer control from one part of the program to another. These
include:
1. break
2. continue
3. goto
4. return
1. break Statement
Syntax:
break;
1234
Explanation:
• The loop runs from 1 to 10, but when i == 5, the break statement terminates the loop.
Code:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 6)
{
break; // Exit loop when i == 6
}
printf("%d ", i);
}
return 0;
}
Output:
12345
Explanation:
• The loop runs from 1 to 10, but when i == 6, break terminates the loop.
Code:
#include <stdio.h>
int main()
{
int arr[] = {1, 3, 5, 7, 8, 9, 11};
int size = sizeof(arr) / sizeof(arr[0]);
for (int i = 0; i < size; i++)
{
if (arr[i] % 2 == 0)
{
printf("First even number: %d\n", arr[i]);
break; // Stop after finding the first even number
}
}
return 0;
}
Output:
Code:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 5; i++)
{
for (int j = 1; j <= i; j++)
{
printf("* ");
if (j == 3)
break; // Stop printing stars when 3rd column is reached
}
printf("\n");
}
return 0;
}
Output:
**
***
***
***
2. continue Statement
The continue statement is used to skip the current iteration and move to the next iteration of the
loop.
Syntax:
continue;
1 2 3 4 6 7 8 9 10
Explanation:
• When i == 5, the continue statement skips printing 5 and continues with the next iteration.
Code:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i % 3 == 0)
{
continue; // Skip multiples of 3
}
printf("%d ", i);
}
return 0;
}
Output:
1 2 4 5 7 8 10
Code:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i == 5)
{
continue; // Skip number 5
}
printf("%d ", i);
}
return 0;
}
Output:
1 2 3 4 6 7 8 9 10
Code:
#include <stdio.h>
int main()
{
for (int i = 1; i <= 10; i++)
{
if (i % 2 == 0)
{
continue; // Skip even numbers
}
printf("%d ", i);
}
return 0;
}
Output:
13579
3. Goto Statement
The goto statement allows jumping to a labeled statement anywhere in the program. It is generally
discouraged as it makes code hard to debug, but sometimes useful for breaking deeply nested loops.
Syntax:
goto label;
...
label:
// Code to execute
#include <stdio.h>
int main()
{
int i = 1;
while(i <= 10)
{
printf("%d ", i);
if(i == 5)
{
goto end; // Jump to the "end" label
}
i++;
}
end: // Label
printf("\nLoop terminated early using goto.\n");
return 0;
}
Output:
12345
Explanation:
• The loop runs normally but exits early when i == 5 using goto.
#include <stdio.h>
int main()
{
int num;
retry: // Label for re-entering the input step
printf("Enter a positive number: ");
scanf("%d", &num);
if(num < 0)
{
printf("Invalid input! Try again.\n");
goto retry; // Jump to "retry" label
}
You entered: 5
Explanation:
• If the user enters a negative number, the program jumps back to the retry label and asks for
input again.
Code:
#include <stdio.h>
int main()
{
printf("Before goto\n");
goto jump;
printf("This won't be printed\n");
jump:
printf("After goto\n");
return 0;
}
Output:
Before goto
After goto
Code:
#include <stdio.h>
int main()
{
int num;
retry:
printf("Enter a positive number: ");
scanf("%d", &num);
if (num < 0)
{
printf("Invalid! Try again.\n");
goto retry;
}
printf("You entered: %d\n", num);
return 0;
}
Code:
#include <stdio.h>
int main()
{
int i = 1;
loop:
if (i > 5) return 0;
printf("%d ", i);
i++;
goto loop;
}
Output:
12345
4. return Statement
The return statement exits the function and optionally returns a value.
#include <stdio.h>
void greet()
{
printf("Hello, ");
return; // Exits the function early
printf("World!"); // This line is never executed
}
int main()
{
greet();
return 0;
}
Output:
Hello,
Explanation: The return statement exits the function early, skipping the second printf.
Pattern Programs
1. Right-Angled Triangle
#include <stdio.h>
int main()
int n = 5;
printf("* ");
}
printf("\n");
return 0;
Output:
**
***
****
*****
#include <stdio.h>
int main()
int n = 5;
printf("* ");
printf("\n");
return 0;
Output:
*****
****
***
**
3. Pyramid Pattern
#include <stdio.h>
int main()
int n = 5;
printf(" ");
printf("*");
printf("\n");
return 0;
Output:
***
*****
*******
*********
int main()
int n = 5;
printf("\n");
return 0;
Output:
12
123
1234
12345
Explanation:
• Inner loop (j) prints numbers from 1 to i, forming the increasing triangle pattern.
#include <stdio.h>
int main()
int n = 5;
{
for(int j = 1; j <= i; j++)
printf("\n");
return 0;
Output:
12345
1234
123
12
Explanation:
#include <stdio.h>
int main()
int n = 5;
if(i == 1 || i == n || j == 1 || j == n)
printf("* ");
}
else
printf(" ");
printf("\n");
return 0;
Output:
*****
* *
* *
* *
*****
Explanation:
7. Diamond Pattern
#include <stdio.h>
int main()
int n = 5;
printf(" ");
}
printf("*");
printf("\n");
printf(" ");
printf("*");
printf("\n");
return 0;
output:
***
*****
*******
*********
*******
*****
***
*
Explanation:
• The second loop prints the lower pyramid by reducing the row count.
Type qualifiers in C specify how a variable should be stored or accessed. They do not change the
data type but modify the behaviour of variables.
• Syntax:
#include <stdio.h>
int main()
printf("%d", x);
return 0;
#include <stdio.h>
int main()
const int a = 5;
printf("%d", a);
return 0;
Explanation: The pointer ptr can point to another integer, but cannot modify the value it points to.
#include <stdio.h>
int main()
int a = 10;
int b = 20;
printf("%d", a);
return 0;
Explanation: ptr is a constant pointer, so its address cannot be changed, but the value it points to
can be modified.
#include <stdio.h>
int main()
printf("%d", a);
return 0;
Explanation:
• const int *const ptr → Cannot modify value, cannot change pointer.
#include <stdio.h>
void modify(const int x)
{
x = 20; // Error: Cannot modify a const parameter
}
int main()
{
int num = 10;
modify(num);
return 0;
}
Explanation: A const parameter cannot be modified inside the function.
#include <stdio.h>
void printValue(const int *ptr)
{
*ptr = 100; // Error: Cannot modify a const value
printf("%d", *ptr);
}
int main()
{
int num = 10;
printValue(&num);
return 0;
}
Explanation: ptr is a pointer to const int, so the function cannot modify the value of num.
4. const with Arrays
#include <stdio.h>
int main()
printf("%d", arr[0]);
return 0;
#include <stdio.h>
int main()
printf("%d", ptr[0]);
return 0;
#include <stdio.h>
void changeValue()
{
x = 100; // Error: Cannot modify a global `const` variable
int main()
changeValue();
return 0;
6. const in Structs
#include <stdio.h>
struct Data
{
const int value;
};
int main()
{
struct Data d1 = {100};
d1.value = 200; // Error: Cannot modify a `const` struct member
printf("%d", d1.value);
return 0;
}
Explanation: A const struct member cannot be modified.
What is volatile in C?
The volatile keyword informs the compiler that a variable’s value can change at any time, preventing
the compiler from applying certain optimizations.
When a variable is declared as volatile, the compiler must always read its value from memory instead
of optimizing it by storing it in a register.
• Syntax:
Compiler Optimizations:
Compilers use various optimization techniques to improve performance and reduce code size by
eliminating unnecessary computations. However, these optimizations can sometimes change the
intended behavior of the program, especially when working with hardware registers, multi-threaded
applications, or memory-mapped devices.
Example 1:
#include <stdio.h>
volatile int flag = 0; // Can be modified by hardware
void check()
{
while (flag == 0)
{
// Loop will not be optimized
}
printf("Flag changed!\n");
}
int main()
{
check();
return 0;
}
• Example Problem:
2. Type Modifiers in C
Type modifiers are used to change the storage size and sign of basic data types.
1. signed - Allows both positive and negative values (default for int).
• Syntax:
• Example:
#include <stdio.h>
int main()
return 0;
• Example Problem:
int main()
printf("%u\n", x);
return 0;
• Syntax:
#include <stdio.h>
int main()
return 0;
• Example Problem:
Summary Table
Practice Problems
int main()
*ptr = 20;