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

Lesson 10. Looping Statement

Uploaded by

ramirez.bas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
13 views

Lesson 10. Looping Statement

Uploaded by

ramirez.bas
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 12

Lesson 10.

Looping Statement in C Language

A Loop or iterative statement executes the sequence of statements many


times until the stated condition becomes false. A loop consists of two parts, a
body of a loop and a control statement. The control statement is a combination
of some conditions that direct the body of the loop to execute until the specified
condition becomes false. The purpose of the looping statement is to repeat the
same code a number of times.

There are three (3)main types of looping statement in C language


namely:

1. For ..Loop

2. While Loop

3. Do while loop

Other looping statement includes:

4. Break

5. continue

For Loop

A for loop is a repetition/iteration control structure that allows you to efficiently


write a loop that needs to execute a statement or statement block as long as
the condition is true.

SYNTAX:

for (initialization; condition; modifiers) {

statement/s;

While Loop
A while loop statement repeatedly executes a target statement as long as a
given condition is true.

In while loop, statement(s) may be a single statement or a block of


statements. The condition may be any expression, and true is any nonzero
value. The loop iterates while the condition is true.

However, when the condition becomes false, the program control passes to the
line immediately following the loop.

SYNTAX:

while (condition) { statements; }

Do-While loop
The do-while loop is similar to the while loop except that the condition is always
executed after the body of a loop. It is also called an exit-controlled loop.

SYNTAX:

do {

statements

while (expression);

As we learned in using a while loop, the body is executed if and only if the
condition is true. In some cases, we have to execute a body of the loop at least
once even if the condition is false. This type of operation can be achieved by
using a do-while loop.

In the do-while loop, the body of a loop is always executed at least once. After
the body is executed, then it checks the condition. If the condition is true, then it
will again execute the body of a loop otherwise control is transferred out of the
loop.

Similar to the while loop, once the control goes out of the loop the statements
which are immediately after the loop is executed.

The other difference between the while and do-while loop is that in while loop
the while is written at the beginning. In do-while loop, the while condition is
written at the end and terminates with a semi-colon (;)

Break Statement

The break statement is used mainly in in the switch statement. It is also useful
for immediately stopping a loop.

The program introduces a break to exit a while loop:

#include <stdio.h> int main() { int num = 5;

while (num > 0) { if (num == 3) break; printf("%d\n", num); num--;

}}

Output:
5

Continue Statement

When you want to skip to the next iteration but remain in the loop, you should
use the continue statement.

For example:

#include <stdio.h> int main() { int nb = 7; while (nb > 0) { nb--; if (nb ==
5) continue;

printf("%d\n", nb);

}}

Output:

So, the value 5 is skipped.

FOR STATEMENTS

int main()

int ntimes;

for(ntimes=1; ntimes<=10; ntimes = ntimes+2)

printf("%d Benj\n", ntimes);

}
printf("\nProgrammed by: Benjamin Ramirez");

return 0;

int main()

int ntimes;

for(ntimes=1; ntimes<=20; ntimes++)

printf("*\n");

printf("\nProgrammed by: Benjamin Ramirez");

return 0;

int main()

int ntimes;

char name[50];

for(ntimes=1; ntimes<=3; ntimes++)

printf("Please enter your name:");

scanf("%s", &name);

}
printf("\nProgrammed by: Benjamin Ramirez");

return 0;

int main()

int ntimes, howmany;

char name[50];

printf("How many times do you want to print your name?");

scanf("%d", &howmany);

for(ntimes=1; ntimes<=howmany; ntimes++)

printf("Please enter your name:");

scanf("%s", &name);

printf("\nProgrammed by: Benjamin Ramirez");

return 0;

}
int main()

int order;

int quantity, hm, ntimes;

float price = 0;

printf("How many times do you want to order food?");

scanf("%d", &hm);

for(ntimes=1; ntimes<=hm; ntimes++)

printf("\n Weclome to Anghelo's, Here's our Menu!\n");

printf("[1]Hamburger 25.00 PHP\n");

printf("[2] Fries 15.00 PHP\n");

printf("[3] Chicken Sandwich 20.00 PHP\n");

printf("[4] Buffalo Cheese Steak 50.00 PHP\n");

printf("[5] Grilled Cheese Sandwich 30.00 PHP\n");

printf("Enter your order: ");

scanf(" %d", &order);

switch(order)

{
case 1:

printf("Enter number of pcs: ");

scanf("%d", &quantity);

price = 25.00 * quantity;

printf("You ordered %d Hamburger(s) worth %.2f PHP.\n", quantity, price);

break;

case 2:

printf("Enter number of pcs: ");

scanf("%d", &quantity);

price = 15.00 * quantity;

printf("You ordered %d Fries worth %.2f PHP.\n", quantity, price);

break;

case 3:

printf("Enter number of pcs: ");

scanf("%d", &quantity);

price = 20.00 * quantity;

printf("You ordered %d Chicken Sandwich(es) worth %.2f PHP.\n", quantity, price);

break;

case 4:

printf("Enter number of pcs: ");

scanf("%d", &quantity);

price = 50.00 * quantity;

printf("You ordered %d Buffalo Cheese Steak(s) worth %.2f PHP.\n", quantity, price);

break;

case 5:
printf("Enter number of pcs: ");

scanf("%d", &quantity);

price = 30.00 * quantity;

printf("You ordered %d Grilled Cheese Sandwich(es) worth %.2f PHP.\n", quantity,


price);

break;

default:

printf("Sorry, that's an Invalid Order... Please enter something else.\n");

printf("Programmed by: Benjamin Alfred S. Ramirez\n");

return 0;

printf("Thank you! Come again!\n");

printf("Programmed by: Benjamin Alfred S. Ramirez\n");

return 0;

WHILE STATEMENTS:

int main()

int n;

n=1;

while(n<=5)

printf("\n Benjamin\n");

n++;
}

return 0;

int main()

int n;

n=1;

while(n<=5)

printf("\n %d. Benjamin\n",n);

n++;

return 0;

int main()

int n, ntimes;

printf("How many times do you want to dislay your name:");

scanf("%d",&ntimes);

while(n<=ntimes)
{

printf("\n %d. Benjamin\n",n);

n++;

return 0;

int main()

int n, ntimes;

printf("How many times do you want to dislay your name:");

scanf("%d",&ntimes);

n=1;

while(n<=ntimes)

printf("\n %d. Benjamin\n",n);

n++;

return 0;

}
DO-WHILE STATEMENTS:

int main()

int n, ntimes;

printf("How many times do you want to dislay your name:");

scanf("%d",&ntimes);

n=1;

do{

printf("\n %d. Benjamin\n",n);

n++;

}while(n<=ntimes);

return 0;

You might also like