Lecture5 - ECE 3090 Software for Engineering Systems Prof....

Info iconThis preview shows pages 1–9. Sign up to view the full content.

View Full Document Right Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon

Info iconThis preview has intentionally blurred sections. Sign up to view the full version.

View Full DocumentRight Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: ECE 3090 Software for Engineering Systems Prof. Hongwei Wu School of Electrical and Computer Engineering Georgia Institute of Technology (Savannah) Todays topic: Looping The while Loop The do while Loop The for Loop Applications of loops Input validation Menu Reading data from a file When to use which loop Nested loops break and continue ; Increment and Decrement Operators, ++ and -- Add or subtract 1 from their operands. Operand are usually variables, i.e., anything that can be put on the lhs of =. Postfix mode index ++; counter --; Prefix mode ++ index; -- counter; Difference between the postfix and prefix modes: order of the execution when ++ and are jointly used with other operators. Output? In Memory? int number = 4; cout << number ++; int number = 4; cout << ++ number; Example: 1. #include <iostream> 2. using namespace std; 3. 4. int main() 5. { 6. int num = 4; 7. 8. cout << num << endl; // Displays 4 9. cout << num ++ << endl; // Displays 4, and adds 1 to num 10. cout << num << endl; // Displays 5 11. cout << ++ num << endl; // Adds 1 to num, and displays 6 12. 13. cout << num << endl; // Displays 6 14. cout << num -- << endl; // Displays 6, and subtracts 1 from num 15. cout << num << endl; // Displays 5 16. cout << --num << endl; // Subtracts 1 from num, and displays 4 17. 18. return 0; 19. } More examples of increment an decrement operators a = 2; b = 5; c = a * b++; c = a * ++b; c = ++(a * b); c = (a * b)++; x = 10; cout << (x++ > 10) << endl; x = 10; cout << (++x > 10) << endl; c = 10; b = 6; c = 12; b = 6; Error ! Illegal statement. Error ! Illegal statement. false; x = 11; true; x = 11; The while loop A loop is a control structure that causes a statement of a group of statements to be repeated. The while loop contains two parts Relational expression A statement or a group of statements to be repeated as long as the relational expression is true. The while loop is a pretest loop: tests the relational expression first before each iteration (which is different than the do-while loop). Statements Relational Expression Yes No Example 1. int main() 2. { 3. int number = 1; 4. while ( number <= 5) 5. { 6. cout << "Hello\n"; 7. number ++; 8. } 9. cout << "That's all!\n"; 10. return 0; 11. } Program Output Hello Hello Hello Hello Hello Thats all! It is important to use number as a counter to avoid infinite loops. What is a counter? A counter is a variable that is regularly incremented or decremented each time a loop iterates. Can be used to control execution of the loop (also known as the loop control variable ) It is also important to initialize the counter before it is used....
View Full Document

Page1 / 28

Lecture5 - ECE 3090 Software for Engineering Systems Prof....

This preview shows document pages 1 - 9. Sign up to view the full document.

View Full Document Right Arrow Icon
Ask a homework question - tutors are online