{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

lec03-flow_control

# lec03-flow_control - CSC 1111 Introduction to Computing...

This preview shows pages 1–12. Sign up to view the full content.

1 CSC 1111 Introduction to Computing using C++ Basics of Flow Controls

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

View Full Document
2 Introduction Flow of controls – the order in which instructions are executed in a program Flow of controls can be fully expressed using three kinds of controls Sequential – Execute the instructions one after another Selective – Conditionally execute the instructions Repetitive – Repeatedly execute the instructions
3 Outlines Relational and equality operators Selective control structures if statement if-else statement Logical operators Repetitive control structure: while statement

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

View Full Document
4 Equality and Relational Operators Operators Examples Meanings Equality == x == y x is equal to y != x != 10 x is not equal to 10 Relational > x > 3 x is greater than 3 < y < 0 y is less than 0 >= x >= y x is greater than or equal to y <= y <= -4 y is less than or equal to -4
5 Results of Comparison The result of comparison is either true or false . Suppose x and y are declared as int x = 0, y = 100; x > 10 false 'A' != 'B' true y >= (x + 100) true 100 == (x + y) true

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

View Full Document
6 Operator precedence and associativity Operator Associativity Precedence () ++ (postfix) -- (postfix) left to right Highest + (unary) - (unary) ++ (prefix) -- (prefix) right to left * / % left to right + - left to right < <= > >= left to right == != left to right && left to right || left to right ?: right to left = += -= *= /= etc. right to left , (comma operator) left to right Lowest
7 Integer values and Boolean Data Type true , when treated as integer, is equal to 1 false , when treated as integer, is equal to 0 Any non-zero value is treated as true value. Zero is treated as false . cout prints true as 1 and false as 0 e.g. bool foo = 2; // foo is true cout << foo; // prints 1 cout << false; // prints 0

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

View Full Document
8 if Statement (syntax) if ( expr ) statement next_statement; expr next_statement statement true false Allows us to conditionally perform a task
9 if Statement int score; cout << "Please enter your score: " ; cin >> score; if (score >= 60 ) cout << "Passed!\n" ; if (score < 60 ) cout << "Failed!\n" ; cout << "Your score is " << score; 1 2 3 4 5 6 7 8 9 Please enter your score: 80 Passed! Your score is 80. Please enter your score: 40 Failed! Your score is 40.

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

View Full Document
10 What's the output if x is … ? if (x % 10 == 0 ) cout << "A" ; cout << "B" ; cout << "C" ; 1 2 3 4 Indentation – adding spaces at the beginning of a line to align codes. Indenting codes does not affect the programs. It only makes programs easier to read. if (x % 10 == 0 ) cout << "A" ; cout << "B" ; cout << "C" ; Same as
11 Compound Statement if (score >= 60 ) { grade = 'P' ; cout << "Congratulations!\n" ; } if (score < 60 ) grade = 'F' ; cout << "Your score is " << score << endl; cout << "Your grade is " << grade << endl; 1 2 3 4 5 6 7 8 9 { … } groups multiple statements into ONE compound statement.

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

View Full Document
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}