0% found this document useful (0 votes)
37 views18 pages

4 Iteration Structure For While DoWhile Loops 1

Uploaded by

Talha Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
37 views18 pages

4 Iteration Structure For While DoWhile Loops 1

Uploaded by

Talha Irfan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Programming Fundamentals

Control Structure
Iteration/Repition Structure
Books

 Deitel & Deitel :– C++ How to Program


 Robert Lafore : Object-Oriented Programming in C++
 IT Series : Object Oriented Programming Using C++
Control Structures

 Control Structures are just a way to specify flow of control in programs.


 Any algorithm or program can be more clear and understood if they
use self-contained modules called as logic or control structures.
 It basically analyzes and chooses in which direction a program flows
based on certain parameters or conditions.
 There are three basic types of logic, or flow of control, known as:
1. Sequence logic, or sequential flow
2. Selection logic, or conditional flow

3. Iteration logic, or repetitive flow


4. Function Call
Loops

 Loops in programming come into use when we need to repeatedly


execute a block of statements.
 In computer programming, a loop is a sequence of instructions that is
repeated until a certain condition is reached.
 An operation is done, such as getting an item of data and changing it, and
then some condition is checked such as whether a counter has reached a
prescribed number.
 Counter not Reached: If the counter has not reached the desired number,
the next instruction in the sequence returns to the first instruction in the
sequence and repeat it.
 Counter reached: If the condition has been reached, the next instruction
“falls through” to the next sequential instruction or branches outside the
loop.
Types of Loops

 There are mainly two types of loops:

 Entry Controlled loops: In this type of loops the test condition is


tested before entering the loop body. For Loop and While Loop are
entry controlled loops.
 Exit Controlled Loops: In this type of loops the test condition is
tested or evaluated at the end of loop body. Therefore, the loop
body will execute atleast once, irrespective of whether the test
condition is true or false. do – while loop is exit controlled loop.
Types of Loops
For Loop
 for Loop
 A for loop is a repetition control structure which allows us to
write a loop that is executed a specific number of times.
The loop enables us to perform n number of steps together
in one line.

 for (initialization expr ; test expr ; update expr )


 {
// body of the loop // statements we want to execute
}
While - Loop

 In For Loop we have seen that the number of iterations is known


beforehand,
 i.e. the number of times the loop body is needed to be executed is known to
us.
 The while loop in C/C++ is used in situations where we do not know
the exact number of iterations of loop beforehand.
 The loop execution is terminated on the basis of the test condition.
Do-While Loop

 In computer programming, loops are used to repeat


a block of code.
 The do-while loop is a variant of the while loop with
one important difference: the body of do while loop is
executed once before the condition is checked.
 The body of the loop is executed at first. Then
the condition is evaluated.
 If the condition evaluates to true, the body of the
loop inside the do statement is executed again.
 This process continues until the condition evaluates
to false.
 Note: In do while loop the loop body will execute at
least once irrespective of test condition.
Nested Loop

 Nested loop means a loop statement inside another loop statement.


 Nested loops are also called as “loop inside loop“.
Practice Work

 Write a program that displays first 5 numbers and their square using
iteration structure.
 Write a program that input a number from the user and display the
table of that number from 1 to 10.
 Write a program that input a number and displays its factorial.
 Write a program that sum all the odd number between 1 to 100.
 Write a program that input a number and displays weather it’s a
prime number or not.(1,3,5,7,13,17,19…..)
Practice Work

 Write a program that input a number and displays wether it’s a palindrome
or not.
 A number is palindrome if the reverse of that number is equal to the original number.

 Write a program that input a number and check weather it’s an Armstrong
number or not.
 Hint: a number is an Armstrong Number if the sum of cubes of its digits is equal to the
number itself. For e.g 371 is armstrong number as 33 + 73 + 13 = 371
 Write a program that input a number from the user and displays n Fibonacci terms.
In Fibonacci sequence sum of two successive terms gives the third term.
 For example : 6 Fibonacci terms are … 0 1 1 2 3 5
Practical Work – Nested Loop

 Write a program displays factorial of all number between 1 to 50.


 Write a program that displays first 50 Prime numbers between starting from 1.
Practical Work – Nested Loop

 Write a program in C++ to  Write a program in C++ to  Write a program in C++ to


display the pattern like right display the pattern like right display the pattern like right
angle triangle using an angle triangle with Numbers.
angle triangle with numbers
asterisk.
increase by 1.
Sample Output: Sample Output: Sample Output:

Please Enter number of rows: 5 Please Enter number of rows: 5


Please Enter number of rows: 4

* 1
** 1 2 1
2 3
*** 1 2 3
**** 1 2 3 4 4 5 6
***** 7 8 9 10
1 2 3 4 5
 Evening Students Class will Start at 2:10

 Quiz from Conditional Structure and Loops


On Tuesday -- Both Morning and Eveing

You might also like