Looping with do…while Statement

This note is about the looping with do…while. It includes break statement; continue statement, go to statement too. It also includes the Nested Control Structures too.

Summary

This note is about the looping with do…while. It includes break statement; continue statement, go to statement too. It also includes the Nested Control Structures too.

Things to Remember

  1. The do..while loop evaluates the condition after the execution of the statements in its construct. At least one time the do…while statement executes.
  2. A break statement is used in the program to breaking from the current point. It terminates the execution of the current loop and the control is transferred to the statement immediately following the loop.
  3. The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when continue statement is encountered.
  4. The goto statement in programming allows you to jump from one place to another without any conditions. This is an unconditional transfer to another part of the program.
  5. The goto statement is common in GWBASIC and QBASIC. Although QBASIC is structured it resembles in many cases with the procedural programming concept.
  6. A loop inside another loop is nested loop. While constructing nested loops, it is necessary to have the same type of looping structure.

MCQs

No MCQs found.

Subjective Questions

No subjective questions found.

Videos

No videos found.

Looping with do…while Statement

Looping with do…while Statement

Looping with do…while Statement

We have come to across the use of while statement. It is top tested, i.e., it evaluates before executing any of the statement in its body. The do..while loop evaluates the condition after the execution of the statements in its construct. At least one time the do…while statement executes.

Hence, do…while statement is known as bottom tested loop.

Syntax:

do

{

statement 1;

statement 2;

………………….;

statement n;

}

while (test expression);

If the program loop should pass through 1 time or single iteration, do…while statement would look like;

do

Statement 1;

While (test expression);

# Write a program to print the following numeric pattern.

1

12

123

1234

12345

#include <stdio.h>

#include <conio.h>

void main ()

{

clrscr ();

int outloop=1;

do

{

int inloop =1;

do

{

printf(“%d”,inloop);

inloop = inloop +1;

}

while (inloop <=outloop);

outloop = outloop+1;

printf(“\n”);

}

while (outloop <=5);

}

# Write a program to check whether a number is palindrome or not.

#include <stdio.h>

#include <conio.h>

void main ()

{

int n, num,digit, sum=0, rev=0;

printf(“\n Enter a number”);

scanf(“%d”,&num);

n= num;

do

{

digit = num ;

sum+= digit;

rev =rev*10+digit;

num /=10;

}

while (num !=0);

printf(“\n Sum of the digits = M”, sum);

printf(“\n The reversed number is: M”, rev);

if (n==rev)

printf(“\n Given number is palindrome”);

else

printf(“\n Given number is not palindrome”);

}

The break Statement

A break statement is used in the program to breaking from the current point. It terminates the execution of the current loop and the control is transferred to the statement immediately by following the loop.

while (1)

{

scanf(“%d”, &i);

if (I ==-1)

break;

sum+=1;

num++;

}

This program segment has used the break statement. If the input value is -1, the condition i==-1 is true and the break statement is executed. Otherwise, the program continues.

# Write a program to check whether a given number is a prime number.

/* check prime number */

#include <stdio.h>

#include <conio.h>

main ()

{

int a, b=2, r, n;

printf(“\n Enter a number :”);

scanf(“%d”, &a);

n =a;

if (a==1 || a==2 || a==3)

printf(“The num %d is prime number “,n);

while (b!=(a-1))

{

aa:

r= a%b;

b=b+1;

if (r ==0)

{

printf(“\n The num %d is not prime number”,n);

break;

}

else if (b!= (a-1))

{

goto aa;

}

else

printf(“\n The num %d is a prime number “,n);

}

}

# Modify the above program and recheck whether a given number is prime number using different logic.

#include <stdio.h>

#include <conio.h>

void main ()

{

int a, b=2, r, n;

clrscr ();

printf(“\n Enter a number: “);

scanf(“%d”, &a);

n=a;

if ((a==1 || (a==2 || (a==3))

printf(“\n The number %d is prime number”,a);

if (a>3)

{

while (b!= (a-1))

{

aa:

r= a%b;

b= b+1;

if (r==0)

{

printf(“\n %d is not a prime number”, n);

break;

}

else if (b! =(a-1))

goto aa;

else

printf(“\n The num %d is a prime number”, n);

}

}

}

The continue Statement

As its name implies, it is used to continue the normal flow of programe statement execution in loop; skipping parrticular iteration in the loop as soon as certain condition is satified. The continue statement is used to bypass the remainder of the current pass through a loop. The loop does not terminate when continue statement is encountered. Instead, the remaining loop statement is skipped and the computation proceeds directly to the next pass through the loop.

Syntax:

#include <stdio.h>

#include <conio.h>

void main ()

{

int i=1,num, sum=0;

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

{

printf(“\n Enter an integer:”);

scanf(“%i”, &num);

if (num<0)

{

puts (“\n You have entered a negative number.”);

continue;

}

sum+=num;

}

printf(“\n The sum of the positive integers entered = %i\n”, sum);

getch ();

}

When you execute the above program, at the time you enter a negative number, you will be prompted for negative entry of any number. It allows you to go to next iteration. Thus, you can enter a new value for five iterations. When you finish entering 5 positive integers, you will get the result as you desire.

Difference between break and continue statement

Break statement Continue statement

When break statement is encountered

the entire loop or switch statement is

terminated.

when continue statement is encountered

the entire loop is not terminated

only that particular itration is skipped.

It is used with loop and switch case statement. It is used only with loop structure.
It uses a keyword break. It uses a keyword continue.

The goto Statement

The goto statement is common in GWBASIC and QBASIC. Although QBASIC is structured it resembles in many cases with the procedural programming concept. The goto statement in programming allows you to jump from one place to another without any conditions. This is an unconditional transfer to another part of the program.

Syntax:

goto label name;

Use: Unconditional jump from one point to another part of the program.

# Write a program to check whether a given number is a prime number or not.

#include <stdio.h>

#include <conio.h>

main ()

{

int a, b=2, r, n;

printf(“\n Enter a number :”);

scanf(“%d”, &a);

n =;

if (a==1 || a==2 || a==3)

printf(“ The num %d is prime number”,n);

while (b! =(a-1))

{

aa:

r=a%b;

b=b+1;

if (r==0)

{

printf(“\n The num %d is not a prime number”,n);

break;

}

else if (b!= (a-1))

{

goto aa;

}

else

printf(“\n The num %d is a prime number”,n);

}

}

# Modify the above program and recheck whether a given number is prime number using different logic.

#include <stdio.h>

#include <conio.h>

void main ()

{

int a, b=2, r, n;

clrscr ();

printf(“\n Enter a number :”);

scanf(“%d”, &a);

n=a;

if ((a==1 || (a==2) || (a==3))

printf(“\n The number %d is prime number”,a);

if (a<3)

{

while (b!=(a-1))

{

aa:

r=a%b;

b=b+1;

if (r==0)

{

printf(“\n %d is not a prime number”,n);

break;

}

else if (b! =(a-1))

got aa;

else

printf(“\n The num %d is a prime number”, n);

{

}

}

Nested Control Structures

Loops like if…else, for, do…while and while statements can be nested, one within another. A loop inside another loop is nested loop. While constructing nested loops, it is necessary to have the same type of looping structure. The number of loops within a loop as many as you desire but some systems limits to its memory limitation.

# Write a program to print multiplication table of number from 1 to 10 up to 10 multiples.

/* multiplication table */

#include <stdio.h>

#incldue <conio.h>

void main ()

{

int pro, i, j;

for (int i=1; i<=10; i++)

{

for (int j=1; j<=10; j++)

{

pro=i*j;

printf(“%d\t”, pro);

}

}

}

# Write a program to print the following pattern.

*

**

***

****

*****

#include <stdio.h>

#include <conio.h>

void main ()

{

int i, j;

char c=’*’;

for (i=1; i<=5; i++)

{

for (j=1; j<=I; j++)

{

printf (“%c”, c);

}

printf(“\n”);

}

}

# Write a program to print the following numeric pattern (using do…while condition).

1

12

123

1234

12345

#include <stdio.h>

#include <conio.h>

void main ()

{

clrscr ();

int outloop=1;

do

{

int inloop =1;

do

{

printf(“%d”,inloop);

inloop = inloop +1;

}

while (inloop <=outloop);

outloop =outloop+1;

printf(“\n”);

}

}

References:

Khanal, R.C. Khanal, R.C. Computer Concept for XII. Pashupatigriha Marga, Thapathali, Kathmandu, Nepal: Ekta Books Distributors Pvt. Ltd., 2010. 180-191.

Adhikari, Deepak Kumar.,et.al., Computer Science XII,Asia Publication Pvt.Ltd

Lesson

Programming in C

Subject

Computer Science

Grade

Grade 12

Recent Notes

No recent notes.

Related Notes

No related notes.