Computer QA Chapter 4
Computer QA Chapter 4
Ans. Control Statements: Broad category that manages execution flow, including loops and
jumps (e.g., for, while, break).
Example
Python:
age = 18
In this example, if the age is 18 or older, the message "You are an adult." will be printed.
Q.4.Write the structure of if else statements. Give one examples
Example
Python:
age = 16
In this example, if the age is 18 or older, it prints "You are an adult." Otherwise, it prints "You
are a minor."
Ans. The structure of an if...else if...else statement typically looks like this:
Plaintext:
if (condition1) {
// code block to execute if condition1 is true
} else if (condition2) {
// code block to execute if condition1 is false and condition2 is true
} else {
// code block to execute if both condition1 and condition2 are false
}
Example:
Here's a simple example in JavaScript that checks a person's age and determines if they are a
child, a teenager, or an adult:
JavaScript:
In this example:
Ans. A switch statement is a control structure that allows execution of different code blocks
based on the value of a variable. It consists of a switch keyword, cases for each possible value,
and an optional default case. Each case typically ends with a break statement to prevent fall-
through. Switch statements are useful for simplifying multiple conditions compared to using
several if-else statements, making the code cleaner and more readable.
Ans. Advantage:
Clarity and Readability: Switch statements simplify the handling of multiple conditions,
making the code easier to read and understand compared to a series of if-else statements.
Disadvantage:
Limited Flexibility: Switch statements can only evaluate a single expression and typically work
with discrete values, making them less versatile than if-else statements for complex conditions.
Example:
Python:
age = 20
citizenship = "USA"
Explanation:
In this example:
Ans. Break:
Exits the nearest loop entirely.
Use Case: Stop looping based on a condition.
Continue:
python
Copy code
for i in range(5):
print(i)
2. While Loop: Repeats as long as a condition is true, checking before each iteration.
o Example:
Python:
i = 0
while i < 5:
print(i)
i += 1
3. Do-While Loop: Similar to while, but checks the condition after executing the loop
body, ensuring at least one iteration.
o Example:
Javascript:
let i = 0;
do {
console.log(i);
i++;
} while (i < 5);
In summary, loops are essential for repetitive tasks, with different types offering flexibility based
on the situation.
Q.2.Write a structure of for statements. Give one example.
General Syntax:
Plaintext:
Example in Python:
Python:
Output:
0
1
2
3
4
In this example, the loop initializes i to 0, continues as long as i is less than 5, and increments i
by 1 in each iteration, printing the current value of i.
General Syntax:
Plaintext:
while (condition) {
// Code to be executed in each iteration
}
Example in Python:
Python:
i = 0 # Initialization
while i < 5: # Condition
print(i) # Code block executed in each iteration
i += 1 # Increment
Output:
0
1
2
3
4
In this example, the loop starts with i initialized to 0 and continues as long as i is less than 5. In
each iteration, it prints the value of i and increments it by 1.
1. Code Block: The set of instructions that are executed at least once.
2. Condition: Evaluated after each iteration; the loop continues as long as this condition is
true.
General Syntax:
Plaintext:
do {
// Code to be executed
} while (condition);
Example in JavaScript:
Javascript:
let i = 0; // Initialization
do {
console.log(i); // Code block executed
i++; // Increment
} while (i < 5); // Condition
Output:
0
1
2
3
4
In this example, the loop starts with i initialized to 0. The code block is executed first, printing
the value of i, and then i is incremented. The condition is checked afterward, ensuring the loop
continues until i is no longer less than 5.
While Loop:
C:
int i = 5;
while (i < 5) {
// No output
}
Do-While Loop:
C:
int j = 5;
do {
// Output: 5
} while (j < 5);
Use a while loop for pre-condition checks and a do-while loop for guaranteed execution.
General Syntax:
plaintext
Copy code
for (initialization; condition; increment) {
// Outer loop code
for (initialization; condition; increment) {
// Inner loop code
}
}
Example in C:
C:
#include <stdio.h>
int main() {
int i, j;
return 0;
}
Output:
Python:
1 x 1 = 1
1 x 2 = 2
1 x 3 = 3
1 x 4 = 4
1 x 5 = 5
2 x 1 = 2
2 x 2 = 4
2 x 3 = 6
2 x 4 = 8
2 x 5 = 10
...
5 x 5 = 25
In this example, the outer loop iterates through numbers 1 to 5, and for each iteration of the outer
loop, the inner loop also iterates through 1 to 5, creating a multiplication table.