0% found this document useful (0 votes)
8 views23 pages

Unit2 C Programming

C programming
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)
8 views23 pages

Unit2 C Programming

C programming
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/ 23

UNIT – 2: CONTROL STRUCTURES IN C PROGRAMMING

1. Simple Sequential Programs


Definition:
A sequential program is one where statements are executed in
the same order as they are written, without any branching or
repetition.
Explanation:
- Default flow of execution in C.
- Each instruction executes once.
- Useful for simple problems like addition, area calculation, etc.
Example:
#include <stdio.h>
int main() {
int a, b, sum;
printf("Enter two numbers: ");
scanf("%d %d", &a, &b);
sum = a + b;
printf("Sum = %d", sum);
return 0;
}
Output:
Enter two numbers: 10 20
Sum = 30

Control Structures
The statements which are used to know the flow of execution
of a program or to alter the execution of sequence of
statements based on the requirement are called “Control
statement or Control Structures”.
Based on application, it is necessary / essential
To alter the flow of a program
To test the logical conditions
To control the flow of execution based on statements used.

2. Conditional Statements
Definition:
Conditional statements are also called decision-making
statements. They allow a program to choose between
alternative paths of execution based on conditions.
Types:
(a) if statement
(b) if-else
(c) else-if ladder
(d) nested if
(e) switch-case
(a) if Statement
Definition:
Simple if statement is used when user has only one option that
is executed or skipped based on a condition.

// Example program on “Simple if” statement.


#include<stdio.h>
void main()
{
int n=0;
printf("Enter a number:");
scanf("%d",&n);
if(n%2==0)
{
printf("%d is even number",n);
}
}
Output:
Enter a number: 10
10 is even number

(b) if-else Statement


The if-else statement is used when user have two options and
only one option has to be executed based on a condition result
(TRUE or FALSE).
FLOWCHART AND SYNTAX

// Example program on if-else statement.


#include<stdio.h>
void main()
{
int n;
printf("enter a number:");
scanf("%d", &n);
if(n%2==0)
{
printf("%d is even number",n);
}
else
{
printf("%d is odd number",n);
}
}
Output:
enter a number: 7
7 is odd number
(c) else-if Ladder
This is another way of putting if‘s together when multiple
decisions are involved. A multipath decision is a chain of if‘s in
which the statement associated with each else is an if. Hence it
forms a ladder called else–if ladder.
if else-if, statement is used to execute one code from multiple
conditions

FLOWCHART

// Example program on “else – if ladder” statement.


#include<stdio.h>
void main()
{
int n;
printf("enter a number:");
scanf("%d",&n);
if(n==10)
printf("number is equals to 10");
else if(n==50)
printf("number is equal to 50");
else if(n==100)
printf("number is equal to 100");
else

printf("number is not equal to 10, 50 or 100");


}
Output:
enter a number: 50
number is equal to 50

(d) Nested if
Using of one if-else statement in another if-else statement is
called as nested if-else control statement.
When a series of decisions are involved, we may have to use
more than one if-else statement in nested form.

FLOWCHART

Example:
#include <stdio.h>
int main() {
int num;
printf("Enter a number: ");
scanf("%d", &num);

if (num > 0) { // First condition: number must be


positive
if (num % 2 == 0) { // Second condition: number must
be even
printf("The number is Positive and Even\n");
} else {
printf("The number is Positive but Odd\n");
}
} else {
printf("The number is not Positive\n");
}

return 0;
}
Output:
Enter a number: 10
The number is Positive and Even
(e) switch-case
Switch is another conditional control statement used to select
one option from several options based on given expression
value.
This is an alternative to the if-else-if ladder.
switch and case values must be either integer or character
but not float or string.
The default case is optional and it can be defined anywhere
inside the switch statement.
FLOWCHART

// Example program on SWITCH statement.


#include<stdio.h>
void main()
{
int n;
printf("enter a number:");
scanf("%d",&n);
switch(n)
{
case 10: printf("number is equals to 10");
break;
case 50: printf("number is equal to 50");
break;
case 100: printf("number is equal to 100");
break;

default: printf("number is not equal to 10, 50 or 100


");
}
}

Output:
50
number is equal to 50

3. Loops
Loops allow repetition of a block of code until a condition
becomes false.
Types: for, while, do-while
(a) for Loop
The for statement is used to execute a single statement or a
block of statements repeatedly as long as the given condition is
TRUE.
FLOWCHART

// Example program on FOR loop statement.


#include<stdio.h>
void main()
{
int i;
scanf(“%d”,&n);
for(i=1;i<=n;i++)
{
printf(“%d \n”,i);
}
}
Output:
7
1234567
(b) while Loop
The while statement is used to execute a single statement or
block of statements repeatedly as long as the given condition is
TRUE. The while statement is also known as Entry looping
statement.

FLOWCHART
// Example program on WHILE loop statement.
#include<stdio.h>
void main()
{
int i=1;
while(i<=10)
{
printf("%d \n",i);
i++;
}
}
Output:
1 2 3 4 5 6 7 8 9 10

(c) do-while Loop


The do-while statement is used to execute a single statement
or block of statements repeatedly as long as given the
condition is TRUE. The do-while statement is also known as
the Exit looping statement.

// Example program on DO-WHILE loop statement.


#include<stdio.h>
void main()
{
int i=1;
do
{
printf("%d \n",i);
i++;
} while(i<=10);
}
Output:
1 2 3 4 5 6 7 8 9 10.

4. Jump Statements
(a) break
The break statement is used:
– to terminate the switch case statement
– to terminate looping statements like while, do-while
and for.
FLOWCHART

// Example program on BREAK statement.


#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
{
break;
}
}
printf("came outside of loop i = %d",i);
}
Output:
01234

(b) continue
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 Example:
// Example program on CONTINUE statement.
#include<stdio.h>
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
{
continue;
}
}
printf("came outside of loop i = %d",i);
}
Output:
012346789

c) goto statement:
C supports the “goto” statement to branch unconditionally
from one point to another in the program.
The goto requires a label in order to identify the place where
the branch is to be made.
A label is any valid variable name and must be followed by a
colon (: ).
The label is placed immediately before the statement where
the control is to be transferred.
The label can be anywhere in the program either before or
after the goto label statement.
// Example program on GOTO statement.
#include <stdio.h>
void main()
{
int n,i=1;

printf("Enter the number whose table you want to pri


nt?");
scanf("%d",&n);
table: printf("%d x %d = %d\n",n,i,n*i);
i++;
if(i<=10)
goto table;
}
Output:
Enter the number whose table you want to print? 5
5x1=5
5 x 2 = 10
5 x 3 = 15
5 x 4 = 20
5 x 5 = 25
5 x 6 = 30
5 x 7 = 35
5 x 8 = 40
5 x 9 = 45
5 x 10 = 50

You might also like