Which of the following is used when you want to execute a sequence of statements based on the results of multiple Boolean expressions?
Control Structures - Intro, SelectionFlow of Control:Flow of control through any given function is implemented with three basic types of control structures:
The function construct, itself, forms another way to affect flow of control through a whole program. This will be discussed later in the course. Show
Some useful tools for building programs or program segments
True and False
Logical Operators:The arithmetic comparison operators in C++ work much like the symbols we use in mathematics. Each of these operators returns a true or a false. x == y // x is equal to y x != y // x is not equal to y x < y // x is less than y x <= y // x is less than or equal to y x > y // x is greater than y x >= y // x is greater than or equal to yWe also have Boolean operators for combining expressions. Again, these operators return true or false These operators will be commonly used as test expressions in selection statements or
repetition statements (loops). Examples of expressions(x > 0 && y > 0 && z > 0) // all three of (x, y, z) are positive (x < 0 || y < 0 || z < 0) // at least one of the three variables is negative ( numStudents >= 20 && !(classAvg < 70)) // there are at least 20 students and the class average is at least 70 ( numStudents >= 20 && classAvg >= 70) // means the same thing as the previous expressionShort Circuit Evaluation:
Selection StatementsThe if/else Selection Statement
Examplesif (grade >= 68) cout << "Passing"; // Notice that there is no else clause. If the grade is below 68, we move on. if (x == 0) cout << "Nothing here"; else cout << "There is a value"; // This example contains an else clause. The bodies are single statements. if (y != 4) { cout << "Wrong number"; y = y * 2; counter++; } else { cout << "That's it!"; success = 1; } Multiple statements are to be executed as a result of the condition being true or false. In this case, notice the compound statement to delineate the bodies of the if and else clauses. Be careful with ifs and elses. Here's an example of an easy mistake to make. If you don't use { }, you may think that you've included more under an if condition than you really have. // What output will it produce if val = 2? Does the "too bad" statement really go with the "else" here? if (val < 5) cout << "True\n"; else cout << "False\n"; cout << "Too bad!\n";* Indentation is only for people! It improves readability, but means nothing to the compiler. Example links
Some common errorsWhat's wrong with these if-statements? Which ones are syntax errors and which ones are logic errors?
The switch statement
The Conditional OperatorThere is a special operator known as the conditional operator that can be used to create short expressions that work like if/else statements.
Which statements execute a sequence of statements multiple times?LOOP statements execute a sequence of statements multiple times. There are three forms of LOOP statements: LOOP , WHILE-LOOP , and FOR-LOOP .
Which of the following is a sequential control statement?Sequential control statements, which are not crucial to PL/SQL programming. The sequential control statements are GOTO , which goes to a specified statement, and NULL , which does nothing.
Which of the following statement has no selector and the when clauses of the statement contain search conditions that give Boolean values?The searched CASE statement has no selector. Also, its WHEN clauses contain search conditions that yield a Boolean value, not expressions that can yield a value of any type. An example follows: CASE WHEN grade = 'A' THEN dbms_output.
Which statement will have function like IFIF-THEN-ELSIF Statement. The IF-THEN-ELSIF statement is mainly used where one alternative should be chosen from a set of alternatives, where each alternative has its own conditions to be satisfied.
|