Chapter 3 Flashcards

1
Q

How is an if-statement used?

A

If-statements are used to execute different actions depending on the situation at hand.

Format:
if (condition)
{
statement1;
statement2;
}

If the condition is true, the statements are executed, however if they are false, all the code in the braces are skipped.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

How does the if-else statement work?

A

If the condition in the parenthesis is true, statement 1 will be executed. If it is false, the first statement will be skipped and the second statement will be executed.

one of the other block is always executed

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

Explain the ir-else-if statement.

A

If condition 1 is true, statement A will be executed, if it is false that first statement will be skipped and will move onto the second condition. If the second condition is true, statement B will be executed otherwise nothing will happen.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

Explain relational operators.

A

Relational operators compare numbers and strings.

Order of least to greatest:
(space) < numbers and symbols < UPPERCASE < lowercase

If two characters are equal, move onto the next character to compare

Ex: “4tom” < “Tom 9” < “joe1” < “joe2”

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

Explain the combined statement: AND (&&)

A

This is a conjunction.

EX: (A&&B) both A and B must be true

DO NOT USE THE WORD AND TO REPRESENT AND, YOU MUST USE &&

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Explain the combined statement: OR (||).

A

This is a disjunction.

EX: (A||B) either A or B must be true

It only evaluates as false if both A and B are false

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

Explain the Boolean operator NOT

A

Format: ! (statement)

Ex: if (! (x<0)) // equivalent to if (X > =0)

If condition C is true then !C is false.

If condition C is false, then !C is true

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

What is the precedence of boolean operators?

A

Parenthesis > Negation > Conjunction > Disjunction

*Recommended: (A || (B && C) )

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

What does it mean that the boolean operators are lazily evaluated?

A

Boolean operators are evaluated from left to right. If the first expression is false, then the second expression will never be evaluated.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
10
Q

What are nested branches?

A

Nested branches are when a decision is contained inside the branch of another decision statement.

Ex:
if (number > 1000) {
if (number % 2 == 0)
cout &laquo_space;“A large even number”;
else cout &laquo_space;“A large odd number;
}
else {
if (number % 2 == 0)
cout &laquo_space;” A small even number”;
else cout &laquo_space;” A small odd number”;

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
11
Q

How do we use if- else statement for input validation?

A

We use them to check if the input is within a required range

Ex:
double x;
cout &laquo_space;“Input a positive number: ”;
cin&raquo_space; x;
if ( x>0 )
{ cout &laquo_space;”The square root of ”
&laquo_space;x &laquo_space;“ is ” &laquo_space;sqrt(x);
}
else
{ cout &laquo_space;“Wrong input.”; }

How well did you know this?
1
Not at all
2
3
4
5
Perfectly