CAT | C language

the various control structures in C

Control structures are the statements which controls the overall any program. Control structures are basically of three types –

  • Sequence statements
  • Iterative statements
  • Selection statements

Sequence Statements : All the State in a program except the iterative & statements. They are generally the individual statements which performs the task of input, output, assignment declaration etc.

Iterative Statement are those repeated execution of a particular set of instructions desired number of times. These statements are generally called loops for their execution nature.  Loops are basically of two types –

    • Count Loop
    • Event Loop

      Count Loop :- Those loops whose no. of times of execution is known prior to the execution of loop are termed as count loops i.e they care countable in nature.

      Event Loop :- Those loop whose execution any certain event are called event before the execution.
      A loop basically consists of a loop variable which has for the completion of any iterative   statement, i.e a loop consists of three main statements:-

  1.  

    (1) Initialization (2) Condition (3) Incrementation

Initialization is used to start any loop i.e. it gives some initial value to the loop variable. Which defines that from which value the loop has to get start. Condition is provided to give the final value to the loop variable so that how many times the loop has to get executed. For reaching the loop variable from initial value to the final value there should be some sort of increamentation and that provided by the third component statement of loop and that is incrementation.

If in any loop the incramentation or the final value is not provided then the loop becomes infinite. If the initialization is not done then the garbage value of the loop variable becomes its initial value.

In C language the iterative statements (loops) can be implemented in the three loops and they are

The for loop

Syntax of For Loop –
for (initialization; condition; incrementation)
{ —————– body of loop ————-
}

For Loop will perform its execution until the condition remains satisfied. If the body of the loop consists of more than one statement then these statements are made compound by placing the open and closed curly brackets around the body of the loop. For loop is a count loop. The initialization condition and increamentation may be done in the same statement. For loop will not execute at least once also if the condition is false at the first time itself.

With loops you also have to put commands between curly brackets if there is more than one of them. Here is the solution to the problem that we had with the 24 printf commands:

 

Code: 

#include 
int main()
{
int i;
for (i = 1;i <= 24;i++)
printf("H\n");
return 0;
}

 

A for loop is made up of 3 parts inside its brackets which are separated by semi-colons. The first part initializes the loop variable. The loop variable controls and counts the number of times a loop runs. In the example the loop variable is called i and is initialized to 1. The second part of the for loop is the condition a loop must meet to keep running. In the example the loop will run while i is less than or equal to 24 or in other words it will run 24 times. The third part is the loop variable incremental. In the example i++ has been used which is the same as saying i = i + 1. This is called incrementing. Each time the loop runs i has 1 added to it. It is also possible to use i– to subtract 1 from a variable in which case it’s called decrementing.

The while loop

Syntax –
Initialization;
While (condition)
{
Body of loop;
Incrementation;
}

In this loop, initialisation, condition and incrementation is done in the three different statements. This loops is count as well as event loop. In case of while loops the body of the loop will consist of more than one statements because each time one statement will be of incrementation. Hence the open and closed curly brackets are required.

 

Code: 

#include<stdio.h> 
int main()
{
int i,times;
scanf("%d",&times);
i = 0;
while (i <= times)
{
i++;
printf("%d\n",i);
}
return 0;
}

 

The do while loop

The third loop statement available in C is do-while statement syntax :-
Initialization;
Do
{
Body of loop;
Increamentation;
}
While (condition);

In this loop the condition is checked at the end, and for this reason this loop will execute at least once whether the condition may be satisfying and unsatisfying.

 

Code: 

#include<stdio.h> 
int main()
{
int i,times;
scanf("%d",&times);
i = 0;
do
{
i++;
printf("%d\n",i);
}
while (i <= times);
return 0;
}

 

Break and continue

You can exit out of a loop at any time using the break statement. This is useful when you want a loop to stop running because a condition has been met other than the loop end condition.

 

Code: 

#include<stdio.h> 
int main()
{
int i;
while (i < 10)
{
i++;
if (i == 5)
break;
}
return 0;
}

 

You can use continue to skip the rest of the current loop and start from the top again while incrementing the loop variable again. The following example will never print “Hello” because of the continue.

 

Code: 

#include<stdio.h> 
int main()
{
int i;
while (i < 10)
{
i++;
continue;
printf("Hello\n");
}
return 0;
}

 

No tags Hide

#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int i,j,sp,n;

printf(“enter the no::”);

scanf(“%d”,&n);

sp=n-1;
for(i=1;i<=n;i++)

{

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

{

printf(” “);

}

for(j=i;j>=1;j–)

{

printf(“%d”,j);

}

for(j=2;j<=i;j++)

{

printf(“%d”,j);

}

printf(“\n”);

sp–;

}

sp=n+1-5;
for(i=n-1;i>=1;i–)

{

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

{

printf(” “);

}

for(j=i;j>=1;j–)

{

printf(“%d”,j);

}

for(j=2;j<=i;j++)

{

printf(“%d”,j);

}

printf(“\n”);

sp++;

}
getch();

}

No tags Hide

/*123454321

2345432

34543

454

5*/

#include<stdio.h>

#include<conio.h>
void main()

{

clrscr();

int i,j,sp,n;

printf(“enter the no::”);

scanf("%d",&n);

sp=n+1;

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

{

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

{

printf(” “);

}

for(j=i;j<=n;j++)

{

printf(“%d”,j);

}

for(j=n-1;j>=i;j–)

{

printf(“%d”,j);

}

printf(“\n”);

sp++;

}

getch();

}

No tags Hide

/*      1

121

12321

1234321

123454321*/
#include<stdio.h>

#include<conio.h>

void main()

{

clrscr();

int i,j,sp,n;

printf(“enter the no::”);

scanf(“%d”,&n);

sp=n-1;

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

{

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

{

printf(” “);

}

for(j=i;j>=1;j–)

{

printf(“%d”,j);

}

for(j=2;j<=i;j++)

{

printf(“%d”,j);

}

printf(“\n”); sp–;

}

getch();

}

No tags Hide

/*1

12

123

1234

12345  */

#include<stdio.h>

#include<conio.h>
void main()

{

clrscr();

int n;

printf(“enter the value::”);

scanf(“%d”,&n);

int i;

int j;

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

{

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

{

printf(“%d”, j);

}

printf(“\n”);

}

getch();

}

No tags Hide

Next »

Managed by

MNP INFOTECH