Date post: | 15-Apr-2017 |
Category: |
Engineering |
Upload: | abhishek-choksi |
View: | 419 times |
Download: | 0 times |
Gandhinagar Institute Of Technology
Topic : While , For , Do-While LoopGuided By :
Branch : Batch :
SEJAL MA’AM
Active Learning Assignment
Electrical
CPU (2110003)
F1
Name ENROLLMENT NO. Abhishek Chokshi 140120109005 Raj Shah Kaveesh Raval
PREPARED BY:-
140120109054140120109016
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)
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.
While Syntax
Logical expression that determineswhether the action is to be executed
while ( Expression ) Action
Action to be iterativelyperformed until logical
expression is false
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
LOGIC OF A WHILE LOOP
statement
true false
conditionevaluated
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
THE 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.
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
LOGIC OF A DO-WHILE LOOP
true
conditionevaluated
statement
false
Basicsdo {statement-1;statement-2;……statement-k;
} while(condition);
Diagrammatic view
Initialize
Statement-1
Statement-2
Statement-k
Is conditionTRUE?
N
Y
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.
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.
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
Logic of a for loop
statement
true
conditionevaluated
false
increment
initialization
The for Statement A for loop is functionally equivalent to the
following while loop structure:initialization;while ( condition ){ statement; increment;}
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
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);}
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
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
Thank You