Loops allow a set of instructions to be repeated until a certain condition is reached. There are three common types of loops: for loops, while loops, and do-while loops. Loops provide an efficient way to repeat code without manually writing out repetitive statements, making code more concise and elegant. They constitute a basic and powerful programming concept.
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 ratings0% found this document useful (0 votes)
56 views14 pages
Loops
Loops allow a set of instructions to be repeated until a certain condition is reached. There are three common types of loops: for loops, while loops, and do-while loops. Loops provide an efficient way to repeat code without manually writing out repetitive statements, making code more concise and elegant. They constitute a basic and powerful programming concept.
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/ 14
What are Loops?
Loops allow a set of instructions to be performed
until a certain condition is reached.
A loop is a series of instructions that is repeated until a certain condition is met.
Each instruction that passes through the loop is called an iteration.
Loops constitute one of the most basic and powerful programming concepts. Loops
Kinds of Loops:
1) for loop 2) while loop 3) do - while loop What are Loops? Importance of Loops
#include <stdio.h> main() { printf(TUP\n"); printf(TUP\n"); printf(TUP\n"); printf(TUP\n"); printf(TUP\n"); } What are Loops? The program shown before is indeed enough to solve the problem. However, if you were asked to do the same program except this time you print TUP 100 times. Are you willing to type printf() 100 times? (Excluding copy and paste)
What if you were asked to print TUP 1000, 10000, 100000 times or what ever it would take you to realize that this isnt a sensible method.
What we are after here is elegance of the code. Doing the job in an efficient manner. What are Loops?
#include<stdio.h> main() { int ctr; for(ctr = 1; ctr <= 5; ++ctr) { printf(TUP\n); } } while Loops The program shown before is indeed enough to solve the problem. However, if you were asked to do the same program except this time you print TUP 100 times. Are you willing to type printf() 100 times? (Excluding copy and paste)
What if you were asked to print TUP 1000, 10000, 100000 times or what ever it would take you to realize that this isnt a sensible method.
What we are after here is elegance of the code. Doing the job in an efficient manner. while Loops Syntax: while(condition) { statement1; statement2; statementn; } while Loops Example: This program will print out numbers from 1-10. #include<stdio.> main() { int ctr; ctr=1; while(ctr<=10){ printf(\n%d,ctr); ++ctr; } }
Analyze: ctr=1; while(ctr<=10) { printf(\n%d,ctr); ++ctr; } do-while Loops Syntax: do { statement1; statement2; statementn; }while(condition); do-while Loops Example: This program will print numbers from 1 - 10. #include<stdio.h> main() { int ctr; ctr=1; do{ printf(\n%d,ctr); ++ctr; }while(ctr<=10); } Analyze: ctr=1; do { printf(\n%d,ctr); ++ctr; }while(ctr<=10); for Loops Syntax: for(initialization;condition;increment) { statement1 ; statement2; statementn; } for Loops Sample program: Create a C program that will print all even numbers from 0 to 100. #include<stdio.h> main() { int ctr; for(ctr=0;ctr<=100; ctr += 2) { printf(\n%d,ctr); } } Analyze: for(ctr=0;ctr<=100;ctr += 2) Loops EXERCISE #11 Create three programs using the following conditions that will print numbers from 1 to 10. x < ___ x <= ___ x != ___
Initialize the value of x ASSIGNMENT 2 Create a program that will accept input from the user and display it. The program will terminate only if the user entered 0. ASSIGNMENT 3 Using the following idea of modulus, create a program that will accept a user input and determine if the number is ODD or EVEN: 2 % 2 = 0 3 % 2 = 1 4 % 2 = 0 5 % 2 = 1 Use the condition: x % 2 = = 0 or x % 2 != 0