Title | EE273 - lecture 03-long |
---|---|
Author | James Smythe |
Course | Engineering Design For Software Development 2 |
Institution | University of Strathclyde |
Pages | 30 |
File Size | 930.2 KB |
File Type | |
Total Downloads | 54 |
Total Views | 135 |
Lecture Notes for 3rd lecture...
EE273/985
Section 2: Conditions, loops
Dr David Harle Dr Christos Tachtatzis EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
1
Structure
o Introduction to program control
Conditional controls if, if … else, ?:, switch-case
Loop controls for, while, do … while, if … break, if … continue
o Typesetting of source code • • •
Use of indentation to keep track of nested conditions and loops Use of whitespace to group actions together Use of comments
EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
2
The need for program control
Not all C++ programs execute from the first line, linearly to the last,
In most programs, the program control path through the code depends on the values of variables at different points in the program
Most involve repetition, skipping and conditional branching
The values of variables can be changed as the program proceeds
Conditional statements can be used to selectively execute either:
A single line of code, Braces { } not required around the executable statement
Multiple lines of related code (called a block) Braces { } REQUIRED around the executable statement
EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
3
An algorithm to make a hot drink Start Kettle water level == 0?
IF statement
Yes Fill kettle
No Heat kettle Yes
A LOOP
Test condition for the loop (whether or not to do an iteration)
Water temp < 100? No
a SWITCH
Tea
Put teabag in mug Yes Add milk Yes Extract teabag
Want tea or coffee?
Want milk? No
Put instant coffee in mug
IF statement
Teabag in mug? No Drink!
EE273/985 – Engineering For Software Design
Coffee
IF statement
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
4
Conditionals: if and if … else
The if keyword is used to test a given expression for a value of true or false
if the result is true, then a specified series of statements is executed
the series of statements is specified within braces after the test the execution of these statements is conditional on the result of the test
otherwise, those conditional statements are skipped
The else keyword is used with an if statement to specify an alternative path to be executed when the test expression is false
Syntax: if (test-expression) { code-to-be-executed-when-true; }
EE273/985 – Engineering For Software Design
Each statement ends with a semi colon!!
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
5
Conditional if statement Example program section: int main(int argc, char* argv[]) { int x=0, y=0; cin >> x ; cin >> y ; if ( x > y ) cout y\n“ ; return 0; } EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
6
Nested if statement o if statements can be nested inside other if blocks to express multiple tests, e.g. x < y, x == y, x != y o Indentation applied for nested if blocks to keep track of nested blocks if (test1_expression) { if (test2_expression) { test2_true_action; }
} EE273/985 – Engineering For Software Design
Indentation
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
7
if-else statement o This is called ‘conditional branching’ because the program will choose a certain route depending on the test result. The syntax: if (test-expression) { test_true_action1; test_true_action2; test_true_action3; } else test_false_action;
EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
8
if-else statement o Example: int main(int argc, char* argv[]) { int int1; cin >> int1; if ( int1 > 1 ) cout 1\n “ ; else cout 1\n “;
Indentation
return 0; } EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
9
The ?: Conditional operator Unique to C/C++ The ?: conditional operator provides a quick way to write a test condition. Associated actions are performed depending on whether the test_expression evaluates to true or false. The operator can be used to replace an equivalent if-else statement. Syntax: test_expression ? true_action : false_action ;
o Using example in previous slide: (int1 > 1) ? cout 1\n “ : cout 1\n “;
EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
10
switch statement o Multiple if-else statements •
conditional branching can often be more efficiently programmed by using a switch statement
o switch uses a given integer/string/float… value then finds a matching value among a series of case statements. o default instructions can be specified for the case of no match being found. o Each case statement ends with a break instruction •
causes the remaining set of switch statements to be skipped.
EE273/985 – Engineering For Software Design
©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
11
switch statement Syntax switch (integral_expression) { case constant1:
No need for braces. Statements execute until break is encountered.
statements1; break; case constant2: statements2; break; default: statements; } EE273/985 – Engineering For Software Design
Never forget to include break; If integral_expression is neither constant1 nor constant2, execute the following statements ©University of Strathclyde & C. Tachtatzis, D. Harle, K.Bell
12
switch statement Example: int main(int argc, char* argv[]) Try choice of hot drink { example, e.g. hot drink int int1; dispensing machine cout > int1; switch (int1) { case 1 : cout...