{[ promptMessage ]}

Bookmark it

{[ promptMessage ]}

CS201-06 - In the last lecture Conditional Construct if...

Info icon This preview shows page 1. Sign up to view the full content.

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

Unformatted text preview: In the last lecture Conditional Construct if if-else Loop ­ Repetition structure Example int sum ; sum = 1+2+3+4+5+……..+10 ; cout << sum ; Find the Sum of the first 100 Integer starting from 1 ? while while ( Logical Expression ) { statements; : } int sum ; sum = 0 ; int sum = 0; ( Optional ) Example int sum , number ; sum = 0 ; number = 1 ; while ( number <= 1000 ) { sum = sum + number ; number = number + 1 ; } cout << “ The sum of the first 1000 integer starting from 1 is ” << sum ; while (number <= UpperLimit) Example int sum, number , UpperLimit ; sum = 0 ; number = 1 ; cout << “ Please enter the upper limit for which you want the sum ” ; cin >> UpperLimi t; while (number <= UpperLimit) { sum = sum + number ; number = number +1 ; } cout << “ The sum of the first ” << UpperLimit << “ integer is ” << sum ; if ( number % 2 == 0 ) { sum = sum + number ; number = number + 1 ; } Example sum = 0; number = 1; cout << “ Please enter the upper limit for which you want the sum ”; cin >> UpperLimit; while (number <= UpperLimit) { if (number % 2 == 0) { sum = sum + number; number = number + 1; } } cout << “ The sum of all even integer between 1 and ” << UpperLimit << “ is” << sum; 2 * ( number / 2 ) ; ? int Junk ; Junk = 1 ; while ( Junk <= UpperLimit ) ( infinite loop ) { sum = sum + number ; number = number + 1 ; } X Flow Chart for While Construct WHILE Statement Entry point for WHILE block While Condition is No true? Exit Process Exit point for WHILE block Factorial Definition n! = n*(n­1)*(n­2)*(n­3)…………*3*2*1 Example: Factorial #include <iostream.h> main ( ) { int number ; int factorial ; factorial = 1 ; cout << “Enter the number of Factorial” ; cin >> number ; while ( number >= 1 ) { factorial = factorial * number ; number = number – 1 ; } cout << “Factorial is” << factorial ; } Property of While Statement It executes zero or more times ...
View Full 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