Why Variables and Loops Matter
Ever wondered how a game keeps score or how a calculator repeats a calculation? That's the magic of variables storing data and loops repeating actions.
💡 In Simple Words: A variable is like a labeled jar where you can store a number or word. A loop is a recipe step that tells the computer to keep doing something until a condition is met.
What is a Variable?
A variable is a named storage location in memory. Think of it as a locker with a name on the door. You put a value inside, change it later, or read it whenever you need.
In C (the language we use for ISC), you must declare a variable before using it. Declaration tells the computer what type of data the locker will hold.
int score; // declares an integer variable named scoreint means integer, a whole number without decimals. Other common types are float for numbers with fractions and char for single characters.
How to Use Variables
- Declaration: tell the computer the name and type.
- Initialization: give the variable an initial value.
- Assignment: change the value later.
Example:
int age = 15; // declaration + initialization
age = age + 1; // assignment – now age is 16Notice how we can read, modify, and reuse the same variable many times. That’s why variables are the backbone of any program.
Understanding Loops
A loop is a control structure that repeats a block of code while a condition stays true. Imagine you’re counting coins: you keep adding 1 until you reach 10. The loop handles the repetition for you.
Basic Loop Flow
This flow works for most loops: set up, test, act, update, repeat.
Types of Loops in C
| Loop | Syntax | When to use |
|---|---|---|
| for | for(initial; condition; update){
// body
} | When you know the exact number of repetitions. |
| while | while(condition){
// body
} | When the loop should continue as long as a condition stays true, and the condition is checked before each pass. |
| do‑while | do{
// body
}while(condition); | When the body must run at least once, even if the condition is false initially. |
For Loop Example
for(int i = 1; i This prints: 1 2 3 4 5. The variable i starts at 1, checks i , prints, then adds 1.
While Loop Example
int i = 1;
while(i Same output, but the initialization happens outside the loop.
Do‑While Loop Example
int i = 1;
do{
printf("%d ", i);
i++;
}while(i Even if i started at 6, the number would still print once because the body runs before the check.
Choosing the Right Loop
- If you know the exact count, reach for
for. - If the loop might never run, use
while. - If you must run at least once, pick
do‑while.
Mixing variables with loops lets you build powerful programs: calculate sums, generate patterns, or process arrays.
Common Pitfalls
- Forgot to update the loop variable – leads to an infinite loop.
- Using the wrong condition operator (
=vs==) – can cause logic errors. - Declaring a variable inside a loop when you need its value later – the variable disappears after the loop ends.
Quick Recap
- Variables store data; declare them with a type.
- Initialize them before use.
- Loops repeat code:
for,while,do‑while. - Pick the loop that matches the repetition pattern.
📝 Likely Exam Questions
- Define a variable and give an example in C.
Answer: A variable is a named memory location that holds a value. Example:int marks = 85; - Write a C program fragment to print numbers 1 to 10 using a while loop.
Answer:int i = 1; while(i - Explain the difference between a for loop and a do‑while loop.
Answer: A for loop checks the condition before each iteration and is ideal when the number of repetitions is known. A do‑while loop executes the body first, then checks the condition, guaranteeing at least one execution. - What will be the output of the following code?
int i = 0; for(; i
Answer: 012 – the loop runs while i is less than 3, printing i then incrementing. - Identify the error in this snippet and correct it:
int sum; while(sum
Answer:sumis used before initialization. Fix:int sum = 0;before the loop.