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:
-
-
Decision control iteration
-
Looping Iterative statement
-
1. Decision Control Iteration
-
if statement
-
if-else statement
-
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
if(condition) { Statements ; }
Flowchart
Program
Program to check number is negative
#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
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
if(condition) { statement1; } else { statement2; }
Flowchart
Program
Program to check number is positive or negative
#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
Enter a number :5 5 is a positive number
c. Switch Case in C
switch (expression) { case constant1 : statement1; break; case constant2 : statement2; break; .......... default : statement3; }
Flowchart of the switch statement.
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
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
do { statement; }while(condition);
FlowchartProgram
#include <stdio.h>
int main()
{
int i = 2;
do {
printf("Hello World\n");
i++;
}
while (i < 1);
return 0;
}
Output
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
while(condition) { statement; }
Flowchart Program
#include <stdio.h> int main () { int a=1; while( a < 5 ) { printf("value of a: %d\n", a); a++; } }
Output
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
for (intialization; decision; increment/decrement) { statement; }
Flowchart
Program
#include <stdio.h> int main() { int i; for (i = 1; i < 5; ++i) { printf("%d ", i); } return 0; }
Output
1 2 3 4
Also, read the Array of Structure.