Control Statement in C

 What are our Control Statement in C

Control Statement controls the sequence of execution of various statements in a c program. To Implement a particular control structure in a programming language, we need to learn how to use the relevant control statement in that particular language. The various types of control statements in C language are as under:

    1. Decision control iteration 

    2. Looping Iterative statement

1. Decision Control Iteration

  1. if statement

  2. if-else statement

  3. switch case

 

a. if statement

This is the most simple form of decision control statement. In this form, a set of statements are executed only if the condition is given with if evaluates to true.

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if(condition)
{
Statements ;
}
if(condition) { Statements ; }
if(condition)
{
Statements ;
}

Flowchart

Control Statement in CProgram

Program to check number is negative 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include<stdio.h>
int main()
{
int num=0;
printf(“Enter the number:”);
scanf(“%d”,&num);
if(num<0)
{
printf(“Entered number is negative”);
}
return 0;
#include<stdio.h> int main() { int num=0; printf(“Enter the number:”); scanf(“%d”,&num); if(num<0) { printf(“Entered number is negative”); } return 0;
#include<stdio.h>
int main()
{
int num=0;
printf(“Enter the number:”);
scanf(“%d”,&num);
if(num<0)
{
printf(“Entered number is negative”);
}
return 0;

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter the number:-2
Entered number is negative
Enter the number:-2 Entered number is negative
Enter the number:-2
Entered number is negative

b. if-else statement

This is a bi-directional control statement. If the condition is true first statement will execute if the condition is false second condition will be executed.

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
if(condition)
{
statement1;
}
else
{
statement2;
}
if(condition) { statement1; } else { statement2; }
if(condition)
{
statement1;
}
else
{
statement2;
}

Flowchart

Control Statement in C

 

 

Program

Program to check number is positive or negative 

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
void main()
{
int n;
printf("Enter a number :");
scanf("%d", &n);
if (num >= 0){
printf("%d is a positive number \n", n);
}
else{
printf("%d is a negative number \n", n);
}
#include <stdio.h> void main() { int n; printf("Enter a number :"); scanf("%d", &n); if (num >= 0){ printf("%d is a positive number \n", n); } else{ printf("%d is a negative number \n", n); }
#include <stdio.h>
void main()
{
int n;
printf("Enter a number :");
scanf("%d", &n);
if (num >= 0){
printf("%d is a positive number \n", n);
}
else{
printf("%d is a negative number \n", n);
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Enter a number :5
5 is a positive number
Enter a number :5 5 is a positive number
Enter a number :5
5 is a positive number

 

c. Switch Case in C 

 The switch declaration is easy to understand if more than 3 alternatives exist. The switch statement permits multiple branching. We can define various statements in multiple cases for the different values of a single variable.
Syntax
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
switch (expression)
{
case constant1 :
statement1;
break;
case constant2 :
statement2;
break;
..........
default :
statement3;
}
switch (expression) { case constant1 : statement1; break; case constant2 : statement2; break; .......... default : statement3; }
switch (expression)
{
case constant1 :
statement1;
break;
case constant2 :
statement2;
break;
..........
default :
statement3;
}

Flowchart of the switch statement.

Control Statement in C
Program
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
include <stdio.h>
void main()
{
int i=2;
switch(i)
{
case 1: printf("Case1: I am case 1");
case 2: printf("Case1: I am case 2");
case 3: printf("Case1: I am case 3");
default: printf("Default: I am default");
}
}
include <stdio.h> void main() { int i=2; switch(i) { case 1: printf("Case1: I am case 1"); case 2: printf("Case1: I am case 2"); case 3: printf("Case1: I am case 3"); default: printf("Default: I am default"); } }
include <stdio.h> 
void main() 
{ 
int i=2; 
switch(i)
{ 
case 1: printf("Case1: I am case 1");
case 2: printf("Case1: I am case 2");
case 3: printf("Case1: I am case 3"); 
default: printf("Default: I am default"); 
} 
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Default:I am case 2
Default:I am case 2
Default:I am case 2

 

2. Loop Control Insertion

a. do-while

In this loop, the structure condition is checked at the end of the loop so at least one loop will get executed. The loop body is always executed at least once in the do-while loop

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
do
{
statement;
}while(condition);
do { statement; }while(condition);
do
{
statement;
}while(condition);

FlowchartControl Statement in CProgram

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
int i = 2;
do {
printf("Hello World\n");
i++;
}
while (i < 1);
return 0;
}
#include <stdio.h> int main() { int i = 2; do { printf("Hello World\n"); i++; } while (i < 1); return 0; }
#include <stdio.h>
int main()
{
    int i = 2;
    do {
        printf("Hello World\n");
 
        i++;
    }
    while (i < 1);
    return 0;
}

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
Hello world
Hello world
Hello world

 

b. while loop

In this loop, the structure condition is checked first so if the condition gets true then the only loop will be executed. The while loop is mostly used
in the case where the number of iterations is not known in advance.

 

Syntax
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
while(condition)
{
statement;
}
while(condition) { statement; }
while(condition) 
{ 
  statement;
}

FlowchartControl Statement in C Program

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main ()
{
int a=1;
while( a < 5 )
{
printf("value of a: %d\n", a); a++;
}
}
#include <stdio.h> int main () { int a=1; while( a < 5 ) { printf("value of a: %d\n", a); a++; } }
#include <stdio.h>
 int main () 
{ 
int a=1; 
while( a < 5 )
 {
 printf("value of a: %d\n", a); a++;
 } 
}
Output
Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
value of a: 1
value of a: 2
value of a: 3
value of a: 4
value of a: 1 value of a: 2 value of a: 3 value of a: 4
value of a: 1 
value of a: 2
value of a: 3
value of a: 4

c. for loop in C

It is a count control loop in the sense that the program knows in advance how many times the loop has to be executed. For loop repeat a statement or group of statements while a given condition is true.

Syntax

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
for (intialization; decision; increment/decrement)
{
statement;
}
for (intialization; decision; increment/decrement) { statement; }
for (intialization; decision; increment/decrement)
 {
 statement;
 }

Flowchart

Control Statement in CProgram

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
#include <stdio.h>
int main()
{
int i;
for (i = 1; i < 5; ++i)
{
printf("%d ", i);
}
return 0;
}
#include <stdio.h> int main() { int i; for (i = 1; i < 5; ++i) { printf("%d ", i); } return 0; }
#include <stdio.h>
 int main() 
{ 
int i; 
for (i = 1; i < 5; ++i)
 {
 printf("%d ", i);
 } 
return 0;
 }

Output

Plain text
Copy to clipboard
Open code in new window
EnlighterJS 3 Syntax Highlighter
1 2 3 4
1 2 3 4
1 2 3 4

 

 

Also, read the Array of Structure.

 

Share this post

Leave a Reply

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