lec03-flow_control

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

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

View Full Document Right Arrow Icon
1 CSC 1111 Introduction to Computing using C++ Basics of Flow Controls
Background image of page 1

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

View Full DocumentRight Arrow Icon
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
Background image of page 2
3 Outlines Relational and equality operators Selective control structures if statement if-else statement Logical operators Repetitive control structure: while statement
Background image of page 3

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

View Full DocumentRight Arrow Icon
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
Background image of page 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
Background image of page 5

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

View Full DocumentRight Arrow Icon
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
Background image of page 6
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
Background image of page 7

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

View Full DocumentRight Arrow Icon
8 if Statement (syntax) if ( expr ) statement next_statement; expr next_statement statement true false Allows us to conditionally perform a task
Background image of page 8
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.
Background image of page 9

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

View Full DocumentRight Arrow Icon
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
Background image of page 10
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. No need semicolon (
Background image of page 11

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

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

Page1 / 42

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

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

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