Chapter4 - Chapter 4 C+ Control Structures 1 Control...

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

View Full Document Right Arrow Icon
1 Chapter 4 C++ Control Structures
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 Control Structures if ... else statement while statement do - while statement for statement LOOPS
Background image of page 2
3 if Statement if ( condition ) { statement(s) } Executes statement(s) if condition is true. ————— or ————— if ( condition ) { statement(s)1 } else { statement(s)2 } Executes statement(s)1 if condition is true. Executes statement(s)2 if condition is not true.
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 if Statement Example # include <iostream> using namespace std; int main( void ) { int ave= 0 , sum= 0 , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != 0 ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; return 0; } Since n != 0 ( n is not zero), the expression is true, and ave = sum/n .
Background image of page 4
5 # include <iostream> using namespace std; int main( void ) { int ave= 0 , sum= 0 , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != 0 ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; return 0; } if Statement Example Since n != 0 ( n is not zero), the expression is true, and ave = sum/n .
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 if Statement Example # include <iostream> using namespace std; int main( void ) { int ave= 0 , sum= 0 , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != 0 ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; return 0 ; }
Background image of page 6
# include <iostream> using namespace std; int main( void ) { int ave= 0 , sum= 0 , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != 0 ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; } if Statement Example, Part 2 Since n = 0 , the expression is false. The if block is skipped. Therefore, ave does not become sum/n , but instead stays 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 # include <iostream> using namespace std; int main( void ) { int ave= 0 , sum= 0 , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != 0 ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; return 0 ; } if Statement Example, Part 2
Background image of page 8
Alternative Methods if ( !n ) ave = 0 . 0 ; else ave = sum / n; if ( n = 0 ) ave = 0 . 0 ; else ave = sum / n; if ( n != 0 ) ave = sum / n; else ave = 0 ; if ( n != 0 ) ave = sum / n; The if statement in the previous program could have been implemented in a couple other ways. .. ...or. .. ...or. .. what’s wrong here? ...or. ..
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 if…else Example # include <iostream> using namespace std; int main( void ) { int failed= 0 , passed= 0 , score; cout << "Enter the score : " ; cin >> score; if (score >= 60 ) { cout << endl << "This is a passing score" ; passed++; } else { cout << endl << "This is a failing score" ; failed++; } cout << endl << "The number of Passed students is : " << passed; cout << endl << "The number of Failed students is : " << failed << endl; return 0 ; } Since score > 60, the expression is true, and the if block is executed.
Background image of page 10
11 # include <iostream> using namespace std; int main( void ) { int failed= 0 , passed= 0 , score; cout << "Enter the score : " ; cin >> score; if (score >= 60 ) { cout << endl << "This is a passing score" ; passed++; } else { cout << endl << "This is a failing score" ; failed++; } cout << endl << "The number of Passed students is : " << passed; cout << endl << "The number of Failed students is : " << failed << endl; return 0 ; } if…else Example
Background image of page 11

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

View Full DocumentRight Arrow Icon
12 # include <iostream> using namespace std; int main( void ) { int failed= 0 , passed= 0 , score; cout << "Enter the score : " ; cin >> score; if (score >= 60 ) { cout << endl << "This is a passing score" ; passed++; } else { cout << endl << "This is a failing score" ; failed++; } cout << endl << "The number of Passed students is : " << passed; cout << endl << "The number of Failed students is : " << failed << endl; return 0 ; } Since score < 60, the expression is false, and the if block is skipped.
Background image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

This note was uploaded on 11/16/2011 for the course COP 2271 taught by Professor Staff during the Fall '11 term at University of Florida.

Page1 / 109

Chapter4 - Chapter 4 C+ Control Structures 1 Control...

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

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