C++ if/else Statements

Choose a study mode

Play Quiz
Study Flashcards
Spaced Repetition
Chat to Lesson

Podcast

Play an AI-generated podcast conversation about this lesson
Download our mobile app to listen on the go
Get App

Questions and Answers

Which conditional statement is best suited for executing a block of code only when a specific condition is true?

  • `if-else` statement
  • `switch` statement
  • `if` statement (correct)
  • `do-while` loop

What is the primary difference between an if statement and an if-else statement?

  • The `if` statement can only evaluate numerical conditions.
  • The `if` statement is used for character comparisons, while `if-else` is for numerical comparisons.
  • There is no functional difference; they can be used interchangeably.
  • The `if-else` statement provides an alternative block of code to execute when the condition is false. (correct)

What is the purpose of the else block in an if-else statement?

  • To execute code when the initial `if` condition is true.
  • To define an additional condition to be checked.
  • To specify the code that runs when none of the `if` conditions are true. (correct)
  • To terminate the program.

When is the else block executed in a series of if-else if-else statements?

<p>Only when none of the preceding <code>if</code> or <code>else if</code> conditions are true. (C)</p> Signup and view all the answers

In C++, what is the correct syntax for a single-line if statement that increments the variable x if y is greater than 10?

<p><code>if (y &gt; 10) x++;</code> (A)</p> Signup and view all the answers

Which keyword is used to specify a default case within a switch statement?

<p><code>default</code> (A)</p> Signup and view all the answers

What happens if a break statement is omitted from a case in a switch statement?

<p>Execution will continue to the next <code>case</code>. (C)</p> Signup and view all the answers

Under what circumstance would an if statement that is nested inside another if statement, be executed?

<p>When both the outer and inner <code>if</code> statements' conditions are true. (C)</p> Signup and view all the answers

What is the purpose of the endl manipulator in C++?

<p>To print a new line and flush the output buffer. (B)</p> Signup and view all the answers

What is the effect of the system("pause"); command in a C++ program?

<p>It pauses the program execution, waiting for user input. (D)</p> Signup and view all the answers

What does it mean to say that the else block will only execute when all other conditions fail?

<p>Given a set of mutually exclusive conditions (connected via <code>if-else if-else</code>), the <code>else</code> statements block of code is executed only when none of the preceding <code>if</code> or <code>else if</code> statements were triggered. (C)</p> Signup and view all the answers

Given the C++ code snippet:

char grade = 'B'; if (grade >= 'A' && grade <= 'Z') cout << "Valid grade"; else cout << "Invalid grade";

What will be the output?

<p><code>Valid grade</code> (B)</p> Signup and view all the answers

Consider the following code:

int num = 75;
if (num > 90) {
 cout << "A";
} else if (num > 70) {
 cout << "B";
} else {
 cout << "C";
}

What will be printed to the console?

<p><code>B</code> (C)</p> Signup and view all the answers

What is the correct way to assign the character z to a variable letter in C++?

<p><code>char letter = 'z';</code> (B)</p> Signup and view all the answers

Given the C++ code: if (x > 5) if (y < 10) cout << "Hello"; else cout << "World";. If x = 3 and y = 12, what is the output?

<p>No output (D)</p> Signup and view all the answers

Which of the following statements about the switch statement in C++ is correct?

<p>It can only be used with integer data types. (C)</p> Signup and view all the answers

What is the purpose of the following code snippet in C++?

if (var >= 'a' && var <= 'z')
 cout << "small alphabet";

<p>To check if <code>var</code> is a lowercase alphabet. (D)</p> Signup and view all the answers

What happens if multiple case labels within a switch statement match the switch expression's value?

<p>Only the first matching <code>case</code> is executed. (A)</p> Signup and view all the answers

Given the nested if structure:

int a = 5, b = 10, c = 15;
if (a < b)
 if (b < c)
 cout << "Both conditions true";
 else
 cout << "Outer true, inner false";

What output will be generated?

<p><code>Both conditions true</code> (C)</p> Signup and view all the answers

Consider the following C++ code. What will be the output?

int x = 10, y = 5;
if (x > 5)
 if (y < 3)
 cout << "A";
 else if (y > 7)
 cout << "B";
 else
 cout << "C";
else
 cout << "D";

<p><code>C</code> (D)</p> Signup and view all the answers

Flashcards

Conditional Operators

Used in programming to make decisions based on conditions.

if/else statement

A conditional statement that executes one block of code if a condition is true and another block if the condition is false.

switch statement

A conditional statement that chooses a code path to execute based on the value of a variable.

endl or \n

Used to print a new line in the output in C++.

Signup and view all the flashcards

Nested if Statement

A situation where an if statement is placed inside another if statement.

Signup and view all the flashcards

if-else-if statement

Used when we need to check multiple conditions sequentially.

Signup and view all the flashcards

Loops

A programming construct used to repeat a block of code until a condition is met.

Signup and view all the flashcards

while loop

A loop that executes a block of code as long as a condition is true.

Signup and view all the flashcards

do...while loop

A loop that executes a block of code at least once, then checks a condition to determine if the loop should continue.

Signup and view all the flashcards

Study Notes

  • Conditional operators are used in programming to make decisions based on conditions.
  • The types of conditional operators include:
    • if/else
    • switch

Conditional Statements in C++

  • The if/else statement executes a block of code if a condition is true, otherwise it executes another block.
  • If the condition in an if statement is true, the "Hello" message prints; otherwise, the "Hi" message prints.
  • The conditional if/else statement can also check inner conditions.
  • When the first condition is true, the second block will not be considered.
  • The if-else-if conditions are used to perform different actions based on a particular grade.
  • endl or \n prints a new line in the output.
  • Before return 0;, write system("pause"); if the screen does not pause after running the code in Visual Studio.

Important Note

  • Do not use a semi-colon ; at the end of a condition.
  • The else block only executes when all other conditions fail.

Character Assignment

  • Assign a character value using single quotes: char variable = 'c';
  • The following code is used to check if the character is a lowercase alphabet: if (var >= 'a' && var <= 'z')
  • Numbers are called ASCII.

If Statement

  • The single if statement executes code if a condition is true and is also known as a one-way selection statement.
  • To execute only one statement in if or else, curly brackets {} can be skipped. If multiple statements are required, enclose them within curly brackets.

If-else Statement

  • The if-else statement executes code if a condition is true or false and is called a two-way selection statement.

Nested If Statement

  • A nested if statement is an if statement inside another if statement.

If-else-if Statement

  • The if-else-if statement is used when multiple conditions need to be checked.

Switch Statement

  • The Syntax for the switch statement is as follows:
switch (n)
{
case constant1:
  // code to be executed if n is equal to constant1;
  break;
case constant2:
  // code to be executed if n is equal to constant2;
  break;
default:
  // code to be executed if n doesn't match any constant
}

Loops

  • Loops are used to repeat a specific block of code until an end condition is met.
  • There are three types of loops:
    • for loop
    • while loop
    • do...while loop

For Loop

  • The syntax for loop is as follows:
for(initialization Statement(counter); testExpression; updateStatement)
{
  // code
}
  • The steps for how for loop works are:
    • The initialization statement is executed only once at the beginning.
    • Then, the test expression is evaluated.
    • If the test expression is false, the loop is terminated.
    • If the test expression is true, codes inside the body of the for loop is executed and the update expression is updated.
    • The test expression is evaluated again until the test expression is false.

While Loop

  • The syntax for loop is as follows:
while (testExpression)
{
  // codes
}
  • The steps for how while loop works are:
    • The while loop evaluates the test expression.
    • If the test expression is true, codes inside the body of the while loop is evaluated.
    • Then, the test expression is evaluated again and continues until the test expression is false.
    • When the test expression is false, the while loop is terminated.

Do...While Loop

  • The do...while loop is a variant of the while loop with one key difference: the body of the do...while loop is executed once before the test expression is checked.
  • The syntax for loop is as follows:
do {
  // codes;
}
while (testExpression);
  • The steps for how while loop works are:
    • The codes inside the body of the loop is executed at least once.
    • The test expression is checked.
    • If the test expression is true, the body of loop is executed, continuing until the test expression becomes false.
    • When the test expression is false, the do...while loop is terminated.

Studying That Suits You

Use AI to generate personalized quizzes and flashcards to suit your learning preferences.

Quiz Team

Related Documents

More Like This

C++ Operators and Statements Quiz
48 questions
Conditional Expressions and Operators
27 questions

Conditional Expressions and Operators

BetterThanExpectedLearning9144 avatar
BetterThanExpectedLearning9144
Use Quizgecko on...
Browser
Browser