0% found this document useful (0 votes)
21 views

For Loop

This document discusses repetition structures in C++ programming such as while, do-while, and for loops. It provides examples of using each type of loop, including counter-controlled loops to iterate a certain number of times. It also discusses the continue statement, which skips the remaining code in the loop body and proceeds to the next iteration. Practice exercises are provided to help reinforce understanding of loops and nested loops.

Uploaded by

Nabiha Raza
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

For Loop

This document discusses repetition structures in C++ programming such as while, do-while, and for loops. It provides examples of using each type of loop, including counter-controlled loops to iterate a certain number of times. It also discusses the continue statement, which skips the remaining code in the loop body and proceeds to the next iteration. Practice exercises are provided to help reinforce understanding of loops and nested loops.

Uploaded by

Nabiha Raza
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 25

CS 102- Fundamentals of Computer Programming

Lecturer Rabia Khan

Lecture 5

CPS235: Introduction

Review
Selection structures
One-way and two-way selection using the if statement Multi-way selection using: Nested if statement the switch statement

Repetition structures using:


the while statement do while statement for statement

continue and break statements


2

Repetition Structures
There are 3 types of repetition structure:
1. while (LoopControlExpression)
LoopBody-statement;
/*end_while */

while

2. do
LoopBody-statement; while (LoopControlExpression); /* end_do_while */

do .. while

3. for (InitializationExp; LoopControlExp; UpdateExp)


LoopBody-statement; /*end_for */

for
3

do while Repetition Structure

Repetition: do while Statement


Syntax:
do LoopBody-statement; while (LoopControlExpression); /* end_do_while */
NO semicolon Semicolon is compulsory

The LoopBody-statement inside it will be executed once no matter what. Then only the condition will be checked to decide whether the loop should be executed again or just continue with the rest of the program.
5

Counter-controlled do while Statement : Example


#include<stdio.h> main ( ) { cout<<Hello World\n; cout<<Hello World\n; cout<<Hello World\n; cout<<Hello World\n; cout<<Hello World\n; } #include<stdio.h> main () { int num = 1; do { cout<<"Hello World\n"; num++; } while(num <=5); /*end_do_while */ }
6

Output?
#include<stdio.h> main () { int j = 10; while(j < 10) { cout<<J = <<j; j++; } /*end_while */ } }
CSEB134 : BS/2008 7

#include<stdio.h> main () { int j = 10; do { cout<<J =<<j; j++; } while(j < 10); /*end_do_while */

dowhile Statement : Example


#include<stdio.h> Value 999 will main () terminate the loop. { int num; cout<<"Enter an integer or 999:"; cin>>num; do { cout<<"Number is:<<num; cout<<"Enter an integer or 999:"; cin>>num; } while (num != 999); /* end_do_while */ }

for Repetition Structure

Repetition: for Statement


The syntax:
for (InitializationExp; LoopControlExp; UpdateExp) LoopBody-statement; /*end_for */
Semicolons are compulsory NO semicolon

InitializationExp = assign initial value to a loop control variable UpdateExp = change value of the loop control variable at the end of each loop LoopControlExp = the loop condition - to compute to True (1) or false (0)
10

Repetition: for Statement


In both the while and do loops, you usually don t know, at the time you enter the loop, how many times the loop will be executed. The condition that terminates the loop arises spontaneously inside the loop. In a For loop, the number of times the loop will be executed is (usually) stated at the beginning of the loop.
int j; // define the loop variable for(j=0; j<20; ++j); // cycle 20 times cout << '*'; // print asterisk

11

Counter-controlled for Statement : Example


#include<stdio.h> main () { int num; for (num=1;num<=5;num++) { cout<<"Hello World\n"; } /*end_for */ }
#include<stdio.h> main () { int num = 1; while(num <=5) { cout<<"Hello World\n"; num++; } /*end_while */ }
CSEB134 : BS/2008 12

Same output!

Repetition: for Statement


#include<stdio.h> main () { int num=1; Omit the InitializationExpression. Initial value of the variable num depends on what?

for (;num<=5;num++) { cout<<"Hello World\n"; } /*end_for */ }

13

Repetition: for Statement


#include<stdio.h> main () {
Missing the test expression?? Results into an infinite loop

for (int num=1;;num++) { cout<<"Hello World\n"; } /*end_for */ }


14

Repetition: for Statement


#include<stdio.h> #include <iostream.h> main () { for (int num=1;num<=5;) { cout<<"Hello World\n"; } /*end_for */ }
15

Missing the increment expression?? Results into an infinite loop!!

Repetition: for Statement


Missing the initialization #include<stdio.h> expression?? main () { int num; for (num<=5;num++) { cout<<"Hello World\n"; } /*end_for */ } Reports en error that For statement missing
16

Repetition: for Statement


#include <iostream.h> What happens here?? #include <conio.h> main() { for (;;) cout<<"*"; getch(); }

Nested for Statement : Example


#include<stdio.h> void main( ) { int i, j; for(i=0; i<6; i++) { cout<<"\n"; for(j=i+1; j<6; j++) cout<<j; } }

Output:
12345 2345 345 45 5

18

Example
#include<stdio.h> void main() { int i, j, x, y; cout<<Enter 2 integers in the range of 1-20:"; cin>>x>>y; for(i=1; i<=y; i++) { for(j=1; j<=x; j++) Output : Enter 2 integers in the range of 1-20:5 3 cout<<"*"; cout<<"\n"; ***** } }

***** *****

19

Practice Exercises
1. Modify program in the previous slide to get the following output:
a)
@@@@@ @@@@ @@@ @@ @

b) @@@@@
@@@@ @@@ @@ @

20

Practice Exercises
2. Write a program that keeps printing the multiples of the integers 2, namely 2, 4, 8, 16, 32, 64 and128. Your loop should terminate at 128. Use a for loop to implement this.

21

Practice Exercises
3. Write a calculator program that takes two (integer) numbers as input and allows the user to select which operation they would like to perform on the two numbers, either:
Addition Subtraction Multiplication Division

and display the result. Terminate the program when user enters n .
22

continue statement

continue statement
The continue statement, when executed in a while, for or do...while statement, skips the remaining statements in the body of that statement and proceeds with the next iteration of the loop. In while and do...while statements, the loopcontinuation test evaluates immediately after the continue statement executes. In the for statement, the increment expression executes, then the loop-continuation test evaluates.

Example
int main() { for ( int count = 1; count <= 10; count++ ) { if ( count == 5 ) continue; cout << count << " "; cout << "\nUsed continue to skip printing 5" << endl; } getch(); return 0; }

You might also like