Home / C languaege / Control Structure in C language

Control Structure in C language

Here we discuss about control structure in C language usually the program are executed line by line but if we want to transfer control form one line to another then control structure is used .

Control Structure in C

In in C language we have different types of control  structure which are following

1#.Sequence structure

In this type of structure the instruction of program are executed in sequence I mean executes one after the other in the order in which they are written .It is also called the default flow of a program .he program starts execution form the first line of program and execute the program till the last in sequence.The control in transferre in sequential order .

2#. Selection structure?

In selection structure the execution on program is based on some specific condition. If condition is true its control transfer to one statement if it got false control will be transfer to some other statement in C language.

3#.Repetition structure?

It is also called iterative or repeated  structure. It execute one statement or set of statement again and again until the condition get false .In other words we can say that it is an type control structure in C language that is used to repeat same or similar work .There are three kind of loop structure in C language.

4# If statement

If is a simplest form of selection structure in C language  and is used to execute or skip a statement or set of statement after testing a condition .The condition should be logical or relational expression.

 

Control structure in c

Write a program In C language that take marks from student and display pass if marks are greater or equal to 33.

Example

#include <stdio.h>

#include<conio.h>

void main

{

int m;

clrscr();

scanf(“%d”,&m);

if (m>=33)

printf(“pass”);

getch();

}

Explanation

In this program if you give any input which is equal or greater than 33 then  this program will print pass otherwise it will print fail

5#.compound statement

If we want to execute more than one statement on sin any program then compound statement is used. We can say that

a set of statement enclosed in curly brackets is called compound statement .It is also

Called block of code.

If condition

{ Statement 1;

Statement 2;

}

5#. if else statement

It is also known as two way decision making structure .If we want to make after the evaluation of a condition one from two blocks will be executed other will be skipped

Syntax       if(condition )

Statement;

else statement ;

If else control structure

Example.

Write a program In C language that take marks from student and display pass if marks are greater or equal to 33 if not then print fail.

#include <stdio.h>

#include<conio.h>

void main

{

int m;

clrscr()

;scanf(‘%d”,&m);

if (m>=33;

printf(“p”);

else

printf(“fail);

getch();

}

6#.if else if statement

If –else –is statement can be used to choose one block of statement from many block of statement .It is used when there is many option and only one block of statement should be executed on the basis

of condition .

Write a program that inputs a number form user and determines whether it is positive, negative or zero

 

ifelseif

Program  :

#include <stdio.h>

#include<conio.h>

void main

{

clrscr();

int m;

scanf(‘%d”,&m);

if (m>0)

printf(“positive ”);

else

if (m<0)

printf(“Negative “);

else

printf(“The number is zero “);

getch();

}

7#.Conditional operator

Conditional operator is used as an alternative of simple if  else statement it is used to make two way decision. The condition should be logical or relational expression .After evaluation the result of condition is true the statement 1 is executed if false then statement two is executed .

Exampe.

Write a program In C language that take marks from student and display pass if marks are greater or equal to 33.

Program:

#include <stdio.h>

#include<conio.h>

void main

{

int m;

clrscr();

scanf(“%d”,&m);

if(m>=33)

printf(“pass”);

printf(“fail”);

getch();

}

8#.What is switch Statement

Switch statement is an alternative of if-else-if statement .It is also conditional statement .It is used when there are many choices available and only one should be executed .

Control Structure Switch Statement

Example

Write a program that inputs a number form 1-7 and display the relevant day of the week

#include <stdio.h>

#include<conio.h>

void main

{

int m;

clrscr();

scanf(‘%d”,m);

switch (m)

{

case 1:

printf(“\nMonday ”);

breake;

case 2:

printf(“\nTuesday ”);

breake;

case 3:

printf(“\Wednesdya ”);

breake;

case 4:

printf(“\nThursday ”);

breake;

case 5:

printf(“\nFridya ”);

breake;

case 6:

printf(“\n Satureday ”);

breake;

case 7 :

printf(“\nSunday ”);

breake;

defaule:

printf(“Invalid Entery”);

getch();

}

9#.Nested if

The use of an if statement with another “if” Statement called  nested if statement.

Write a program that three number and display whether all number are equal or different using nested if.

#include <stdio.h>

#include <conio.h>

void mian ()

{

int a,b,c;

clrscr();

scanf(“%d%d%d,&a,&b,&c);

if (a==b) if (a==c)

printf(“all number are equal”);

else

printf(“all number are different ”);

getch ();

}

 

Want to learn about compiler and interpreter? click me 

10#. Break statement ?

Ans :The break is used to transfer flow of control out side a code block .When break statement executes in switch statement the flow of control is transferred to the first instruction after the switch break .

 

The following two tabs change content below.
Hi I am Ch Shahbaz from Pakistan.I love to writing different type of informative Articles. By profession i am a Pro Blogger, Web Developer, Graphic Designer and Creative writer.

Latest posts by Ch Shahbaz (see all)

Check Also

GW BASIC MCQS in Urdu and English

 GW BASIC MCQ,s in Urdu and English for class 10

 Often people feeling difficulty while making paper of GW BASIC MCQS in Urdu and English …

Leave a Reply

Your email address will not be published. Required fields are marked *