Control Statements
Control Statements
in a program must be executed. They make it possible to make decisions, to perform tasks repeatedly or to
jump from one section of code to another.
Objectives
Conditional
Looping
If statement
if else statement
nested if
Breaking
for loop
while loop
do while loop
break
Continue
exit
goto
If statement: - The if statement provides a selection control structure to execute a section of code if and
only if an explicit run-time condition is met. The condition is an expression which evaluates to a boolean
value, that is, either true or false.
Syntax
if ( <expression> )
{
statement
}
Semantics
If the expression evaluates to true, the statement part of the if statement is executed.
If the expression evaluates to false, execution continues with the next statement after the if
statement.
If...else statement
The if...else statement executes some code if the test expression is true (nonzero) and some other
code if the test expression is false (0).
Syntax of if...else
if (test Expression)
{
// codes inside the body of if
}
else
{
// codes inside the body of else
}
If test expression is true, code inside the body of if statement is executed; and code inside the body
of else statement is skipped.
If test expression is false, code inside the body of else statement is executed; and code inside the
body of if statement is skipped.
// True if remainder is 0
if( number%2 == 0 )
printf("%d is an even integer.",number);
else
printf("%d is an odd integer.",number);
return 0;
Output
Enter an integer: 7
7 is an odd integer.
When user enters 7, the test expression ( number%2 == 0 ) is evaluated to false. Hence, the
statement inside the body of else statement printf("%d is an odd integer"); is executed and the
statement inside the body of if is skipped.
Nested if...else statement (if...else if....else Statement)
The if...else statement executes two different codes depending upon whether the test expression is true
or false. Sometimes, a choice has to be made from more than 2 possibilities.
The nested if...else statement allows you to check for multiple test expressions and execute different
codes for more than two conditions
Syntax of nested if...else statement.
if (testExpression1)
{
// statements to be executed if testExpression1 is true
}
else if(testExpression2)
{
// statements to be executed if testExpression1 is false and testExpression2 is true
}
else if (testExpression 3)
{
// statements to be executed if testExpression1 and testExpression2 is false and testExpression3 is
true
}
.
.
else
{
// statements to be executed if all test expressions are false
}
Example #3: C nested if...else statement
// Program to relate two integers using =, > or <
#include <stdio.h>
int main()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
//checks if two integers are equal.
if(number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
//checks if number1 is greater than number2.
else if (number1 > number2)
{
printf("Result: %d > %d", number1, number2);
}
// if both test expression is false
else
{
printf("Result: %d < %d",number1, number2);
}
return 0;
Output
Enter two integers: 12
23
Result: 12 < 23
LoopLoops are used in programming to repeat a specific block until some end condition is met. There are
three loops in C programming:
for loop
while loop
do...while loop
for Loop
The syntax of a for loop is:
for (initializationStatement; testExpression; updateStatement)
{
// codes
}
How for loop works?
For loop- In this, first the expression or the variable is initialized and then condition is checked. if the
condition is false ,the loop terminates
return 0;
Output
Enter a positive integer: 10
Sum = 55
while loop
The syntax of a while loop is:
while (testExpression)
{
//codes
}
How while loop works?
The while loop evaluates the test expression.
If the test expression is true (nonzero), codes inside the body of while loop is evaluated. Then, again
the test expression is evaluated. The process goes on until the test expression is false.
When the test expression is false, the while loop is terminated.
Flowchart of while loop
return 0;
Output
Enter an integer: 5
Factorial = 120
do...while loop
The do..while loop is similar to the while loop with one important difference. The body ofdo...while loop
is executed once, before checking the test expression. Hence, thedo...while loop is executed at least
once.
do...while loop Syntax
do
{
// codes
}
while (testExpression);
return 0;
Output
Enter a number: 1.5
Enter a number: 2.4
Enter a number: -3.4
Enter a number: 4.2
Enter a number: 0
Sum = 4.70
break Statement
The break statement terminates the loop immediately when it is encountered. The break statement is
used with decision making statement such as if...else.
Syntax of break statement
break;
Flowchart of break statement
int i;
double number, sum = 0.0;
for(i=1; i <= 10; ++i)
{
printf("Enter a n%d: ",i);
scanf("%lf",&number);
// If user enters negative number, loop is terminated
if(number < 0.0)
{
break;
}
printf("Sum = %.2lf",sum);
return 0;
Output
Enter a n1: 2.4
Enter a n2: 4.5
Enter a n3: 3.4
Enter a n4: -3
Sum = 10.30
Break statement- The term break means breaking out of a block of code. The break statement has
two use, you can use it to terminate a case in the switch statement, and you can also use it to force
immediate termination of loop, bypassing the normal loop condition test
Switch Statement Flowchart
return 0;
Output
Enter an operator (+, -, *,): Enter two operands: 32.5
12.4
32.5 - 12.4 = 20.1
continue Statement
The continue statement skips some statements inside the loop. The continue statement is used with
decision making statement such as if...else.
Syntax of continue Statement
continue;
Flowchart of continue Statement
printf("Sum = %.2lf",sum);
return 0;
}
Output
Enter a n1: 1.1
Enter a n2: 2.2
Exit statement-
The purpose of exit is to terminate the current program with a specific exit code. Its prototype is:
exit (exitcode);
The exitcode is used by some operating systems and may be used by calling programs. By convention, an
exit code of 0 means that the program finished normally and any other value means that some error or
unexpected results happened
#include <stdio.h>
#include <stdlib.h>
int main ()
{
printf("Start of the program....\n");
printf("Exiting the program....\n");
exit(0);
printf("End of the program....\n");
return(0);
Output:
Start of the program....
Exiting the program....