C- Programming
Sujan Karki
Email: sujan074bct045@ioepc.edu.np
Contact No.: 9819387234
Master’s in Information System Engineering (MscIne) *Ongoing
Bachelor in Computer Engineering – Purwanchal Campus,IOE
UNIT 6
CONTROL STATEMENTS
2
Control Statements
⚫The statements which alter the flow of execution of the
program are known as control statements.
Why Control Statements?
⚫to do certain calculations/tasks depending on whether
a condition or testis true or false.
⚫to perform repeated actions or skip some statements.
3
Types of Control
Statements
⚫Decision Making(or branching) Statements
1. If statement
2. If…else statement
3. Nested if...else statement
4. If…Else if ladder statement
5. Switch statement
⚫Loop statement or Iteration
1. For loop
2. While loop
3. Do…while loop
4
Decision making statements
⚫If statement
If a single statement or block of statements has to be
executed only when the given condition is true, if-
statement is used.
Syntax:
if (condition)
{
//Statement(s);
}
5
⚫Example:
Program to test whether the given number is negative:
#include<stdio.h>
Void main() {
int n;
printf("Enter a number");
scanf("%d",&n);
if(n<0) {
printf("The number is negative");
}
}
6
Decision making
statements
⚫If…else statement
The if-else statement is used when there are only two
possible actions-one happens when a test condition is
true, and other when it is false.
Syntax:
if (condition)
{
//Statement(s);
}
else
{
//Statement block
}
7
⚫Example:
Program to test whether the given number is odd or even:
#include<stdio.h>
Void main() {
int a;
printf("Enter a number");
scanf(“%d",&a);
if (a%2==0) {
printf("%d is even", a);
}
else {
printf("%d is odd", a);
}
}
8
Decision making statements
⚫Nested If…else statement
The nested if-else statement includes if-else statement
inside if-else statement itself.
Syntax:
else
if(condition1)
{
{
if(condition3)
if(condition2)
{
{
//statements;
//statements;
}
}
else
else
{
{
//statements;
//statements;
}
}
}
}
9
⚫ Nested If…else statement Flowchart
10
⚫Example:
Program to ask three number from user and find the greatest:
#include <stdio.h>
int main() {
int n1, n2, n3; else
printf("Enter three numbers: "); {
scanf("%d %d %d", &n1, &n2, &n3); if (n2 >= n3)
if (n1 >= n2) {
{ printf("%d is the largest number.", n2);
if (n1 >= n3) }
{ else
printf("%d is the largest number.", n1); {
} printf("%d is the largest number.", n3);
else }
{ }
printf("%d is the largest number.", n3); return 0;
} }
}
11
Decision making
statements
⚫If…else-if ladder statement
Else if is used when multipath decisions are required.
Syntax:
if (condition) {
//Statement(s);
} .
elseif (condition) { .
//statement; .
} .
elseif (condition) { else
//statement; {
} //Statement block
elseif (condition) { }
//statement;
}
.
.
12
.
⚫Example:
Program to ask three number from user and find the greatest:
#include <stdio.h>
Void main() {
int n1, n2, n3;
printf("Enter three numbers: ");
scanf("%d %d %d", &n1, &n2, &n3);
if (n1 >= n2 && n1>=n3) {
printf("%d is the largest number.", n1);
}
elseif (n2>=n1 && n2>=n3) {
printf("%d is the largest number.", n2);
}
else {
printf("%d is the largest number.", n3);
}
}
13
⚫ If…else-if ladder statement Flowchart
14
Decision making statements
⚫Switch case statement
When there are a number of options available and one of
them is to be selected on the basis of some criteria,
switch statement is used..
Syntax:
switch (expression) {
case constant1:
// statements;
break;
case constant2:
// statements;
break;
.
.
default:
// default statements
15 }
⚫Example:
Write a program to find whether a input char is vowel or
consonant using switch statement.
case 'E':
#include <stdio.h> printf("Vowel");
void main() { case 'i': break;
char ch; printf("Vowel"); case 'I':
printf("Enter a character:"); break; printf("Vowel");
scanf("%c", &ch); case 'o': break;
switch(ch) printf("Vowel"); case 'O':
{ break; printf("Vowel");
case 'a': case 'u': break;
printf("Vowel"); printf("Vowel"); case 'U':
break; break; printf("Vowel");
case 'e': case 'A': break;
printf("Vowel"); printf("Vowel"); default:
break; break; printf("Consonant");
}
}
16
⚫Loop statement or Iteration
⚫for statement
The for loop is the most commonly used looping in C.
Syntax:
for(initialization; condition expression; increment/decrement)
{
// Statements or Body of loop
}
Where,
Initialization part: Where value is assigned to the variable.
Conditional: where the condition is checked until the
expression is true.
Increment or decrement part: Which change index value of
the for loop.
17
⚫ FOR statement Flowchart
18
⚫Example:
Write a program to calculate sum of first n natural number.
#include <stdio.h>
Void main() {
int n, i, sum=0;
printf("\n Enter a number n:");
scanf("%d", &n);
for(i=1; i<=n; i++)
{
sum=sum+i;
}
printf("\nThe sum is %d", sum);
}
19
⚫Loop statement or Iteration
⚫While Loop statement
The statements are executed continuously till the condition is
true and when it becomes false, the loop terminates and the
control comes out of the loop.
Syntax:
Initialization;
while(condition_expression)
{
//Body of loop
//update statement(increment/decrement)
}
20
⚫ While statement Flowchart
21
⚫Example:
Write a program to calculate factorial of a number using
while loop.
#include <stdio.h>
Void main() {
int n, i;
long int fact;
printf("Enter the integer:");
scanf("%d", &n);
fact=1, i=1;
while(i<=n) {
fact=fact*i;
i++;
}
printf("Factorial of %d is %ld", n, fact);
}
22
⚫Loop statement or Iteration
⚫Do…While Loop statement
Firstly the segments inside the loop body are executed and
then the condition is evaluated. If the condition is true, then
again the loop body is executed and this process continues
until the condition becomes false.
Syntax:
initialization;
do {
//Body of loop
//update statement(increment/decrement)
}while(condition_expression);
23
⚫ Do…While statement Flowchart
24
⚫Example:
Write a program to print the even number up to n using do-
while loop.
#include <stdio.h>
Void main() {
i=1;
do {
if(i%2==0)
printf("%d\t",i);
i++;
}while(i<=n);
}
25
Difference between while and do-while loop:
1. The while loop is pre-test loop, where firstly the condition is checked and
if the condition is true then only the statements of the while loop execute.
The do-while loop is a post-test loop. In the do-while loop, the statements of
the do-while loop are executed after that, the condition is evaluated, and if
the condition is true then again the statements of the do-while loop are
executed.
2.Syntax
3. While loop can’t be terminated with a semicolon but the do-while loop
should be terminated with a semicolon.
4. The statements of the do-while loop execute at least 1 time in every
condition. In the while loop, the test expression evaluates false in first
checking then the statements of the while loop is not executed.
26
Write a program to compute and print the sum
of given numbers of squares.
[12+22+ 32+42+………n2]
27
#include<stdio.h>
void main() {
int n, i, sum=0;
printf("Enter a value of n:");
scanf("%d",&n);
for(i=1;i<=n;i++)
{ #include<stdio.h>
sum=sum+i*i; void main() {
} int n, i, sum=0;
printf("\nThe sum is:%d", sum); printf("Enter a value of n:");
} scanf("%d",&n);
i=1;
#include<stdio.h>
while(i<=n) {
void main() {
sum=sum+i*i;
int n, i, sum=0;
i++;
printf("Enter a value of n:"); }
scanf("%d",&n); printf("\nThe sum is:%d", sum);
i=1; }
do {
sum=sum+i*i;
i++;
} while(i<=n)
printf("\nThe sum is:%d", sum);
28
}
⚫Nested Loop
When a loop is written inside the body of another loop, then it
is known as nesting of loops. Any type of loop can be nested
inside any other type of loop.
29
⚫break statement
The break statement is used to terminate loops or exit from a
switch. It can be used within a for, while, do –while, or switch
statement.
E.g.
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
for (i = 1; i <= 10; ++i) {
printf("Enter a number”);
scanf("%lf", &number);
if (number < 0.0)
{
break;
}
sum = sum + number;
}
30
printf("Sum = %lf", sum);
}
⚫Continue Statement
The continue statement skips the current iteration of the loop
and continue with the next iteration.
E.g.
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
for (i = 1; i <= 10; ++i) {
printf("Enter a number”);
scanf("%lf", &number);
if (number < 0.0)
{
continue;
}
sum = sum + number;
}
printf("Sum = %lf", sum);
31 }
⚫goto Statement
The goto statement allows us to transfer control of the
program to the specified label.
E.g.
#include <stdio.h>
int main() {
int i;
double number, sum = 0.0;
for (i = 1; i <= 10; ++i) {
printf("Enter a number”);
scanf("%lf", &number);
if (number < 0.0) {
goto hello;
}
sum = sum + number;
}
hello:
printf("Sum = %lf", sum);
}
32
. . . to be continued !!!
33