lect04_Condition

lect04_Condition - Simple Flow of Control Simple Flow of...

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

View Full Document Right Arrow Icon
Simple Flow of Control SIS1117 Computer Programming CS S Co pute og a g 1 c1117 lecture 4
Background image of page 1

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

View Full DocumentRight Arrow Icon
ontents Contents election control Selection control If -statement Data type -- bool If/else statement Dangling else ogical operators Logical operators Short-circuit evaluation c1117 lecture 4 2
Background image of page 2
tatements Statements o far we have learned to write mple programs So far, we have learned to write simple programs , which can interact with users. owever in many cases we want some However, in many cases, we want some statements to be executed only if certain criteria re tisfied are satisfied . e.g. we need to check if y is zero to prevent the run me error time error. int x, y; We will have n runtime cin >> x >> y; cout << x/y << endl; an runtime error if y=0 c1117 lecture 4 3
Background image of page 3

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

View Full DocumentRight Arrow Icon
election control Selection control he problem can be solved by attaching a st The problem can be solved by attaching a test expression ( condition ). If the condition is true , e tatement executed Otherwise the the statement is executed. Otherwise, the is skipped. est expression int x, y; cin >> x >> y; Test expression Only execute if the if( y != 0) cout << x/y << endl; f( y == 0) test expression is true if( y == 0) cout << "The dividend can’t be zero" << endl; c1117 lecture 4 4 not equal equal
Background image of page 4
- tatement if statement if( test expression ) body ; We can use an if-statement to determine whether rtain code is required to execute certain code is required to execute. An if-statement consists of a test expression that evaluates to true or false and the body . The body (consequence) will be executed only if the test expression is true . See cal-div.cc as an example c1117 lecture 4 5
Background image of page 5

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

View Full DocumentRight Arrow Icon
ata type -- ool Data type bool e e discussed the data type nt ouble and We ve discussed the data type int , double , and string , they use to represent different values . nt 7 1000 etc int : 7, 3, 1000, etc … double : 1.2, -3.45, 0.012, etc … tring "Peter "" Hello World!" etc string : Peter , Hello World! , etc … bool only for storing true or false value. Test expression has the value of type bool . bool isZero = (y == 0); c1117 lecture 4 6
Background image of page 6
st expression usually formed by comparing A test expression is usually formed by comparing two numbers with Relational Operator . g a>b a>=b a<=b a<b a==b a!=b e.g. a>b, a>=b, a<=b, a<b, a==b, a!=b != means == means bool isZero = (y == 0); NOT equal equal (y ); if(isZero) out << The dividend can’t be zero" cout << The dividend can t be zero << endl; Equal to: c1117 lecture 4 7 if(isZero == true )
Background image of page 7

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

View Full DocumentRight Arrow Icon
lock statement Block statement uppose now we want to print two lines if zero Suppose now we want to print two lines if y is zero.
Background image of page 8
Image of page 9
This is the end of the preview. Sign up to access the rest of the document.

This document was uploaded on 05/04/2011.

Page1 / 29

lect04_Condition - Simple Flow of Control Simple Flow of...

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