Control Structures: If Statement

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 control structure allows a program to choose from alternative paths of execution?

  • Repetition statement
  • Compound statement
  • Simple statement
  • Selection statement (correct)

What is a key characteristic of a compound statement in the context of control structures?

  • It ends with a semicolon.
  • It can't be used within control structures.
  • It's grouped into a block enclosed in braces. (correct)
  • It consists of a single instruction.

Under what condition will the statement within an if statement be executed?

  • Always, regardless of the expression's value.
  • If the expression isn't a number
  • Only if the expression evaluates to true. (correct)
  • Only if the expression evaluates to false.

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

<p>To specify a block of code to execute only IF the initial <code>if</code> condition is false. (C)</p> Signup and view all the answers

How does the conditional operator (?:) function?

<p>It takes three arguments. (D)</p> Signup and view all the answers

What is the role of the break keyword in a switch statement?

<p>To prevent 'fall-through' to the next case. (D)</p> Signup and view all the answers

What happens in a switch statement when the expression does not match any of the listed case constants?

<p>The optional <code>default</code> section is executed if provided. (B)</p> Signup and view all the answers

Which statement about repetition structures is correct?

<p>Repetition structures execute a sequence of statements until a condition is met. (C)</p> Signup and view all the answers

What are the two essential segments of a program loop?

<p>Body and control statement. (D)</p> Signup and view all the answers

What distinguishes an entry-controlled loop from an exit-controlled loop?

<p>Entry-controlled loops test the condition prior to loop execution. (C)</p> Signup and view all the answers

Which type of loop is guaranteed to execute its body at least once?

<p><code>do-while</code> loop (C)</p> Signup and view all the answers

Which of the following is NOT a requirement for a while loop control variable to execute correctly?

<p>Being declared as <code>constant</code> (D)</p> Signup and view all the answers

In a for loop, what is the purpose of the 'initialization' section?

<p>To declare and optionally initialize loop control variables; it executes only once. (C)</p> Signup and view all the answers

What happens to the flow of control in a for loop after the body of the loop is executed?

<p>It checks the condition to decide if the body of the loop is to be executed again. (A)</p> Signup and view all the answers

In the context of loop control, what is a counter-controlled loop?

<p>A loop that iterates a known number of times. (D)</p> Signup and view all the answers

What is the role of a 'sentinel value' in a sentinel-controlled loop?

<p>To indicate the end of data input, causing the loop to terminate. (D)</p> Signup and view all the answers

What happens when a return 0; statement is executed within a function?

<p>The program or function terminates at the point of execution. (A)</p> Signup and view all the answers

What is the purpose of the stdlib. header file when using the exit() function?

<p>It's required as it contains the declaration for <code>exit()</code> (D)</p> Signup and view all the answers

What effect does the break statement have on a loop's execution?

<p>Terminates the loop entirely. (B)</p> Signup and view all the answers

In the context of loops, what is the key difference between break and continue statements?

<p><code>break</code> exits the loop; <code>continue</code> skips an iteration. (C)</p> Signup and view all the answers

What action does the continue statement perform in a loop?

<p>It skips to the beginning of the loop's body for the next iteration. (B)</p> Signup and view all the answers

What are potential consequences of using an 'empty for loop' ( for (;;) ) without any control statements?

<p>It will create an infinite loop. (C)</p> Signup and view all the answers

What is a 'zero iteration loop'?

<p>A loop whose condition is never met, so it never executes. (A)</p> Signup and view all the answers

In the context of loops, what is a 'flag-controlled loop'?

<p>A loop that uses a boolean variable to control its execution. (D)</p> Signup and view all the answers

What is the defining characteristic of a 'nested loop'?

<p>A loop within another loop. (A)</p> Signup and view all the answers

In nested loops, how often does the outer loop change in relation to the inner loop?

<p>The outer loop changes only after the inner loop has finished all its iterations (B)</p> Signup and view all the answers

Which loop statement described below would be described as a pretest loop?

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

What is the proper syntax for a simple if statement?

<p>if (conditional expression) statement; (B)</p> Signup and view all the answers

What is the correct syntax for the conditional operator?

<p>( expression1) ? expression2 : expression3 (A)</p> Signup and view all the answers

According to the material provided, which is the correct syntax for a compound statement?

<p>{ statement1; statement2; } (D)</p> Signup and view all the answers

Which of the options below accurately lists selection statements?

<p>if, if-else, switch (B)</p> Signup and view all the answers

What should you include in your code to handle unexpected values in a switch statement?

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

In a while loop, what happens if the condition is never met?

<p>The statements inside the loop are skipped. (C)</p> Signup and view all the answers

In a for loop, which of the following is executed only one time?

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

Which type of the loop is best suited when the number of iterations are known?

<p>For loop (B)</p> Signup and view all the answers

Which of the following is/are additional features of for loop?

<p>You can use expressions in the assignment statements of initialization and increment section. (A)</p> Signup and view all the answers

Flashcards

Simple Statement

A simple instruction that ends with a semicolon.

Compound Statement

Instructions grouped into a block enclosed in braces {}.

"if" statement

A control flow statement that executes a block of code only if a specified condition is true.

"if-else" statement

A control flow statement that first evaluates a condition; if true, it executes a block of code, otherwise, it executes another block of code.

Signup and view all the flashcards

Conditional operator(?:)

A shorthand way of writing a simple if-else statement. It is a ternary operator that takes three operands.

Signup and view all the flashcards

"switch" statement

A control statement that allows a variable to be tested for equality against a list of values.

Signup and view all the flashcards

"break" statement

A statement that separates the execution of statements per labeled case in a switch statement.

Signup and view all the flashcards

"default" section

An optional section in a switch statement that is executed if the expression does not match any of the listed constants.

Signup and view all the flashcards

Repetition statements

A control flow statement that allows code to be executed repeatedly based on a given boolean condition.

Signup and view all the flashcards

Body of the Loop

A segment with instructions that will be repeated.

Signup and view all the flashcards

Control Statement

A part of a loop that checks the condition for the execution of the body the loop.

Signup and view all the flashcards

Entry-controlled loop

A loop where the condition is tested before the loop body is executed.

Signup and view all the flashcards

Exit-controlled Loop

A loop where the condition is tested after the loop body is executed.

Signup and view all the flashcards

"while" loop

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

Signup and view all the flashcards

"do-while" loop

A repetition statement similar to a while loop, but the loop body is executed at least once.

Signup and view all the flashcards

"for" loop

A repetition statement useful for executing a block of code a specific number of times.

Signup and view all the flashcards

Initialization

The initialization section of a for loop is executed only once to declare loop control variables.

Signup and view all the flashcards

Test-condition

The test-condition is evaluated before each iteration; the loop continues if the condition is true.

Signup and view all the flashcards

Increment/Decrement

The step size of an for loop, evaluated after the body to determine more iterations are necessary.

Signup and view all the flashcards

"break" statement

It stops the execution of a loop or switch statement and transfers control to the statement immediately following the loop or switch.

Signup and view all the flashcards

"continue" statement

A statement that skips the current iteration of a loop and proceeds to the next iteration.

Signup and view all the flashcards

Zero Iteration Loop

A loop that immediately terminates upon the first test.

Signup and view all the flashcards

Flag-controlled Loop

A loop that continues as long as a flag variable is true.

Signup and view all the flashcards

Nested Loop

A loop placed inside the body of another loop.

Signup and view all the flashcards

Study Notes

  • Lecture on Control Structures
  • Engineering Pre-Major Year Collaborative (EPYC) Program, January 2022

Remember: Simple Statements

  • Simple statements are simple instructions that end with a semicolon
  • Example: statement1;

Remember: Compound Statements

  • Compound statements are several instructions grouped into a block enclosed in braces {}
  • Example:
{ 
 statement1;
 statement2; 
}

Selection Statements

  • Checks certain conditions before executing a set of statements
  • Provides the branching mechanism which allows a program to choose from alternatives
  • Can be in the form of an if statement, if-else statement, conditional operator (?:), or a switch statement.

If Statement

  • Used for single selection
  • The statement executes if and only if the expression evaluates to true

If Statement: Simple Statement Syntax

if (conditional expression)
 statement;

If Statement Example

#include <iostream>
using namespace std;

int main() {
 int hinum = 0;
 cout << "Please type in a number";
 cin >> hinum;

 if (hinum == 123)
  cout << "Correct";
 return 0;
}

If Statement: Compound Statements Syntax

if (conditional expression)
{
 statement1;  //first
 statement2;  //second
    .
 statementN;  //Nth
}

Compound Statement Example

#include <iostream>
using namespace std;

int main() {
 float fPrice;
 cout << “Enter the price:”;
 cin >> fPrice;
 if (fPrice >= 1000) {
  cout << “Expensive!\n”;
  cout << “Can't Afford!\n”;
 }
 return 0;
}

Studying That Suits You

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

Quiz Team

Related Documents

More Like This

Use Quizgecko on...
Browser
Browser