Loops In C Programming Ppt



Programming

C program for bouncing ball graphics animation. In this program, we first draw a red color ball on screen having center at (x, y) and then erases it using cleardevice function. We again draw this ball at center (x, y + 5), or (x, y - 5) depending upon whether ball is moving down or up. This will look like a bouncing ball. The break & continue statements used inside a for, while and do.-while loops The loop does doe not terminate when a continue statement is encountered The switch statemen can only for test equality & Break used to terminate or exit from a switch statement The goto statement is used to transfer the control in a loop or function from one point to. A loop statement allows us to execute a statement or group of statements multiple times. Given below is the general form of a loop statement in most of the programming languages − C programming language provides the following types of loops to handle looping requirements. This website is designed for readers who have less or no programming experience. Even the experienced programmers will find this website equally useful. We have covered all the basic of C, C, C#, JAVA, VB.NET, ASP.NET, etc., programming language with easy examples and their descriptions.

A loop inside another loop is called a nested loop. The depth of nested loop depends on the complexity of a problem. We can have any number of nested loops as required. Consider a nested loop where the outer loop runs n times and consists of another loop inside it. The inner loop runs m times. Then, the total number of times the inner loop runs during the program execution is n*m.

Types of nested loops

Note: There can be mixed type of nested loop i.e. a for loop inside a while loop, or a while loop inside a do-while loop.

Nested while loop

Ppt

A while loop inside another while loop is called nested while loop.

Syntax of Nested while loop

Flowchart of Nested while loop

Example of Nested while loop

Example 1: C program to print the number pattern.

In this program, nested while loop is used to print the pattern. The outermost loop runs 5 times and for every loop, the innermost loop runs i times which is 1 at first, meaning only '1' is printed, then on the next loop it's 2 numbers printing '1 2' and so on till 5 iterations of the loop executes, printing '1 2 3 4 5'. This way, the given number pattern is printed.

Loops In C Programming Ppt Python

Loop

Nested do-while loop

A do-while loop inside another do-while loop is called nested do-while loop.

Syntax of Nested do-while loop

Flowchart of Nested do-while loop

Example of Nested do-while loop

Example 2: C program to print the given star pattern.

In this program, nested do-while loop is used to print the star pattern. The outermost loop runs 5 times and for every loop, the innermost loop runs i times which is 1 at first, meaning only one '*' is printed, then on the next loop it's 2 printing two stars and so on till 5 iterations of the loop executes, printing five stars. This way, the given star pattern is printed.

Nested for loop

A for loop inside another for loop is called nested for loop.

Syntax of Nested for loop

Flowchart of Nested for loop

Example of Nested for loop

Loops In C Programming Ppt Examples

Example 3: C program to print all the composite numbers from 2 to a certain number entered by user.

Output

A number is said to be composite if it has at least one factor other than 1 and itself. This program prints all the composite numbers starting from 2 to a certain number n, entered by user. We need to use a nested loop to solve this problem. The outer for loop runs from 2 to n and the inner loop is used to determine whether a number is composite or not. We need to check for that factor starting from 2 to integer part of square root of that number.

Consider 15, its square root is nearly 3.873. Here, the integer part is 3. Now, if there is a factor of 15 from 2 to 3 then it is composite. Here, 3 is a factor of 15. Hence, 15 is a composite number.

Loops In C Programming Ppt Presentations

Submitted by Sagun Shrestha