0% found this document useful (0 votes)
59 views

Introduction To Loops

The document discusses different types of looping constructs in C including for, while, and do-while loops. It provides the syntax and working of each loop type with examples. The key looping constructs covered are: - For loop: Includes initialization, termination, and update statements. Common uses and variations are demonstrated. - While loop: Evaluates a test expression and repeats the loop body until it is false. Syntax and example are shown. - Do-while loop: Executes the body at least once and then checks the condition, repeating until it is false. Syntax and example given. - Nested loops are demonstrated using a matrix example. The continue statement and practice problems on loops are also briefly discussed

Uploaded by

srimathi.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
59 views

Introduction To Loops

The document discusses different types of looping constructs in C including for, while, and do-while loops. It provides the syntax and working of each loop type with examples. The key looping constructs covered are: - For loop: Includes initialization, termination, and update statements. Common uses and variations are demonstrated. - While loop: Evaluates a test expression and repeats the loop body until it is false. Syntax and example are shown. - Do-while loop: Executes the body at least once and then checks the condition, repeating until it is false. Syntax and example given. - Nested loops are demonstrated using a matrix example. The continue statement and practice problems on loops are also briefly discussed

Uploaded by

srimathi.r
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 24

LOOPING CONSTRUCTS

FOR EMBEDDED C

By
Dr. H. Parveen Sultana
Professor/SCOPE,
VIT,Vellore 19/12/2023
TYPES OF LOOPING CONSTRUCTS

1. For Loop
2. While
3. Do-While
FOR LOOP SYNTAX
for (statement 1; statement 2; statement 3) {
// code block to be executed }
Statement 1 - executed one time before the execution of the code block.

Statement 2 - the condition for executing the code block.


Statement 3 - executed every time after the code block has been executed.
How for loop works?

• The initialization statement is executed only once.


• Then, the condition is evaluated. If the test expression is false, the for loop is terminated.
• If the condition is evaluated to true, statements inside the body of the for loop are executed,
And then the expression is updated.

• Again the condition is evaluated.


• This process goes on until the condition is false. When the condition is false, the loop
terminates.
EXAMPLE

int i;

for (i = 0; i < 5; i++) {


printf("%3d", i);

}
Output : 0 1 2 3 4
FOR LOOP – TYPE1
int main() Note: return 0 is implicit at the
end of the programs, please
{
ignore that.
int lower_limit = 0;
int upper_limit = 100;
int count;
Initialization Termination Update
for(count = lower_limit; count < upper_limit; count ++) {
printf("%d, ",count); }
printf("\n");
}
Output
FOR LOOP- TYPE2

Initialization
Termination Update

Initialization
replaced
with a
semicolon
FOR LOOP - TYPE3
Note: Termination
statement cannot be
omitted from the for
loop.

Initialization

Termination
Initialization Omitted the
replaced with a update
semicolon statement

Update
FOR LOOP AND THE COMMA OPERATOR

Output

Notice ‘i’ and ‘j’ looping together in a


single for loop separated by the
comma operator
CONTD…
#include <stdio.h>

int main(){
int i, j;

for (i = 0, j = 10; i < 3 && j > 8; i++, j--){


printf (" the value of i and j : %3d %3d\n",i, j);
}
}

Output:
the value of i and j : 0 10
the value of i and j : 1 9
WHILE LOOP SYNTAX
while (testExpression) {
// the body of the loop }
How while loop works?
• The while loop evaluates the testExpression inside the parentheses ().
• If testExpression is true, statements inside the body of while loop are
executed. Then, testExpression is evaluated again.
• The process goes on until testExpression is evaluated to false.
• If testExpression is false, the loop terminates.
EXAMPLE

int i = 0;

while (i < 5) {
printf("%3d", i);
i++;
}
output : 0 1 2 3 4
CONTD…

Notice the
similarity

Initialization

Termination

Update
PREDICT THE OUTPUT?
WHILE (1) OR WHILE (TRUE) & BREAK
STATEMENT

Run forever
If the condition is
satisfied break from
the while loop

Otherwise, print
count and update
DO- WHILE LOOP SYNTAX
do {
// code block to be executed
}while (condition);
How do...while loop works?
• The body of do...while loop is executed once. Only then, the condition
is evaluated.
• If condition is true, the body of the loop is executed again and
condition is evaluated once more.
• This process goes on until condition becomes false.
• If condition is false, the loop ends.
EXAMPLE

int i = 0;

do {
printf("%3d", i);
i++;
}
while (i < 5);

Output : 0 1 2 3 4
CONTD…

Execute at least once

Start checking condition after executing at least once

Output
CONTD…

Output
CONTD…
On which number
will this program give
an incorrect result ?

Output
CONTD…

Output
NESTED FOR LOOP
(0,0) (0,1) … … (0,4)
(1,0) : : : (1,4)

Matrix : : : : :
: : : : :
(4,0) … … … (4,4)

Output
CONTINUE STATEMENT

(a)
(a) (b)

Both (a) and (b) print odd numbers between 1 and 100. Note
that (a) has an implicit ‘continue’ when the condition is not
satisfied. (b) has an explicit ‘continue’ on the even numbers.

Output
PRACTICE PROBLEMS ON LOOPING

1. Find the total number of digits in a given number.

2. Find the sum of digits of a number.

3. Find if a given number is an Armstrong number.

4. Find if a given number is prime.

5. Reverse a number.
THANK YOU

You might also like