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

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

View Full Document Right Arrow Icon
1 Chapter 4 C++ Control Structures
Image of page 1

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

View Full Document Right Arrow Icon
2 Control Structures if ... else statement while statement do - while statement for statement LOOPS
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.
Image of page 3

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

View Full Document Right 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 .
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 .
Image of page 5

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

View Full Document Right 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 ; }
Image of page 6
7 # 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 Since n = 0 , the expression is false. The if block is skipped. Therefore, ave does not become sum/n , but instead stays 0 .
Image of page 7

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

View Full Document Right 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
Image of page 8
9 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...
Image of page 9

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

View Full Document Right Arrow Icon
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.
Image of page 10
# 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
Image of page 11

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

View Full Document Right Arrow Icon
# 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.
Image of page 12
Image of page 13
This is the end of the preview. Sign up to access the rest of the document.

{[ snackBarMessage ]}

What students are saying

  • Left Quote Icon

    As a current student on this bumpy collegiate pathway, I stumbled upon Course Hero, where I can find study resources for nearly all my courses, get online help from tutors 24/7, and even share my old projects, papers, and lecture notes with other students.

    Student Picture

    Kiran Temple University Fox School of Business ‘17, Course Hero Intern

  • Left Quote Icon

    I cannot even describe how much Course Hero helped me this summer. It’s truly become something I can always rely on and help me. In the end, I was not only able to survive summer classes, but I was able to thrive thanks to Course Hero.

    Student Picture

    Dana University of Pennsylvania ‘17, Course Hero Intern

  • Left Quote Icon

    The ability to access any university’s resources through Course Hero proved invaluable in my case. I was behind on Tulane coursework and actually used UCLA’s materials to help me move forward and get everything together on time.

    Student Picture

    Jill Tulane University ‘16, Course Hero Intern