Skip to main content

Control Statements in c language

Control Statements In C - language

So, Today we are going on new topic name control statements in C language. In which we read Looping(For, While and Do While), Jumping(Break, goto and Continue) and Decision Control statements(Switch, If, If Else and If else if ladder) in C.

Before you read this article May you also like our previous posts:- 
 Structure Of C Language Program
 Variables & Datatypes In C Language 
 Format Specifier In C Programming . It may help you to understanding it.

A Statement is the smallest unit that is a complete instruction in itself statements contains expression and usually end with a semicolon. Statements are of two Types:-

Types of Statements in C Language
Types of Statements in C 
1. Sequential Statement 
2. Control Statement
Sequential Statements are statements which are executed by one in sequential manner.
Control Statements are the statements which alter the flow of execution and provide better control to the programme. On the flow of execution. They are useful to write better and complex program.

Control Statements In C Language

Types of Control Statement in C language
Types of Control Statement in C

On further control statements have types are:-
1. Looping Statements:-

* For * While * Do while

2. Jumping Statements:-

* Break* goto* Continue

3. Decision control Statements:-

* If* If-else*If else if ladder* Nested* Switch

Looping Control Statements In C Language

C - Language supports Three Looping Statements:
A. For B. While C. Do while.
Looping Statements allow the programme to build a sequence of instructions. Which can be executed again and again with some condition deciding when they will stop.
C Language supports three Looping/iteration statements:
They are commonly referred to as loop. A Loop repeatedly executes the same. Set of instructions until a termination condition is met.
Depending on the type of the loop, the code in it is repeated a fixed number of items or repeats until a given condition is true.
Loop offer a great amount of flexibility to the programmers.
C- Language has three loops, each of which has a slightly different purpose. Further loops types is:-

Types of Loop in C
Types of Loop in C
Determinate:- 
For Loop:- They are controlled by a counter.
Indeterminate:-
while and do while Loop:- They are used when we do not know how many times a loop should be used.
imp:- To run a loop we always need a Variable.

FOR Loop

For loop is incredibly versatile and convenient.
Loop Variable
Syntax:-

For(initialization; condition; iteration)
{
/////Loop's Body     //(Code to be executed repeatedly)
}
The For loop operates as follows:-
When the loop first starts, the initialization portion of the loop is executed. This is an expression that sets the value of the loop control variable, Which acts as a counter that controls the loop.
Initialization portion is executed only once.
Next, Condition is evaluated. It usually tests the loop control variable against a target value. If the result is true, Then the body of the loop is executed.
If it is false, then the loop terminates.
The iteration portion of the loop is executed after the code in the body has been executed. Iteration portion is usually an expression that increments or decrements the loop control variable.

For Loop In C
For Loop In C
sample code:-

//Program to print number from 1 to 10.

#include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=10; i=i+1)
{
printf("%d\n", i);
}
getch();

}

While Loop

The simplest of the three loops is the while loop.
syntax:-

While (<condition> )
{


/// Code to be executed which the condition is true

}
A While loop is almost like an if statement, except that the While Loop causes its body to be repeated.
The While Loop will never execute if the condition is false at the outset.
The condition can be any Boolean expression. The body of the loop will be executed as long as the conditional expression is true.
When condition becomes false, Control passes to the next line of code immediately following the loop.

While Loop In C
While Loop In C

sample code:-

//Program to display Half of Number till the time it does not become zero.
#include<conio.h> #include<stdio.h> void main() {
int n; clrscr(); printf("Enter a Number: "); scanf("%d",&n); While(n>0) {
printf("%d\n",n);
n=n/2;
} getch(); }

DO-while Loop

The do-while Loop is similar to the while loop, but it checks the condition after each execution of its loop body. This type of loop is called POST TEST Loop.
Syntax:-

do
{
executable Code;
} while (condition);

Do While Loop In C
Do While Loop In C

The do-while loop always executes its body at least once, because its conditional expression is at the bottom of the loop.
sample code:-

//Program using Do while Loop.
#include<conio.h>
#include<stdio.h> void main() {
int i; clrscr(); i=1; do { printf("Hello");
} while(i
<0 br=""> printf("Program ENds"); getch();
}

Jumping Statements in C Language

C - Language supports Three Jumping Statements:
A. Break B. Continue C. Goto
These Statements transfer control to another part of your program. These statements can be used only inside a function.

Break Statements

The break statement/operator is used for premature exiting . The loop, before it has completed its execution in a natural way. Break statement can be used in a Switch Statement or in any of the loop. It causes program execution to pass to the next statement following the switch or loop.
Normally a loop is exited by checking the loop condition sometimes we might want to exit the loop early. C programming language provides break statement for that purpose.
When break is executed in a loop, the code in the loop's body is skipped and not executed.
Syntax:- break;
Break Statement immediately ends the loop, jumping to the closing brace.
Break statement skips rest of loop and goes to the following statement.
sample code:-

//Program to display the first number from 1 to 100 which is divisible by both 3 and 7.
#include<conio.h>
#include<stdio.h> void main() {
int x; clrscr(); for(x=1; x<=100; x=x+1) {
If (x/3==0 && x/7==0)
{
printf("Number is %d",x);
break;
}
} getch(); }


Continue Statement

The Continue Statement is very similar to the break statement, except that instead of terminating the loop, Continue starts re-excuting the body of the loop from the top.
The continue statement can be used only inside a loop. The continue statement causes a program to skip the rest of the body of the loop and then start a new loop cycle.
Syntax:- continue;
When continue is used in while and do while loop, a continue statement causes control to be transferred directly to the conditional expression that control the loop.
In a for loop, control goes first to the iteration portion of the for statement and then to the conditional expression.

When should we use Continue Statement?
When we want to continue running the loop, but stop processing the remainder of the code in its body for a particular iteration/cycle.
sample code:-

//Program using Continue statement.
#include<conio.h>
#include<stdio.h> void main() {
int i; clrscr(); for(i=1; i<=10; i=i+1) {
If ( i==5 || i==8)
{
continue;
}
printf("%d ",i);
} getch(); }

Goto Statement

Goto Statement is used in C-Language to jump to a location in a program.
Syntax:-

    goto label;
////Code
label:
Label can be any plain text except C keyword and it can be set any where in the C Program above or below to goto statement. Goto statement provides an unconditional jump from the goto to a labelled statement.
sample code:-

//Program using Goto Statement.

#include<conio.h>
#include<stdio.h> void main() {
clrscr(); printf("!st Line of Code"); printf("\n goto Started"); goto abc; printf("code which will be skipped"); abc: printf("End of the program"); getch(); }

Decision Control Statement

In a normal application, this are sequential statements. These statements are executed one after the other without any checking.
C-Programming language provides with special type of statements which are executed only after some checking/evaluation. Such statements come under the category of decision Control statements.
In c-language we are provided with following decision control statements.
A. Simple If B. Nested If C. If Else D. If else If Ladder
E. Switch

Simple If

In C-programming Language, We use a simple if statement to allow a program to select a particular execution path by testing a condition.
Syntax:-

If (expression)	//condition to be evaluated
{
statement(s)
}
Whenever an If Statement is executed, The expression in the parenthesise is evaluated. 
If the result is a true, Then the code inside the If block gets executed. If the result is false, The control gets transferred to the first line after the If block.
sample code:-

//Program to display Numbers from 1 to 100 which is divisible by 7.

#include<conio.h>
#include<stdio.h>
void main()
{
int i;
clrscr();
for(i=1; i<=100; i=i+1)
{
If (i%7==0)
printf("%d\n",i);
}
getch();
}
If Else

If else is the most widely used decision control statement. We use if else when their are two sets of code and they are to be executed after some checking. 
Syntax:-

If ( condition )	//condition to be evaluated
{
/// code //if condition is true then execute
}
else
{
///code //if condition is False then execute
}
One set is to be executed if the condition evaluates to true and other set to be executed if the condition evaluates to false. Condition is specified to the If Statement.
sample code:-

 //Program to check weather a person is valid voter or not.

#include<conio.h>
#include<stdio.h>
void main()
{
int age;
clrscr();
printf("Enter the age");
scanf("%d",&age);
If ( age>18 )
{

printf("Valid Voter");

}
Else
{

printf("Not Eligible To Vote");

}
getch();
}
If else If Ladder

If Else If is a programming construct which is based upon sequence of nested if.
If Else If Ladder is used when we are working with data which involves conditions, Having different ranges. The execution of the ladder starts from top. As soon as a condition evaluates to true, The control of the program jumps to the first line after the ladder.
Syntax:-

If ( condition )	
{

/// code
}
else if ( condition )
{

///code
}
else if ( condition )
{

///code
}

.
.
.

else
{

///code

}
The else statements in the ladder acts as default condition. The code in the else gets executed if all the conditions evaluates to falls.
sample code:-

//Creating a program for Blood Sugar Patient.

#include<conio.h>
#include<stdio.h>
void main()
{
int bsugar;
clrscr();
printf("Enter the blood sugar");
scanf("%d",&bsugar);
if( bsugar>200)
{

printf("consult to Doctor");

}
Else if ( bsugar>=150 && bsugar<=200 )
{

printf("sugar is High");

}
Else if ( sugar>=100 && bsugar<=149 )
{

printf("Sugar is Normal");

}
else
{ printf("sugar is Low");
}
getch();
}
Switch

Switch case is a control statement which is used when their are several options and we have to choose only one option from the available choices. Switch case is a decision controlled jumping statement.
Syntax:-

Switch ( Variable )
{

case constant:
///code
break;

case constant:

///code
break;
.
.
.
case default:

///code

break;
}

The expression or the variable specified to the switch case must either be Int, Long or Char. Each of the value specified in the case statement must be compatible with the expression. Each  case constant must be a literal which is unique.
sample code:-

//Creating simple calculator using switch case.

#include<conio.h>
#include<stdio.h>
void main()
{
int a,b,c,choice;
clrscr();
printf("Enter two number");
scanf("%d%d",&a,&b);
printf("1. Add\n");
printf("2. Subtract\n");
printf("3. Multiply\n");
printf("4. Divide\n");
printf("Enter the choice");
scanf("%d",&choice);
switch(choice)
{
case 1: C=a+b;
printf("the sum is %d",c);
break;
case 2: C=a-b;
printf("the result is %d",c);
break;
case 3: C=a*b;
printf("the product is %d",c);
break;
case 1: C=a/b;
printf("the result is %d",c);
break;
}
getch();
}
We will going on series of C Programming language of all topics. So, you can follow us for more updates.

If their is mistake of sentence, error in program or syntax, spelling mistake, sentence up and down and any kind of error found in this article. Kindly request to you please ignore them and contact us via comment or using Contact form to inform us particular place, article name (link of article), line of article where error found and what is error form you kindly contact with us. We are deeply sorry for inconvenience.   

Comments

Popular posts from this blog

Beginner our view on C programming Language

  Beginner our view on C programming Language   Beginner ourview on C programming Language History about C programming Language. The founder or we can say developer of C programming language was Dennis Ritchie. He was found in  Bell labs USA. The Birth of C language is originally on 1972. After the time C is widely popular all around the world. C is also used in to develop many OS system some are Linux and Windows. Now days C is still widely used or popular language in Programming world. Here, we have a question is What is a Program ?  A Program is a set of instruction which on   execution by a machine produces a specific result. A machine needs set of instructions to perform an operation. Those set of instruction are contained in a program. What is program? What is Programming languages ? What Is a Programming Languages? Meaning- A programming language allows a programmer to develop instructions which can be executed by a machine. A programming language acts as an intermediate bet

What is Blogger? How we create a account on it.

What is Blogger ? How we create a account on it. Welcome to blog. Here I am talking about blog on blogger.com. Hope you guys like it, Thanks. Here, I will talk about Ø What is blog. Ø About blogger. Ø How to create an account on blogger. Ø Can we earn money to blog. What is Blog Blog is a kind of platform, where you can write your thinking about that particular topic. Also you can say blog is article which we write through blogger.com and WordPress, etc. After writing the blog we go to publish in internet. If we write on a unique content hope traffic will came on our blog. You should write a blog on different topics like - Games, Technical, programming, Knowledge, education, and many more you like. What is blogger.com     Blogger is website that manages by Google. Google bought blogger from Pyra labs in 2003.Blogger is free for all, it give space to our passion in his platform. Around the world millions of peoples are used blogger daily and post something every