Skip to main content

Loops

LOOP:

A loop is a sequence that gets executed several times. The ability to specify repetition of a group of instructions is an important problem-solving tool.

TYPES OF LOOPS:

For loop Statement.
While loop Statement.
Do-while Statement.

for Loop:

The for loop consist of three parts
1- The initialization: Set the loop control variable assignment.
2- The condition: A relational expression that determines when the loop exits.
3-The increment: Defines how the loop control variable changes each time the loop is repeated.

Syntax:

for (initialization expression ; loop repetition condition ; update expression )
{
Statement_Block;
}

INTERPRETATION:

First, the initialization expression is executed.
Then, the loop condition is tested. If it's true, the statement is executed, and the update expression is evaluated.
Then the loop repetition condition is retested.
The statement is repeated as long as the loop condition is true. When this condition is false, the for loop is exited, and the next program statement after the for statement is executed.

While Statement:

This loop is used to repeat a section of code an unknown number of times until a specific condition is met.
WHILE loop and FOR loop can be used interchangeably. But normally FOR loop is used when the exact number of iterations of the loop are known. WHILE loop is used when the number of iterations is not known.

SYNTAX:

while ( loop repetition condition )
{
Statement_Block;
}

INTERPRETATION:

The loop repetition condition (a condition to control the loop process) is tested; if it's true, the statement (loop body) is executed, and the loop condition is retested.
The statement is repeated as long as the loop condition is true. When this condition is false, the while loop is exited and the next program statement after the while statement is executed.

 

Do-While Loop:

Unlike for and while loops, which test the condition at the top of the loop, do while loop checks its condition at the bottom of the loop. This means that it always executes at least once.
This loop executes the loop body again and again as long as condition evaluates to true.

C- SYNTAX:

do
{
Statement_Block;
} while ( loop repetition condition );

INTERPRETATION:

First, the statement is executed.
Then, the loop repetition condition is tested, and if it's true, the statement is repeated and the condition retested. When this condition is false, the loop is exited and the next statement after the do-while is executed.

Implementation of Loops in C:

Q: Write a program to test whether a given number is prime or not.

#include<stdio.h>

int main()
{
 int no,x=1;
 int count=0,y;
 printf("Please enter Number ");
 scanf("%d",&no);
 while (no>=x)
 {
  y=no%x;
  x++;
  if (y==0)
  {
   count=count+1;
  }
 }
 if (count==2)
 {
  printf("\n%d is a Prime number",no);
 }
 else
 {
  printf("\n%d is a Not Prime number",no);
 }
return 0;
}
Q: Write a program to reverse the digits of a number such that 1234 becomes 4321.

#include<stdio.h>

int main()
{
 int no,a;
 int sum=0;
 printf("Please enter Number ");
 scanf("%d",&no);
 a=no;
 do
 { 
  a=no%10;
  sum=(sum*10)+a;
  no=no/10;
 }
 while (no!=0); 
 printf("The reverse number is %d ",sum);
 return 0;
 
 printf("%c",65);
}

Pattern printing Diamond:

#include<stdio.h>

int main()
{
 int x,y,a,b,z;
 for (x=0;x<10;x++)
 {
  for(a=10;a>x;a--)
  {
   printf(" ");
  }
  printf("*");
  
  for (y=0;y<x;y++)
  {
   printf("  ");
   
  }
  printf("*");
  printf("\n");
 }
 
 for (x=0;x<10;x++)
 {
  for(y=0;y<x;y++)
  {
   printf(" ");
  }
  printf("*"); 
  
  for (z=10;z>x+1;z--)
  {
   printf("  ");
  }
  printf("*");
  printf("\n");
 }
 return 0;
}

Comments

Popular posts from this blog

Pointers

Pointers: A pointer is a variable whose value is a memory address. A pointer contains the memory address of a variable that, in turn, contains a specific value. In this sense, a variable name directly references a value, and a pointer indirectly references a value. Syntax: type * variable ; Interpretation: The value of the pointer variable ptr is a memory address. A data item whose address is stored in this variable must be of the specified type. Dynamic Memory Management: C/C++ enables programmers to control the allocation and deallocation of memory in a program for any built in or user defined type. The ability for a program to obtain more memory space at execution time to hold new nodes, and to release space no longer needed is known as dynamic memory management. Syntax: int *num = (int *)malloc(sizeof (int)*numCount); or int *ptr = (int *)calloc(numCount, sizeof (int)); /* returns a pointer to a section of memory just large enough to hold the integers, whose q...

Single LinkList in Java

Linked List: Linked List contains a sequence nodes which are linked together. Each node contains a connection to another link and data. Linked list is the second most-used data structure after array. Following are the important terms to understand the concept of Linked List. Link − Each link of a linked list can store a data called an element. Next − Each link of a linked list contains a link to the next link called Next. LinkedList − A Linked List contains the connection link to the first link called First. Types of Linked List: Following are the various types of linked list. Simple Linked List − Item navigation is forward only. Doubly Linked List − Items can be navigated forward and backward. Circular Linked List − Last item contains link of the first element as next and the first element has a link to the last element as previous. Basic Operations: Insert:  Inserts at tail,  specific index. Delete: Deletes from the tail.  specific index. ...

Object Oriented Programming (OOP)

Object Oriented Programming: Object Oriented Programming (OOP) is a programming concept which used in the modern programming world. Languages like Java, C++,  and Python support Object Oriented Programming (OOP). It works on the principle that objects are the most important part of a program. In OOP we think in terms of objects and every object has its attributes (properties) and a state (behavior/functions). Object Oriented Programming (OOP) is a technique of system modeling and its main purpose is to understand the product before developing it and manipulating these objects to achieve a specific task.   Pillars of Object Oriented Language (OOP): There are four basic principles of Object Oriented Language (OOP). Inheritance Polymorphism Data Encapsulation Abstraction WHY is Object Oriented Language (OOP) NEEDED? Problems with Procedural Languages: Functions have unrestricted access to global data Unrelated Functions and data. Before Object Oriente...