Topic Covered: Introduction to Loops , while loop+ programs, For loop + do while loop +programs, Programming practice using loops. Lecture: 8
Introduction of loop Loop is used to execute the block of code several times according to the condition given in the loop. It means it executes the same code multiple times. In programming, loops are used to repeat a block of code until a specified condition is met. Output: Hello Hello Hello Hello Hello loop(condition) { //statements }
Types of loop
While Loop while is an entry control loop. Statements inside the body of while are repeatedly executed till the condition is true. while is keyword. The syntax of the while loop is: while (testExpression) {
// the body of the loop
}
How while loop works?
How while loop works?
Write A Program to print 1 to n using While loop?
Write A Program to print Odd numbers between 1 to n using while loop.
WAP to print multiplication table using while loop.
WAP to Sum of 5 numbers entered by user using while loop.
WAP to find factors of a number using while loop.
WAP to print reverse a number while loop.
for loop for is an entry control loop. Statements inside the body of for are repeatedly executed till the condition is true for is keyword. The syntax of the for loop is: for (initialization; condition; updateStatement) { // statements }
How for loop works?
How for loop works? T he initialization statement is executed only once. Then, the test expression is evaluated. If the test expression is evaluated to false, the for loop is terminated. However, if the test expression is evaluated to true, statements inside the body of the for loop are executed, and the update expression is updated. Again the test expression is evaluated. This process goes on until the test expression is false. When the test expression is false, the loop terminates.
WAP to print numbers 1 to n using for loop.
do while loop do while is an exit control loop. Statements inside the body of do while are repeatedly executed till the condition is true. Do and while are keywords. The do.. while loop is similar to the while loop with one important difference. The body of do...while loop is executed at least once. Only then, the test expression is evaluated. The syntax of the do...while loop is: do {
// the body of the loop
}
while ( test_Expression );
How do...while loop works? The body of do...while loop is executed once. Only then, the test_Expression is evaluated. If test_Expression is true , the body of the loop is executed again and test_Expression is evaluated once more. This process goes on until test_Expression becomes false . If test_Expression is false , the loop ends.
WAP to print Odd numbers between 1 to n using do while loop.
WAP to find factors of a number using do while loop.