C++ Operators and Control Structures

0.0(0)
studied byStudied by 0 people
learnLearn
examPractice Test
spaced repetitionSpaced Repetition
heart puzzleMatch
flashcardsFlashcards
Card Sorting

1/44

flashcard set

Earn XP

Description and Tags

Flashcards created based on C++ relational and logical operators, control structures, and loops.

Study Analytics
Name
Mastery
Learn
Test
Matching
Spaced

No study sessions yet.

45 Terms

1
New cards

What do relational operators do in C++?

Relational operators compare two values and return true or false based on the comparison.

2
New cards

What does < do in C++?

The less than (<) operator returns true if the left operand is smaller than the right operand.

3
New cards

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.

4
New cards

What does > do in C++?

The greater than (>) operator returns true if the left operand is larger than the right operand.

5
New cards

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.

6
New cards

What does == do in C++?

The equality (==) operator returns true if both operands are equal.

7
New cards

What does != do in C++?

The not equal to (!=) operator returns true if the two operands are different.

8
New cards

What do logical operators do in C++?

Logical operators are used to combine or modify boolean expressions, returning true or false.

9
New cards

What does ! do in C++?

The logical NOT (!) operator inverts the boolean value—true becomes false, and false becomes true.

10
New cards

What does && do in C++?

The logical AND (&&) operator returns true if both conditions are true; otherwise, it returns false.

11
New cards

What does || do in C++?

The logical OR (||) operator returns true if at least one of the conditions is true; otherwise, it returns false.

12
New cards

What are the relational operators in C++?

<, <=, >, >=, ==, != <, <=, >, >=, ==, !=

13
New cards

What are the logical operators in C++?

!, &&, ||

14
New cards

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.

15
New cards

What is an if statement?

An if statement executes a block of code if a specified condition evaluates to true.

16
New cards

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.

17
New cards

What is a multi-branch if-else statement?

A multi-branch if-else statement uses multiple if-else conditions to evaluate different cases.

18
New cards

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.

19
New cards

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.

20
New cards

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.

21
New cards

What is a switch statement?

A switch statement allows multi-way branching based on the value of an integer or character expression.

22
New cards

What does the break statement do in a switch statement?

The break statement exits the switch block, preventing fall-through to the next case.

23
New cards

What is switch case fall-through?

If a case does not have a break statement, execution continues into the next case.

24
New cards

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.

25
New cards

How are char and string values compared in C++?

Using relational operators (==, !=,

26
New cards

What does .at() do in a string?

The .at(index) function accesses a character at a specific position in the string with bounds checking.

27
New cards

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.

28
New cards

What does .append() do in a string?

The .append() function adds another string to the end of the current string.

29
New cards

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.

30
New cards

What is a while loop used for?

A while loop is a general-purpose loop that runs as long as its condition remains true.

31
New cards

What is a do-while loop?

A do-while loop executes its body at least once, then continues looping while its condition remains true.

32
New cards

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.

33
New cards

What are the three parts of a for loop?

Initialization, test condition, and update (for(init; test; update)).

34
New cards

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.

35
New cards

What do x++ and y-- do in C++?

x++ increments x by 1 (post-increment), and y-- decrements y by 1 (post-decrement).

36
New cards

What is a count-controlled loop?

A loop that runs a specific number of times, typically using a counter variable.

37
New cards

What is a sentinel-controlled loop?

A loop that continues running until a special sentinel value is encountered (e.g., -1 to stop input).

38
New cards

What are common uses of loops in C++?

Counting, summing values, calculating averages, finding maximum or minimum values.

39
New cards

What is a nested loop?

A loop inside another loop, useful for working with grids, tables, or multi-dimensional data.

40
New cards

What is an infinite loop?

A loop that runs indefinitely, often due to a missing termination condition (e.g., while(true)).

41
New cards

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.

42
New cards

What is incremental development in software?

A method where code is developed and tested in small parts, making debugging easier.

43
New cards

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

44
New cards

Write a C++ program to display a table of sphere volumes for radius values 1 through 10. Use the formula V = (4/3) π .

#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;

}

45
New cards

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;

}