0% found this document useful (0 votes)
28 views18 pages

Control Satement C

c program control statements

Uploaded by

Karthiga Murugan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
28 views18 pages

Control Satement C

c program control statements

Uploaded by

Karthiga Murugan
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

SREE SOWDAMBIKA COLLEGE OF ENGINEERING

CHETTIKURICHI, ARUPPUKOTTAI-626134

Department of Electronics and Communication Engineering

CONTROL STATEMENTS IN C

Control statements in C
In C, the control flows from one statement to next statement until the end
of the program. This kind of execution is called sequential control flow.
Why we use control statements
The programmer may want to skip or repeat a set of instructions
repeatedly as per their requirements. Control statement will be very useful for
their requirements.
Types of control statements in C

Control
statements

Conditional Looping Jump


statements statements statement
Conditional statements
• Also known as Selection statements, Branching statements.
• Used to execute codes based on certain condition
• e.g: Amount can be withdrawn from ATM only if we enter valid PIN.
These situations are handled using conditional statements.
Types of Control statements
• Simple if statement
• if…else statement
• Nested if statement
• else-if ladder

Simple if statement
If the given condition is true, then the statements inside the body of if
blocks get executed.
If the given condition is false, the program moves on to the next
statement.

Example program:

#include <stdio.h>

int main()
{
int number = 5;

if (number > 0)
{
printf("The number is positive.\n");
}

return 0;
}
Output:
The number is positive
if…else statement
The if…else statement evaluates the given condition.
• If it is true, it executes the true block of statements.
• If it is false, it executes the false block of statements.

Example program:
#include<stdio.h>
#include<conio.h>

void main()
{
int n;
clrscr();
printf("Enter any integer number: ");
scanf("%d", &n);
if (n%2 == 0)
printf("Given number is EVEN\n");
else
printf("Given number is ODD\n");
}

Output:
Enter any integer number: 10
Given number is EVEN
Nested if statement
Writing a if statement inside a another if statement is called nested if statement.

Example program
#include<stdio.h>
#include<conio.h>

void main()
{
int n;
clrscr() ;
printf("Enter any integer number: ");
scanf("%d", &n);
if (n < 100)
{
printf("Given number is below 100\n");
if(n%2 == 0)
printf("And it is EVEN");
else
printf("And it is ODD");
}
else
printf("Given number is not below 100");
}

Output:
Enter any integer number: 75
Given number is below 100
And it is ODD
if-else-if statement (else-if ladder)
The else-if ladder is used when you need to test multiple conditions. It’s a
sequence of if statements, each checking a different condition. When a
condition is true, the corresponding block of code runs, and the rest are ignored.
Example program
#include<stdio.h>
#include<conio.h>

void main()
{
int a, b, c;
clrscr();

printf("Enter any three integer numbers: ");


scanf("%d%d%d", &a, &b, &c);

if(a>=b && a>=c)


printf("%d is the largest number", a);

else if (b>=a && b>=c)


printf("%d is the largest number", b);

else
printf("%d is the largest number", c);
}

Output:
Enter any three integer numbers: 10 20 35
35 is the largest number

Switch Statement
The switch statement in C is a decision control statement that allows you to
choose between multiple options based on the value of a variable or expression.
The switch statement contains one or more cases and each case has a value
associated with it. At first switch statement compares the first case value with
the switch Value, if it gets matched the execution starts from the first case. If it
doesn't match the switch statement compares the second case value with the
switch Value and if it is matched the execution starts from the second case. This
process continues until it finds a match. If no case value matches with the
switch Value specified in the switch statement, then a special case
called default is executed.
When a case value matches with the switch Value, the execution starts from that
particular case. This execution flow continues with the next case statements
also. To avoid this, we use the "break" statement at the end of each case. That
means the break statement is used to terminate the switch statement. However,
it is optional.
.
Example program:
#include<stdio.h>
#include<conio.h>

void main()
{
int n;
clrscr();

printf("Enter any digit: ");


scanf("%d", &n);

switch(n)
{
case 0: printf("ZERO");
break;
case 1: printf("ONE");
break;
case 2: printf("TWO");
break;
case 3: printf("THREE");
break;
case 4: printf("FOUR");
break;
case 5: printf("FIVE");
break;
case 6: printf("SIX");
break;
case 7: printf("SEVEN");
break;
case 8: printf("EIGHT");
break;
case 9: printf("NINE") ;
break;
default: printf("Not a Digit");
}
getch();
}
Output:
Enter any digit: 7
SEVEN
LOOPING STATEMENTS
Loop statements are used to execute a block of code repeatedly until a certain
condition is met.
Types:
While loop
Do-While loop
For loop
While loop
The while loop continues executing as long as the specified condition remains
true. The condition is checked before executing the loop body, so if the
condition is false initially, the body may not run at all.

Example program:
#include<stdio.h>
#include<conio.h>

void main()
{
int n = 0;
clrscr();
printf("Even numbers upto 10\n");

while n <= 10)


{
if(n%2 == 0)
printf("%d\t", n);
n++;
}

getch();
}
Output:
Even numbers upto 10
0 2 4 6 8 10
While using the while statement we must follow the rules,
• while is a keyword so it must be used only in lower case letters.
• If the condition contains a variable, it must be assigned a value before it
is used.
• The value of the variable used in condition must be modified according to
the requirement inside the while block.
• In a while statement, the condition may be a direct integer value, a
variable or a condition.
• A while statement can be an empty statement.
Do-while
The do-while loop is similar to the while loop, but with one key difference: the
condition is checked after the loop body is executed. This means the loop body
will always run at least once, even if the condition is false initially.
At first, the single statement or block of statements which are defined
in do block are executed. After the execution of the do block, the given
condition gets evaluated. If the condition is evaluated to TRUE, the single
statement or block of statements of do block are executed again. Once the
execution gets completed again the condition is evaluated. If it is TRUE, again
the same statements are executed. The same process is repeated until the
condition is evaluated to FALSE. Whenever the condition is evaluated to
FALSE, the execution control moves out of the while block.

Example program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n = 0;
clrscr();
printf("Even numbers upto 10\n");
do
{
if(n%2 == 0)
printf("%d\t", n);
n++;
}while n <= 10);

getch();
}
Output:
Even numbers upto 10
0 2 4 6 8 10
When we use the do-while statement we must follow the following rules
• Both do and while are keywords so they must be used only in lower case
letters.
• If the condition contains a variable, it must be assigned a value before it
is used.
• The value of the variable used in the condition must be modified
according to the requirement inside the do block.
• In a do-while statement, the condition may be a direct integer value, a
variable or a condition.
• A do-while statement can be an empty statement.
• In do-while, the block of statements is executed at least once.
For Statement
The for loop is used to execute a block of code a specific number of times.
The for loop is used when you know the number of iterations beforehand.
It consists of three parts: the initialization statement (executed only once at the
beginning of the loop), the condition statement (checked before each
iteration), and the update statement (executed at the end of each iteration)
Example program:
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("Even numbers upto 10\n");

for(n = 0; n <= 10; n++)


{
if(n%2 == 0)
printf("%d\t", n);
}

getch();
}
Output:
Even number upto 10
0 2 4 6 8 10
When we use for statement we must follow the following rules
• for is a keyword so it must be used only in lower case letters.
• Every for statement must be provided with initialization, condition, and
modification (They can be empty but must be separated with ";")
Ex: for ( ; ; ) or for ( ; condition ; modification ) or for ( ; condition ; )
• In for statement, the condition may be a direct integer value, a variable or
a condition.
• The for statement can be an empty statement.

JUMP STATEMENTS
Allows you to control the flow of your program by skipping parts of code
or jumping to specific locations in the program. These statements are part of the
types of control statements in C, and they provide you with flexibility when you
need to exit loops early, skip iterations, or return from functions.
C programming language provides the following unconditional control
statements...
• break
• continue
• goto
Break Statement
In C, the break statement is used to perform the following two things...
1. break statement is used to terminate the switch case statement
2. break statement is also used to terminate looping statements like while,
do-while and for.
Example program:
#include <stdio.h>

int main() {
// For loop that breaks when count reaches 3
for (int count = 1; count <= 5; count++)
{
if (count == 3)
{
break; // Exit the loop when count is 3
}
printf("%d ", count); // Print current value of count
}

return 0;
}
Output:
12
Continue statement
The continue statement is used to move the program execution control to
the beginning of the looping statement. When the continue statement is
encountered in a looping statement, the execution control skips the rest of the
statements in the looping block and directly jumps to the beginning of the loop.
The continue statement can be used with looping statements like while, do-
while and for.
When we use continue statement with while and do-while statements the
execution control directly jumps to the condition. When we
use continue statement with for statement the execution control directly jumps
to the modification portion (increment/decrement/any modification) of the for
loop. The continue statement execution is as shown in the following figure.

Example program:
#include <stdio.h>

int main() {
// For loop that skips printing 3
for (int count = 1; count <= 5; count++) {
if (count == 3) {
continue; // Skip this iteration when count is 3
}
printf("%d ", count); // Print current value of count
}

return 0;
}
Output:
1245
Explanation:
• The continue statement is executed when count is 3, causing the loop to
skip the rest of the code for that iteration.
• Therefore, 3 is not printed, and the loop continues with the next iteration.
Goto Statement:
The goto statement is used to jump from one line to another line in the program.
Using goto statement we can jump from top to bottom or bottom to top. To
jump from one line to another line, the goto statement requires a label. Label is
a name given to the instruction or line in the program. When we use
a goto statement in the program, the execution control directly jumps to the line
with the specified label.
Example program:
#include<stdio.h>
#include<conio.h>
void main()
{
clrscr();
printf("We are at first printf statement!!!\n");
goto last;
printf("We are at second printf statement!!!\n");
printf("We are at third printf statement!!!\n");
last: printf("We are at last printf statement!!!\n");
getch();
}
Output:
We are at first printf statement!!!
We are at last printf statement!!!

You might also like