C++: Expressions, if, loops (TBC)

¡Supera tus tareas y exámenes ahora con Quizwiz!

How do you use the conditional operator

(condition) ? true action : false action

When should I use an if-else-if ladder rather than a switch

1. when the conditions controlling the selection process do not rely upon a single value (e.g. age, nationality, gender needs if-else-if not a switch) 2. You need if-else-if when testing floating-point values or other objects that are not of types valid for a switch 3. When the testing is not of an equality. switch is used for testing against specific case constants (but also with a default option for anything else) but if can test for things like > 10.

Important points about switch and if

5 key points: 1. switch can test only for equality (i.e. equals one of the case constants) whereas if can be of any type (e.g. > or not equals etc.) 2. No 2 case constants in the same switch can have identical values 3. A switch statement is usually more efficient than nested ifs 4. The statement sequences associated with each case are not blocks. However the entire statement does define a block. 5. The different branches of the switch all refer to the same condition being tested, whereas the different else if tests can test different things. E.g. a switch can test if someone is in their teens or 20s or 30s etc but an if else if ladder can test if someone is in their teens or is male or lives in Dublin.

Give examples of Console statements

<< is the console output operator and is used with cout to make a console output statement e.g. cout << "Eamonn is cool. \n". Console output generally refers to the computer's screen (but could be e.g. a printer). cin is the console input command, is usually linked to the keyboard and has the syntax cin >> var;

What is a nested if statement

A nested if statement is one that is the target of another if or else. Remember that an else statement always refers to the nearest if statement that is within the same block and not already associated with an else.

Accumulator

A variable that keeps a total amount over each iteration of a loop.

Flag

A variable that signals the condition of the program. EX: A variable to mimic on/off state of a TV based on if statements.

What sort of expression can be used to control an if statement

Any valid C++ expression can be used. The controlling expression must evaluate to a true or false result. A value of 0 is evaluated as false and all non-zero values are converted as true. E.g. myanswer = a + b; if (myanswer) cout << " Ok to divide by this number"; else cout << Warning, answer is 0 & you cannot divide by 0";

Nested if-statements are used for:

Basically used for multi-level conditions.

What is an if-else-if ladder (aka staircase)

If(condition) statement; else if(condition) statement; else if(condition) statement; ... else statement; The conditional expressions are evaluated from the top downwards. As soon as a true condition is found, the statement associated with it is executed and the rest of the ladder is bypassed. The final else often acts as a default action ie if all the other conditional tests fail, then the last else statement is performed. If there is no final else statement and all other conditions are false, then no action is performed.

What is switch

Switch provies for a multiway branch. IE it enables a program to select among several alternatives. A series of nested if statements can perform multiway tests, but for many situations the switch is a more efficient approach. The value of an expression is successively tested against a list of constants. When a match is found, the statement sequence associated with that match is executed. switch (expression) { case constant1: statement sequence break; case constant2: statement sequence break; ... default: statement sequence } The switch expression must evaluate to either a character or an integer value (floating point not allowed). Frequently the expression is just a variable. The case constants must be integer or character literals.

break

Terminates a loop. Usually used in conjunction with an if statement inside a loop to terminate prematurely. In a nested loop it only works for the loop it's in.

What happens when operands have different data types in an expression?

They are converted to the same data type with the highest precedence.

Use of a break statement in switch

Usually each case ends with break; If there is no break, than all of the statements from there on will be executed. IE with no break, execution continues into the next case if there is no break. Usually (but not always) you do not want this.

Can you have nested switch statements

Yes

Can you have empty cases

Yes. Eg switch(i) { case1: case2: case3: cout << "Is less than 4"; break case4:

Internally, True is stored as ____ and False is stored as _____

a non-zero number (usually 1 by default) zero

how are else if and else used?

else if, is used after the initial if as an alternative set of conditions that continues in a chain testing for a condition to be true. else is used after all other if statements are exhausted as a default condition.

In a for-loop, how is the increment/decrement changed?

for(num = 0; num <=10; num += 2); notice the compound assignment operator at the end.

What is the syntax for an if statement?

if(condition) { statements } alternatively: if(condition) statement (only if one statement is used)

What is the full form of the if statement

if(expression) statement; else statement; One part or other will be executed, never both. Note that the semicolon is need after the if part, not just after the whole statement. A block i.e. { statements } can be used rather than a single statement. ie if (expression) { statement sequence } else { statement sequence }

If the variable is initialized in a for-loop then ____

it is limited to the scope of the loop.

prefix and postfix increment and decrement operators ? What is the difference?

pre: ++num, --num post: num++, num-- In simple cases there is n difference between ++x; and x++; but where it is used as part of a larger expression there is: pre increments/decrements first then returns the values of the variable for use by the rest of the expression. Post returns the value first then increments/decrements Eg x = 10; y = ++x; //sets y to 11, x to 11. x=10; y = x++; //sets y to 10, y to 11. In both cases, x is set to 11

what are pre and post test loops?

pre: while and for loops. tests before iteration. post: do-while. tests after

For a nested Loop statement what is true of the inner loop?

The inner loop goes through all of its repetitions for each single repetition of the outer loop. Therefore the total number of loops for the inner loop is a product of the inner repetitions times the outer.

%? What is the condition to use it?

The modulus operator. It returns the remainder of two numbers. Both numbers must be int ex: 10 % 3 = 1 because 10 divided by 3 leaves a remainder of 1

continue

skips an iteration of a loop Usually used in conjunction with an if-statement inside the loop.

Switch statement structure?

switch(variable) { case value1: action break; case value2: action break; default: action }


Conjuntos de estudio relacionados

ATI RN Nutrition Online Practice 2023 B

View Set

Liver/Hep & DKA/HHS Evolve N3 Practice Questions

View Set

Jurisdiction of the Federal Courts

View Set

Module 1: Chapter 1: Introduction to Computers and Programming

View Set

Nursing foundations exam 2 black chapter 4

View Set

End-of-Life, Palliative Care, Spirituality, Pain Saunders NCLEX ch. 5,6,71 (culture, ethics, crisis)

View Set

Epiglottitis Practice Questions:

View Set