Shared Flashcard Set

Details

C#
Chapter 4 - Making Decisions / If, Else Statements
12
Computer Science
Undergraduate 4
10/21/2024

Additional Computer Science Flashcards

 


 

Cards

Term
Pseudocode
Definition

A tool that helps them plan a program’s logic by writing plain English statements.

Ex.)

  • go west on Algonquin Road,
  • turn left on Roselle Road,
  • enter expressway heading east, and so on—you have written pseudocode.
Term
Flowchart
Definition

Sequence Structure: One step follows another step with no branching pathways, linear design

 

Decision Structure: Steps can branch off depending on whether certain conditions are met, such as a true or false statement. See below

[image]

Term
if-else Statements
Definition

Used to check a condition in C#. If the condition is true, it executes a block of code. Optionally, you can use else to specify what happens when the condition is false.

 

Ex.) 

if (age >= 18)      //DO NOT ADD SEMICOLON HERE

{

    Console.WriteLine("You are an adult.");

}

else

{

    Console.WriteLine("You are not an adult.");

}

 

Term
Nested If Statement
Definition

An if statement inside another if. It allows you to check multiple conditions, with the inner condition only evaluated if the outer condition is true. 

Ex.)

int age = 20;

bool hasID = true;

 

if (age >= 18) // First condition

{

    if (hasID) // Second condition, only checked if age >= 18

    {

        Console.WriteLine("You are allowed to enter.");

    }

}

 

Term
Conditional AND Operator (&&)
Definition

Used to check multiple conditions in an if statement. All conditions must be true for the code block to execute. If any condition is false, the expression will short-circuit because the whole expression will be false. Must be a complete statement on both sides of the "&&."

 

Ex.) [image]

Term
Conditional Or Operator (||)
Definition

Used to check multiple conditions in an if statement. Only requires one of the conditions to be true for the entire expression to evaluate as true. If the first condition is true the expression will short-circuit and doesn't evaluate the second condition because it doesn't need to, as the entire expression is true. Ex.)

bool isWeekend = true;

bool isHoliday = false;

 

if (isWeekend || isHoliday)

{

    WriteLine("You can relax today.");

}

else

{

    WriteLine("It's a workday.");

}

 

 

 

Term
Truth Tables
Definition

A mathematical table used to determine the result of a logical expression for every possible combination of inputs.

&& Table: Evaluates False unless both are True

[image]

 

|| Table: Evaluates True unless both are False

 

[image]

Term
Differences between Logical And/Or and Conditional And/Or
Definition

In terms of how expressions are evaluated, both do the same thing, however Logical and/or (& and |) do not support short-circuit evaluation like Conditional And/Or (&& and ||). Ex.)

 

bool a = false;

bool b = true;

if (a & b)  // b is evaluated even though "a" is already false

{

    Console.WriteLine("Both are true.");

}

 

Term
Combining AND and OR Operators
Definition

You can combine as many && and || operators in an expression as you need. When you use a series of only && or || operators in an expression they are evaluated from left to right. However, when you use both && and || operators in the same expression, the && operators take precedence. You can change this precedence by putting the || operator(s) in parentheses.

 

bool hasLicense = true;

bool hasCar = false;

int age = 20;

 

if ((age >= 18 && hasLicense) || hasCar)

{

    Console.WriteLine("You can drive.");

}

else

{

    Console.WriteLine("You cannot drive.");

}

 

Term
Switch Statements
Definition

An alternative to multiple nested-if statements. Tests a single variable against a series of exact matches.

 

Keywords:

Switch - Starts structure and is followed by a test expression ("year" in the example below)

Case - followed by one of the possible values that might equal the switch expression

Break - terminates a switch structure at the end of each case

Default - Optionally used for when a test expression does not match a case.

Ex.)

[image]

Term
Conditional Operator (?)
Definition

Shorthand for an else-if statement. Follows the syntax:

 

condition ? expression_if_true : expression_if_false;

Ex.) 

int age = 20;

 

string message = age >= 18 ? "You are an adult." : "You are a minor.";

Console.WriteLine(message); // Output: "You are an adult."

 

 

Term
The NOT Operator (!)
Definition

Inverts the Boolean value of a condition. If a condition is true, ! makes it false, and vice versa.

 

Ex.) 

bool isRaining = false;

 

if (!isRaining)

{

     Console.WriteLine("You can go outside without an umbrella.");

}

 

 

Supporting users have an ad free experience!