1/44
Flashcards created based on C++ relational and logical operators, control structures, and loops.
Name | Mastery | Learn | Test | Matching | Spaced |
---|
No study sessions yet.
What do relational operators do in C++?
Relational operators compare two values and return true or false based on the comparison.
What does < do in C++?
The less than (<) operator returns true if the left operand is smaller than the right operand.
What does <= do in C++?
The less than or equal to (<=) operator returns true if the left operand is smaller than or equal to the right operand.
What does > do in C++?
The greater than (>) operator returns true if the left operand is larger than the right operand.
What does >= do in C++?
The greater than or equal to (>=) operator returns true if the left operand is larger than or equal to the right operand.
What does == do in C++?
The equality (==) operator returns true if both operands are equal.
What does != do in C++?
The not equal to (!=) operator returns true if the two operands are different.
What do logical operators do in C++?
Logical operators are used to combine or modify boolean expressions, returning true or false.
What does ! do in C++?
The logical NOT (!) operator inverts the boolean value—true becomes false, and false becomes true.
What does && do in C++?
The logical AND (&&) operator returns true if both conditions are true; otherwise, it returns false.
What does || do in C++?
The logical OR (||) operator returns true if at least one of the conditions is true; otherwise, it returns false.
What are the relational operators in C++?
<, <=, >, >=, ==, != <, <=, >, >=, ==, !=
What are the logical operators in C++?
!, &&, ||
What is the precedence order of relational and logical operators?
Relational operators (<, <=, >, >=, ==, != <, <=, >, >=, ==, !=) have higher precedence than logical operators (!, &&, ||). Parentheses can be used to control evaluation order.
What is an if statement?
An if statement executes a block of code if a specified condition evaluates to true.
What is an if-else statement?
An if-else statement executes one block of code if the condition is true and another block if the condition is false.
What is a multi-branch if-else statement?
A multi-branch if-else statement uses multiple if-else conditions to evaluate different cases.
What is a nested if-else statement?
A nested if-else statement is an if-else structure inside another if or else block, allowing for more complex decision-making.
What is a block or compound statement in C++?
A block or compound statement is a group of statements enclosed in {} that are executed together.
How can ranges be detected using multi-branch if-else statements?
By using multiple if-else conditions, a value can be compared against different ranges without explicitly checking every possible value.
What is a switch statement?
A switch statement allows multi-way branching based on the value of an integer or character expression.
What does the break statement do in a switch statement?
The break statement exits the switch block, preventing fall-through to the next case.
What is switch case fall-through?
If a case does not have a break statement, execution continues into the next case.
How can multiple cases be handled in a switch statement?
Multiple case labels can be used before a single block of code to handle different values the same way.
How are char and string values compared in C++?
Using relational operators (==, !=,
What does .at() do in a string?
The .at(index) function accesses a character at a specific position in the string with bounds checking.
What is the difference between .size() and .length() in a string?
.size() and .length() both return the number of characters in a string and are functionally equivalent.
What does .append() do in a string?
The .append() function adds another string to the end of the current string.
Why is floating point comparison tricky in C++?
Due to precision errors, direct comparison (==) may not work correctly. It is recommended to check if the absolute difference is within a small epsilon value.
What is a while loop used for?
A while loop is a general-purpose loop that runs as long as its condition remains true.
What is a do-while loop?
A do-while loop executes its body at least once, then continues looping while its condition remains true.
When is a do-while loop useful?
A do-while loop is good for repeating a process that should always run at least once, such as user input validation.
What are the three parts of a for loop?
Initialization, test condition, and update (for(init; test; update)).
When should a for loop be used?
A for loop is best for situations with a known number of iterations, such as counting or iterating through arrays.
What do x++ and y-- do in C++?
x++ increments x by 1 (post-increment), and y-- decrements y by 1 (post-decrement).
What is a count-controlled loop?
A loop that runs a specific number of times, typically using a counter variable.
What is a sentinel-controlled loop?
A loop that continues running until a special sentinel value is encountered (e.g., -1 to stop input).
What are common uses of loops in C++?
Counting, summing values, calculating averages, finding maximum or minimum values.
What is a nested loop?
A loop inside another loop, useful for working with grids, tables, or multi-dimensional data.
What is an infinite loop?
A loop that runs indefinitely, often due to a missing termination condition (e.g., while(true)).
What is the scope of a variable inside a loop?
Variables declared inside { } are only accessible within that block and are destroyed when the block exits.
What is incremental development in software?
A method where code is developed and tested in small parts, making debugging easier.
What is the output of the following C++ code?
cpp
CopyEdit
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
int fox = 6;
double dog = 5.7;
dog = fox + dog;
if (fox > dog)
cout << "Hello!";
else if (fox < dog)
cout << dog;
else
cout << fox;
cout << endl;
cout << fixed << setprecision(1);
cout << "dog is: " << dog << endl;
}
A) Hello! dog is: 5.7
B) Hello! dog is: 11.7
C) 11.7 dog is: 11.7
D) 6 dog is: 5.7
C) 11.7 dog is: 11.7
dog = fox + dog;
→ dog = 6 + 5.7 = 11.7
fox (6) < dog (11.7)
, so cout << dog;
→ 11.7
cout << fixed << setprecision(1);
ensures one decimal place.
cout << "dog is: " << dog << endl;
prints dog is: 11.7
Write a C++ program to display a table of sphere volumes for radius values 1 through 10. Use the formula V = (4/3) π r³.
#include <iostream>
#include <iomanip>
using namespace std;
int main() {
const double PI = 3.14159;
cout << "Radius\tVolume\n";
cout << fixed << setprecision(2);
for (int r = 1; r <= 10; r++) {
double volume = (4.0 / 3.0) PI r r r;
cout << r << "\t" << volume << endl;
}
return 0;
}
Write a C++ program that inputs a series of numbers ending with -1
and outputs the count of numbers greater than 50.
#include <iostream>
using namespace std;
int main() {
int num, count = 0;
cout << "Enter numbers (-1 to stop): ";
while (true) {
cin >> num;
if (num == -1) break;
if (num > 50) count++;
}
cout << "Count of numbers greater than 50: " << count << endl;
return 0;
}