Freelancer

Control Statements - C programming examples

Control Statements of C programming language with examples

To learn C Programming we have to go through all the control statements as they are necessary in each and every coding that we do. The main goal of programming is to use loops so that we can calculate enormous sized mathematical calculations within seconds. 

Control Statements

Control logo - C Programming Examples
Control - C Programming Examples
Without control statements, all a program can do is to execute the code from the top to the bottom once and for all which is probably not much useful. With control statements, a program can branch out to different parts and repeat operations as many times as need be. The following are the five types of control statements that can control the flow of the program:

• if .... else
• for
• while
• do while
• switch

IF STATEMENT IN C PROGRAMMING

A block (a set of statements surrounded by { and }) following the if statement is executed when the condition inside (. . . . .) is satisfied. If there is no block, the next statement is executed. else is optional.

The following program tells whether an integer entered from the keyboard is between 1 and 100 or not:

#include <stdio.h>
int main()
{
int i;
printf("Enter an integer = ");
scanf("%d",&i);
if (i>1 && i<100)
printf("The number is between 1 and 100.\n");
else
printf("The number is not in that range.\n");
return 0;
}

The output looks like:
 
If-Else statement - C Programming Examples
If-Else statement - C Programming Examples

If-Else statement - C Programming Examples
If-Else statement - C Programming Examples

FOR STATEMENT OF C PROGRAMMING

A for loop is to be used when the number of iterations to be executed is known in advance. The for loop consists of three parts each of which is separated by a semicolon (;). The first part is initialization of a counter variable, the second part is a test of the iteration counter variable and if this test fails, the loop is finished. The third part defines an action on the counter variable. A block can be followed after the for statement to execute multiple statements for each iteration.

for (initial value; condition ; counter increment) statement;

Examples

1. The following program prints 0–9.

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

The output is:
For statement - C Programming Examples
For statement - C Programming Examples

2. The following program computes
S = 1 + 2 + 3 + 4 + 5 + 6 + ……….. + 100

#include <stdio.h>
int main()
{
int i, sum = 0;
for (i=0; i<= 100; i++) sum = sum + i;
printf("Sum = %d\n", sum);
return 0;
}

The output is
For statement - C Programming Examples
For statement - C Programming Examples

In this program, i is a counter variable and sum is a placeholder for the sum. The parameters
in the for statement mean that the counter variable, i, is reset to be 0 first and as long
as i is less than or equal to 100, the sum = sum + 1 statement is repeated. Upon each
execution of sum = sum + 1, i is incremented by 1. In the sum = sum + 1 statement,
the previous value of sum is incremented by 1 at each iteration and the newly incremented
value of sum+1 replaces the previous value of sum.
The following pattern can compute mathematical summation:
sum = 0.0;
for (i=0; i<= 100; i++)      sum = sum + f(i);

Note: The iteration variables (i, j, k, . . . . ) must be always declared as int.

WHILE STATEMENT OF C PRO

A while statement executes the statement(s) that follows while the condition is true.

Note that the following program outputs 10:

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

The output is
While statement - C Programming Examples
While statement - C Programming Examples
It appears that the program should output 9, not 10. However, in the test i<10 when i=9, i is
incremented to 10 and this value is held thereafter.
For multiple statements, it is necessary to use a block with curly brackets 
({ and }).

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

The output is
While statement - C Programming Examples
While statement - C Programming Examples

DO WHILE STATEMENT OF C PROGRAMMING

The do while loop is similar to the while loop except that the test occurs at the end of the loop body. This guarantees that the loop is executed at least once before continuing. Such a setup is frequently used where data is to be read. The loop is used to re-read the data if the first set was unacceptable.

The following program keeps prompting until the user enters 0 or 1:

#include <stdio.h>
int main()
{
int input_value;
do
{ printf("Enter 1 for yes, 0 for no :");
scanf("%d", &input_value);
} while (input_value != 1 && input_value != 0);
return 0;
}

The output is
 Do While statement - C Programming Examples
Do While statement - C Programming Examples

SWITCH STATEMENT OF C PROGRAMMING

A switch statement can branch out to different tasks depending on the value of the variable used. You can achieve the same result using multiple if statements but using switch simplifies the program flow.

The following program checks to see if the value entered is either 1 or 2 and prints “a is neither 1 nor 2.” if otherwise:

#include <stdio.h>
int main()
{
int i;
printf("Enter an integer=");
scanf("%d", &i);
switch(i)
{
case 1: printf("a is 1\n");break;
case 2: printf("a is 2\n");break;
default: printf("a is neither 1 nor 2\n");break;
}
return 0;
}

The output is
Switch statement - C Programming Examples
Switch statement - C Programming Examples

It is important to use a break statement to break out from the block for each statement executed. 
Note that a colon (:) must be used instead of a semicolon (;) after case.

Exit from an infinite loop in C programming

From time to time, the program you wrote may be trapped in an infinite loop and nothing can be done except for closing the window or shutting down the machine. 

For instance, the following program generates an infinite loop:

#include <stdio.h>
int main()
{
int i;
for (i=1; i>0; i++) printf("loop");
return 0;
}

The output is:
Infinite Loop - C Programming Examples
Infinite Loop - C Programming Examples


To exit from the infinite loop, enter control+C

This was a brief discussion about the control statements which are the backbones of different codes that we go through in our regular practices.

No comments

Powered by Blogger.