180 likes | 199 Views
Get answers to C++ programming questions and helpful tips on expressions, loops, and more. Enhance your coding skills with practical examples and explanations.
E N D
Quiz Answers 1. Show the output from the following code fragment: int a = 5, b = 2, c = 3; cout << a + b * c << endl; 11 2. Give an expression in C++ that is true when the variable age is between 0 and 100, inclusive. 0 <= age && age <= 100
Quiz Answers (cont'd) 3. What is a data object and what is a variable? A data object is a box that holds a value. A variable is a label for the box. 4. What is the value of the expression 5/3 and the value of the expression 5.0/3.0? 1 1.6666...
Quiz Answers (cont'd) 5. Give the syntax for the if-else statement. if (<Boolean-expression>) <statement> else <statement> 6. What's wrong with the following code fragment? int a, b; cout << a + b << endl; The variables aren't initialized.
More on Boolean Expressions • Simple Boolean (true/false) expressions are built by using relational operators: <, <=, >=, >, ==, != • More complex ones can be built by combining simple expressions with Boolean operators: &&, ||, and !. • The && operator is “and.” Both operands must be true. The || operator is “or.”
Examples of Boolean Expressions • grade is between 90 and 100, inclusive: 90 <= grade && grade <= 100 (note: 0 <= grade <= 100 won't work) • guests is even and is less than 100: guests % 2 == 0 && guests < 100 • myAge or brothersAge is greater than 21: myAge > 21 || brothersAge > 21
More Examples • age is not between 20 and 30, inclusive: !(20 <= age && age <= 30) Also, age < 20 || age > 30 would work. • a is bigger than b and bigger than c: a > b && a > c
Count-Controlled Loop A count-controlled loop can be used to repeat a section of code a variable number of times, if we know the value of the variable in advance: int n; cin >> n; for (int i = 0; i < n; i++) cout << “Hello, world!” << endl;
Common Pitfall Do not put a semicolon at the end of the loop statement: for(int i = 0; i < 10; i++); <code> The semicolon ends the for statement and the loop does nothing. The code following the loop will be done only once.
Semicolons in C++ • A semicolon changes an expression into a statement: a+b is an expression, a+b; is a statement (although it doesn't do anything). • This is commonly use with assignment. a = b + c is an assignment expression. a = b + c; is an assignment statement. • The semicolon by itself is the empty statement.
Nested for-loops • For-loops can be nested, that is, the code inside of one loop is another loop. • This is commonly used to process data in tables. • The two loops should have different counters and may have different tests as well.
Nested For-loop Example The following code prints out the mult. table: #include <iostream> #include <iomanip> using namespace std; int main(void) { for(int i = 1; i <= 10; i++) { for(int j = 1; j <= 10; j++) cout << setw(3) << i * j << " "; cout << endl; } }
Example Output 1 2 3 4 5 6 7 8 9 10 2 4 6 8 10 12 14 16 18 20 3 6 9 12 15 18 21 24 27 30 4 8 12 16 20 24 28 32 36 40 5 10 15 20 25 30 35 40 45 50 6 12 18 24 30 36 42 48 54 60 7 14 21 28 35 42 49 56 63 70 8 16 24 32 40 48 56 64 72 80 9 18 27 36 45 54 63 72 81 90 10 20 30 40 50 60 70 80 90 100
While-Statement • The while-statement can be used to create sentinel-controlled loops (those that read until a special value) and general-condition loops (those that loop until while some conditional is true) • The syntax is: while (<Boolean-expression>) <statement>
While-Statement Operation • When executing a while-statement, the test (Boolean-expression) is checked first. If it is true the statement is executed and the test is checked again. This continues until the test becomes false. • The statement should somehow change the value of the test. If it doesn't, and the test is initially true, we have an infinite loop.
While-Statement Example int n = 1; while (n < 2000) { cout << n << “ “; n = n * 2; } cout << endl; produces the output: 1 2 4 8 16 32 64 128 256 512 1024
Sentinel-Controlled Loop • A sentinel-controlled loop reads values in from the user and process them until a special (non-valid) value is read. • The loop then stops when the sentinel is read. • The sentinel is not processed like the data. • The sentinel can be a special value, such as a negative number (assuming data is not negative).
Sentinel-Controlled Loop Example To find the sum of a list of non-negative grades (ints), we could read until a negative number: int grade, sum = 0; cin >> grade; while (grade >= 0) { sum += grade; cin >> grade; } cout << sum << endl;
Exercise • Modify grades.cpp to print out the average grade. • The average is found by dividing the sum of the grades by the number of grades. • You will need to add a counter to count the number of grades read in. • Watch out for integer division.