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 y We also have Boolean operators for combining expressions. Again, these operators return true or false x && y // the AND operator -- true if both x and y are true x || y // the OR operator -- true if either x or y (or both) are true !x // the NOT operator (negation) -- true if x is 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 expression Short 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.
What statements can execute a set of statements only under certain circumstances?A structure can execute a set of statements only under certain circumstances. A structure provides one alternative path of execution.
Which structure is a structure that causes a statement or a set of statements to execute repeatedly?A Loop is a control structure that causes a statement or group of statements to be executed repeatedly. Frequently in a program you will want to repeat an action several times or until some condition is met. We do this in a loop.
What is a structure that allows the computer to make a decision between alternative conditions?A decision structure is a construct in a computer program that allows the program to make a decision and change its behavior based on that decision. The decision is made based on the outcome of a logical test.
Which structure determines the order in which a set of statements executes?– Control structure : a logical design that controls the order in which a set of statements execute. – Sequence structure is a set of statements that execute in the order that they appear.
|