CS 1428 Unit 3: if/else and switch Statements

Lakukan tugas rumah & ujian kamu dengan baik sekarang menggunakan Quizwiz!

variables with same name

-In an inner block, a variable is allowed to have the same name as a variable in the outer block. -When in the inner block, the outer variable is not available (it is hidden). -Not good style: difficult to trace code and find bugs

two

All of the relational operators are binary, which means they use this many operands.

relational

Numeric data is compared in C++ by using these operators.

if Statement and how it works

The common programming structure that implements "conditional statements". -causes other statements to execute only under certain condtions

>= and <=

Which of the following relational operators test for two relationships? >= and <= > and < All of these choices ==and !=

conditionally executed

action is _______ _______ bc it is only performed when a certain conditions exists

Relational operators

allow you to compare numeric and char values and determine whether one is greater than, less than, equal to, or not equal to another.

break statement format example

char ch; ... switch (ch) { case 'a': case 'A': cout << "Option A"; break; case 'b': case 'B': cout << "Option B"; break; case 'c': case 'C': cout << "Option C"; break; default: cout << "Invalid choice"; }

if/else statemenets: how it works

expression is evaluated -if it is true, statement 1 is executed (statement 2 is skipped) -if it is false, statement 2 is executed (statement 1 ignored)

if statement format

if (expression) statement

if/else statement format

if (expression) statement1 (or block) else statement2 (or block) //NO ; after expression //indent statements in each branch!

if/else if statement format

if (expression) statement; else if (expression) statement; else if (expression) statement; else if (expression) statement; else if (expression) statement; //braces removes, put if (expression) on previous line, elminates nested indentation

nested if statement format

if (expression) statement; else { if (expression) statement; else { if (expression) statement; else { if (expression) statement; else statement; } } } //braces optional!

nested if statements

if/else statement that can occur as a branch of another if/else statement to test more than one condition

scope of a variable

innermost block in which it is defined, from point of definition to end of block

relational operators has higher precedence than

of relational operators and the assignment operator, which has a higher precedence

Reason for placement of semicolon in an if/else statement

semicolons mark the end of a complete c++ statement and not a line, so semicolon must be placed after statement instead of expression

block (or compound) statement

set of statements inside braces, allows use of multiple statements when by rule only one is allowed because it groups several statements into single one

if/else statement

statement that causes program execution to follow one of two paths

switch statement

statement that lets the value of a variable or expression determine where the program will branch -like if/else, used to select one of multiple alternative code selections ***NO strings or floating point

switch statement format

switch (IntegerExpression) { case ConstantExpression: // place one or more // statements here case ConstantExpression: // place one or more // statements here // case statements may be repeated as many // times as necessary default: // place one or more // statements here } //braces not needed and not recommened //default: is optional

if/else if statement

tests a series of conditions until one is found to be true, often simpler than nested if (not really a different statement, just different way of indeting the nested if staement)

scope

the ______ of a variable is the part of the program where the variable may be used

Boolean expressions

their value can only be true or false. If x is greater than y, the expression x > y will be true, while the expression y == x will be false.

break statement

this statement causes an immediate exit from the switch statement, without a break statement, execution continues on to nest set of statements (the next case)

switch statement: how it works

this statement tests ONE integer/char expression against MULTIPLE constant integer values: -expression is evaluated to an int/char value -execution starts at the case labeled with that int.char value -execution starts at defualt if the int/char value matches none of the case labels

relational expression used to determine whether x is greater than y

x > y

block(compound) statement formate

{ int x; cout << "Enter a value for x: " << endl; cin >> x; cout << "Thank you." << endl; }


Set pelajaran terkait

Research Methods Final (Multiple Choice)

View Set

Ford and Carter Presidencies, Reagan Era and End of the Cold War, Foreign Policy Challenges of the 90s, Domestic and International Terrorism, Contemporary Domestic Issues, The Civil Rights Movement, The Vietnam War, Domestic Policies of Johnson and N...

View Set

Chapter 16 - The Federal Reserve and Monetary Police

View Set

28 Fundamentals of Electricity: Unit 28 - Single-Phase Transformers

View Set

Sensation and Perception - Chapter 3

View Set