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

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

View Full Document Right Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

View Full DocumentRight Arrow Icon

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

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

Unformatted text preview: 1 Chapter 4 C++ Control Structures 2 Control Structures if ... else statement while statement do- while statement for statement LOOPS 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. 4 if Statement Example # include <iostream> using namespace std; int main( void ) { int ave= , sum= , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != ) { 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 . 5 # include <iostream> using namespace std; int main( void ) { int ave= , sum= , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != ) { 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 . 6 if Statement Example # include <iostream> using namespace std; int main( void ) { int ave= , sum= , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; return ; } 7 # include <iostream> using namespace std; int main( void ) { int ave= , sum= , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; return ; } if Statement Example, Part 2 Since n = , the expression is false. The if block is skipped. Therefore, ave does not become sum/n , but instead stays . 8 # include <iostream> using namespace std; int main( void ) { int ave= , sum= , n; cout << "Enter sum : " ; cin >> sum; cout << endl << "Enter the count : " ; cin >> n; if ( n != ) { ave = sum / n; } cout << endl << "Average is : " << ave << endl; return ; } if Statement Example, Part 2 9 Alternative Methods if ( !n ) ave = . ; else ave = sum / n; if ( n = ) ave = . ; else ave = sum / n; if ( n != ) ave = sum / n; else ave = ; if ( n != ) ave = sum / n; The if statement in the previous program could have been implemented in a couple other ways... ...or... ...or... whats wrong here? ...or... 10 ifelse Example # include <iostream> using namespace std; int main( void ) { int failed= , passed= , 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 ; } Since score > 60, the expression is true, and the if block is executed. 11 # include <iostream> using namespace std;...
View Full Document

Page1 / 109

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

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