Programming Fundamentals lec 9. x

hanzalaahmad787 6 views 15 slides May 20, 2025
Slide 1
Slide 1 of 15
Slide 1
1
Slide 2
2
Slide 3
3
Slide 4
4
Slide 5
5
Slide 6
6
Slide 7
7
Slide 8
8
Slide 9
9
Slide 10
10
Slide 11
11
Slide 12
12
Slide 13
13
Slide 14
14
Slide 15
15

About This Presentation

C++ fundamentals


Slide Content

Programming Fundamentals Lecture 12 Ms. Sidra

Agenda of Lecture Counter Controlled Statements Nested while For Loop

Counter-Controlled Repetition Essentials of Counter-Controlled Repetition the name of a control variable (or loop counter) the initial value of the control variable the loop-continuation condition that tests for the final value of the control variable (i.e., whether looping should continue) the increment (or decrement ) by which the control variable is modified each time through the loop.

4 while Condition Statement list T F while (Condition) { Statement list }

Counter-Controlled Repetition int main() { int counter = 1; // declare and initialize control variable while ( counter <=10 ) // loop-continuation condition { cout << counter << " "; counter++; } // end while cout << endl ; // output a newline r eturn 0; } // end main

Counter-Controlled Repetition int counter; // declare control variable counter = 1; // initialize control variable to 1 while (counter <= 10 ) // loop-continuation condition cout << counter << " "; counter++;

Example : while string ans = “ n ” ; while ( ans != “ Y ” && ans != “ y ” ) { cout << “ Would you help me with the assignment? ” ; cin >> ans ; } cout << “ Great!! ” ; (ans != “ Y ” || ans != “ y ” ) Can I put ; here? No!!

Example : while int a,b,sum; cout << “ This program will return the ” ; cout << “ summation of integers from a to b.\n\n ” ; cout << “ Input two integers a and b: ” ; cin >> a >> b; while (a <= b) { sum += a; a++; } cout << “ The sum is ” << sum << endl; sum = 0; sum = sum + a; Don’t forget to set sum = 0;

9 Nested while loop

10 Nested while loop int i =1; int j=1; int rows=8; while ( i <= rows) { while (j <= i ) { cout <<j; j++ ; } cout <<"\n"; i ++; j = 1; }

For Repetition Statements for ( Initialization_action ; Condition ; Condition_update ) { statement_list ; } 1 2 3 Specifies the counter-controlled repetition details in a single line of code . The general format for a loop

For Loop //Counter-controlled repetition with the for statement. int main() { // for statement header includes initialization, // loop-continuation condition and increment. for ( int counter = 1; counter <= 10; counter++ ) { cout << counter << " "; } cout << endl ; // output a newline r eturn 0; } // end main

13 Compare: for and while for ( Initialization_action ; Condition ; Condition_update ) { statement_list; } int n,f=1; cin >> n; for (i=2; i<=n; i++) { f *= i; } cout << “ The factorial of ” << n << “ is ” << f << “ . ” ; i=2; while (i<=n) { f *= i; i++; } 1 2 3 for ( Initialization_action ; Condition ; Condition_update ) { statement_list; }

Compare : for and while int n,i; n = 100; for (i=1; i <= n; i++) { statement_list; } int n,i; n = 100; i = 1; while (i <= n) { statement_list; i++; } v.s.

Questions??