Docsity

Understanding Conditional Statements and Loops in C++: A Comprehensive Guide, Slides of Computer Programming

Partial preview of the text

Download Understanding Conditional Statements and Loops in C++: A Comprehensive Guide and more Slides Computer Programming in PDF only on Docsity!

Conditional Construct

In the last lecture

 if

 if-else

Loop - Repetition structure

  • Find the Sum of the first
    • Integer starting from

while

int sum ; sum = 0 ;

int sum = 0; ( Optional )

while (number <= UpperLimit)

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 ; 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; Example

2 * ( number / 2 ) ;

Flow Chart for While Construct

Condition is While true? WHILE Statement Entry point for WHILE block Exit point for WHILE block Process Exit No

Factorial Definition

n! = n(n-1)(n-2)(n-3)…………32

Property of While

Statement

It executes zero or more times