Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 18

LOOPS

Namayala Phesto
Revision
Complete a program for a simple calculator using both if else and
switch case statements.
Highlights the limitations of switch case statement.
Accept views, challenges encountered or some other businesses from
students.
Write a simple program to test if entered number is even or not
Write both flow chart and a simple program test if entered integer
number is ODD or not
Program to check if entered number is even
#include <stdio.h>
int main() {
int num;
printf("Please enter a number \n");
scanf("%d",&num);
if (num%2 == 0)
printf("%d is EVEN",num);
else
printf("%d is NOT EVEN",num);
return 0;
}
Program to check if entered number is odd
or not
#include <stdio.h>
int main() {
int num;
printf("Please enter a number \n");
scanf("%d",&num);
if (num%2 == 1)
printf("%d is ODD",num);
else
printf("%d is NOT ODD",num);
return 0;
}
other

Both flow chart and a program for a simple calculator was written in
the class and discussed
Objectives
Define what is looping statement
Differentiate loops from conditional statements
Identify categories of looping statements
Create a general flow chart
Create flow charts and examples for each type of looping
statement
What is looping statement ?
Looping statement are the statements execute one or more
statement repeatedly several number of times.
In programming language there are three types of loops;
While loop,
for loop and
do-while loop.
Why Loops
When you need to execute a block of code several number of times
then you need to use looping
Example
1. What a program which prints your name 100 times
2. Write a program which prints all numbers between 1 and 10000
inclusive

For a knowledge which we have above programs will require multiple


lines of output statements to meet requirement.

Multiple lines can be avoided by using loops.


Advantage of looping statements
Reduce code length
Take less memory space.
Reduce developers
programming burden.
Reduce time consumed on
executing program processes
Conditions necessary for looping statements
Initial condition A condition which can allow program enters in a
loops
Test condition allows a loop either to continue or exit
Update or change initial condition is very important in the
sense that it can be used to avoid an endless loop
Difference between conditional and looping
statement

Conditional statement executes only once in the


program where as looping statements executes
repeatedly several number of time.
For Loop
for loop is a statement
which allows code to be
repeatedly executed. For
loop contains 3 parts
Initialization, Condition
and Increment or
Decrements.
Example

Using for loop solve the above two problems


workings
While loop
in while loop First
check the condition if
condition is true then
control goes inside the
loop body other wise
goes outside the body.
while loop will be
repeats in clock wise
direction.
Repeat the above problems using while loop
Do-while loop
A do-while loop is similar to a
while loop, except that a do-
while loop is execute at least
one time before testing the
condition.
A do while loop is a control
flow statement that executes a
block of code at least once, and
then repeatedly executes the
block, or not, depending on a
given condition at the end of
the block
workings
Do the above examples using the do while statements

You might also like