24
Gandhinagar Institute Of Technology opic : While , For , Do-While Loop Guided By : nch : Batch : SEJAL MA’AM Active Learning Assignment Electrical CPU (2110003) F1

While , For , Do-While Loop

Embed Size (px)

Citation preview

Page 1: While , For , Do-While Loop

Gandhinagar Institute Of Technology

Topic : While , For , Do-While LoopGuided By :

Branch : Batch :

SEJAL MA’AM

Active Learning Assignment

Electrical

CPU (2110003)

F1

Page 2: While , For , Do-While Loop

Name ENROLLMENT NO. Abhishek Chokshi 140120109005 Raj Shah Kaveesh Raval

PREPARED BY:-

140120109054140120109016

Page 3: While , For , Do-While Loop

Repetition Statements Repetition statements allow us to execute a

statement multiple times Often they are referred to as loops Like conditional statements, they are controlled

by boolean expressions There are three kinds of repetition statements:

◦ THE WHILE LOOP (EXIT CONTROLLED LOOP)◦ THE DO WHILE LOOP(EXIT CONTROLLED LOOP)◦ THE FOR LOOP (ENTRY CONTROLLED LOOP)

Page 4: While , For , Do-While Loop

The while Statement in C The syntax of while statement in C:while (loop repetition condition)

statement Loop repetition condition is the condition

which controls the loop. The statement is repeated as long as the

loop repetition condition is true. A loop is called an infinite loop if the loop

repetition condition is always true.

Page 5: While , For , Do-While Loop

While Syntax

Logical expression that determineswhether the action is to be executed

while ( Expression ) Action

Action to be iterativelyperformed until logical

expression is false

Page 6: While , For , Do-While Loop

The while Statement A while statement has the following

syntax:while ( condition ){ statement;}

• If the condition is true, the statement is executed• Then the condition is evaluated again, and if it is still true, the

statement is executed again• The statement is executed repeatedly until the condition becomes

false

Page 7: While , For , Do-While Loop

LOGIC OF A WHILE LOOP

statement

true false

conditionevaluated

Page 8: While , For , Do-While Loop

The while Statement An example of a while statement:

int count = 1;while (count <= 5){ System.out.println (count); count++;}

• If the condition of a while loop is false initially, the statement is never executed

• Therefore, the body of a while loop will execute zero or more times

Page 9: While , For , Do-While Loop

THE DO-WHILE LOOP

Page 10: While , For , Do-While Loop

The do-while Statement in C The syntax of the do-while statement

in C:do

statementwhile (loop repetition condition);

The statement is first executed. If the loop repetition condition is

satisfied, the statement is repeated else, the repetition of the loop is stopped.

Page 11: While , For , Do-While Loop

The Do-While Statement

Syntax do Action while (Expression)

Semantics◦ Execute Action◦ If Expression is true then

execute Action again◦ Repeat this process until

Expression evaluates to false

Action is either a single statement or a group of statements within curly braces

Action

true

false

Expression

Page 12: While , For , Do-While Loop

LOGIC OF A DO-WHILE LOOP

true

conditionevaluated

statement

false

Page 13: While , For , Do-While Loop

Basicsdo {statement-1;statement-2;……statement-k;

} while(condition);

Page 14: While , For , Do-While Loop

Diagrammatic view

Initialize

Statement-1

Statement-2

Statement-k

Is conditionTRUE?

N

Y

Page 15: While , For , Do-While Loop

An Example of the do-while Loop

/* Find an even number input */do{

printf(“Enter a value:\n”);scanf(“%d”,&num);

}while(num%2!=0)printf(“\n%d”,num);

This loop will repeat if the user inputs odd number.

Page 16: While , For , Do-While Loop

The for Statement in C The syntax of for statement in C:for (initialization expression;

loop repetition condition; update expression) statement

The initialization expression set the initial value of the loop control variable.

The loop repetition condition test the value of the loop control variable.

The update expression update the loop control variable.

Page 17: While , For , Do-While Loop

The for Statement A for statement has the following syntax:

for ( initialization ; condition ; increment ){ statement;}

The initializationis executed once

before the loop begins

The statement isexecuted until the

condition becomes false

The increment portion is executed at the end of each

iteration

Page 18: While , For , Do-While Loop

Logic of a for loop

statement

true

conditionevaluated

false

increment

initialization

Page 19: While , For , Do-While Loop

The for Statement A for loop is functionally equivalent to the

following while loop structure:initialization;while ( condition ){ statement; increment;}

Page 20: While , For , Do-While Loop

The for Statement An example of a for loop:

for (int count=1; count <= 5; count++){ System.out.println (count);}

• The initialization section can be used to declare a variable

• Like a while loop, the condition of a for loop is tested prior to executing the loop body

• Therefore, the body of a for loop will execute zero or more times

Page 21: While , For , Do-While Loop

The for Statement The increment section can perform any

calculation

• A for loop is well suited for executing statements a specific number of times that can be calculated or determined in advance

for (int num=100; num > 0; num -= 5){ System.out.println (num);}

Page 22: While , For , Do-While Loop

The for Statement Each expression in the header of a for loop is

optional If the initialization is left out, no initialization is

performed If the condition is left out, it is always considered

to be true, and therefore creates an infinite loop◦ We usually call this a “forever loop”

If the increment is left out, no increment operation is performed

Page 23: While , For , Do-While Loop

break revisited Remember the break keyword that we used

to stop a switch statement from executing more than one statement?

break can also be used to exit an infinite loop

But it is almost always best to use a well-written while loop

Page 24: While , For , Do-While Loop

Thank You