Unit #04
Learn Complete Summary + Syntax ( Most Important short question)
Conceptual Question
What is a loop?
A loop is a structure in programming that allows a set of instructions to be executed repeatedly until a
specific condition is met.
What is the purpose of a loop?
Loops are used to repeat a block of code multiple times, reducing code size and saving time (e.g.,
printing numbers 1 to 10).
What is a for loop?
A for loop is a counter-controlled loop that repeats code a specific number of times. It includes
initialization, condition, and increment/decrement in one line.
What is a nested loop?
A loop inside another loop is known as a nested loop.
What is the difference between while and do-while loops? A while loop checks the condition before
execution, whereas a do-while loop executes the body at least once before checking the condition.
What is an infinite loop?
A loop that never terminates because its condition always remains true is called an infinite loop.
Define break and continue statements?
break exits the current loop, while continue skips the current iteration and jumps to the next one.
What is a loop control variable?
A variable used to control the execution of a loop, which is initialized, tested, and updated.
What is an Array?
An array is a data structure that can hold multiple values of the same data type at consecutive locations
in computer memory.
How is an array element accessed?
Each element of an array has a unique index (starting from 0). You access it by using the array name
followed by the index in square brackets, e.g., array_name[index].
What is Array Initialization?
It is the process of assigning values to array elements at the time of declaration. Example: int marks[3] =
{70, 80, 90};.
What is a Data Structure?
A container to store a collection of data items in a specific layout. Arrays and lists are common
examples.
How does an array differ from a simple variable?
A simple variable can store only one value at a time, whereas an array can store multiple values of the
same type under a single name.
Loop & Control Structure Details
What is an iteration?
Each individual run or cycle of a loop is called an iteration.
What is the syntax of a for loop?
The general syntax is: for (initialization; condition; increment/decrement) { // code }.
What is the "counter variable"?
It is the variable used in a loop to keep track of the number of iterations.
What happens if a loop condition never becomes false?
The loop will repeat indefinitely, resulting in an infinite loop that never terminates.
How are loops and arrays related?
Loops are commonly used to efficiently perform operations on arrays, such as taking input into an array
or reading all its values.
While Loop
The while loop is an entry-controlled loop. It evaluates the condition before executing the loop body. If
the initial condition is false, the body may not execute even once.
while (condition) {
// block of code to be executed
}
Do-While Loop
The do-while loop is an exit-controlled loop. It executes the loop body first and then checks the
condition. This guarantees that the code inside the loop will run at least once, even if the condition is
false from the start.
do {
// block of code to be executed
} while (condition);
Programming Snippets (Common Short Tasks)
Display 1 to 10: for (int i = 1; i <= 10; i++) { printf("%d\n", i); }.
Table of 2: A for loop running from 1 to 10 that prints 2 * i at each step.