Unit 3 SPC . (4)
Unit 3 SPC . (4)
Control Structure
• Decision making is about deciding the order of execution of statements based on certain
• In the C programming language, there are two decision-making statements they are as
follows.
1 if statement
2 if-else statement
3 switch statement.
Flowchart of the Decision-making
technique
1. if statement
• In C, "if statements" control the program flow based on a condition; it executes some
statement code block when the expression evaluates to true; otherwise, it will get
skipped.
1. Simple if Statement
2. if-else Statement
4. If-else ladder
Simple if Statement:
Example:- simple if statement
#include<stdio.h>
void main (){
int a;
printf("Enter the value of a: "); Output:-
scanf("%d",&a); Enter the value of a: 10
if(a%2==0){ a is Even number
printf("a is Even number");
}
getch(); Output:-
return 0; Enter the value of a: 11
}
Example:- simple if statement
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{ Output:-
printf("Variable x is less than y"); Variable x is less than y
}
getch();
return 0;
}
Example 1: Test whether given number is divisible by 5
# include<stdio.h> OUTPUT:
# include<conio.h> Enter any integer number: 33
int main () Statement does not belong to if!!!
{
int n;
printf (" Enter any integer number : ");
scanf ("%d", &n);
if ( n % 5 == 0 )
{
printf (" Given number is divisible by 5\n");
}
printf (" Statement does not belong to if !!!");
return 0;
}
Example 2: check whether the number is negative number
#include<stdio.h>
void main()
{
int number;
printf("Type a number:");
scanf("%d", &number);
if (number < 0)
{ // check whether the number is negative number.
number = -number; // If it is a negative then convert it into positive.
}
printf("The absolute value is %d\n", number);
getch();
}
}
2 if-else Statement:
Example Program: Test whether given number is even or odd
# include < stdio .h>
void main () Output:
{
int n ; Enter any integer number: 10
printf (" Enter any integer number : "); Given number is EVEN
scanf ("%d", &n);
OR
if ( n % 2 == 0 )
{
Enter any integer number: 15
printf (" Given number is EVEN \n");
Given number is ODD
}
else
{
printf (" Given number is ODD\n");
}
}
3 Nested if Statement
• The nested if statement can be defined using any combination of simple if & if-else statements.
Example Program: Test whether given number is even or odd if it is < 100
# include <stdio .h> printf(“Given number is not below 100”);
# include <conio .h> }
void main () OUTPUT:
{ Enter any integer number: 75
int n; Given number is below 100
printf (" Enter any integer number : "); Given number is ODD
scanf ("%d", &n); OR
if ( n < 100 ) Enter any integer number: 200
{ Given number is not below 100
printf (" Given number is below 100\ n"); OR
if( n % 2 == 0) Enter any integer number: 20
printf ("And it is EVEN "); Given number is below 100
else Given number is EVEN
printf ("And it is ODD");
}
else
Example 2:Demontration of if-else statement :-
#include<stdio.h>
void main()
{
int x=20,y=30;
if(x==20)
{
if(y==30) Output :-
{
value of x is 20, and value of y
printf("value of x is 20, and value of y is 30."); is 30.
}
else
{
printf("value of x is not 20, and value of y is not 30.");
}
}
}
2 switch statement.
• Consider a situation in which we have many options out of which we need to select
• That means the break statement is used to terminate the switch statement .
switch statement
case constant:
//C Statements
break;
default:
//C Statements
}
Example :- switch statement without break statement
#include <stdio.h>
int main()
{
int i=2;
switch (i)
{
OUTPUT
case 1:
printf("Case1 ");
Case2 Case3 Case4 Default
case 2:
printf("Case2 ");
case 3:
printf("Case3 ");
case 4:
printf("Case4 ");
default:
printf("Default ");
}
return 0;
}
Example Program: Display pressed digit in words.
# include <stdio.h>
# include <conio.h>
void main () case 4: printf (" FOUR ");
{ break ;
int n; case 5: printf (" FIVE ");
break ;
printf (" Enter any digit : ");
case 6: printf (" SIX");
scanf ("%d", &n); break ;
switch (n) case 7: printf (" SEVEN ");
{ break ;
case 8: printf (" EIGHT ");
case 0: printf (" ZERO ");
break ;
break ; case 9: printf (" NINE ");
case 1: printf (" ONE"); break ;
break ; default : printf ("Not a Digit");
case 2: printf (" TWO"); }
getch();
break ; }
case 3: printf (" THREE ");
break ;
2. Looping Statement
• Sometimes it is necessary for the program to execute the statement several
times.
1 while Statement
2 do-while Statement
3 for Statement
1. while Statement:
• The while statement is used to execute a single statement or block of statements repeatedly as long
as the given condition is TRUE.
# include <stdio.h>
# include <conio.h>
void main()
{
int n = 0; OUTPUT:
printf (" Even numbers up to 10\ n");
Even numbers up to 10
0 2 4 6 8 10
while ( n <= 10 )
{
if( n % 2 == 0)
printf ("%d\t", n);
n++;
}
getch ();
}
2. do-while Statement:
• The for statement is used to execute a single statement or block of statements repeatedly
as long as given the condition is TRUE.
• A for loop is a repetition control structure that allows you to efficiently write a loop that
needs to execute a specific number of times.
Example Program: To display even numbers upto 10
# include < stdio .h>
# include < conio .h>
void main ()
{ OUTPUT:
int n;
Even numbers up to 10
printf (" Even numbers up to10\ n"); 0 2 4 6 8 10
for (n = 0 ; n <= 10 ; n++)
{
if(n % 2 == 0)
printf ("%d\t", n);
}
getch ();
}
3. Nested Looping/Nested Control Structure
• You can define any type of loop inside another loop; for example, you can define while
• In C, there are control statements that do not need any condition to control the
program execution flow.
1. break
2. continue
3. goto
Break, Continue & goto Statement:
break:
• The break statement is used to terminate the switch case statement.
• It is used to terminate the looping statements like for, while and do-while.
goto:
• This is a kind of jump statement which can take control of the program from anywhere to anywhere within a
function.
Continue:
• It is similar to break statement, the only difference lies that break statement terminates the loop
whereas continue statement forces the control to reach for the next iteration of the loop that is to the
beginning of the loop.
break statement Syntax & Flowchart
Example Program: Use of break statement in switch-case
#include <stdio.h> int main()
void print_name( int roll_number)
{
{ int roll_number = 1;
switch(roll_number) print_name(roll_number);
getch();
{ return 0;
case 1: }
printf("\n Name is A");
break;
case 2:
printf("\n Name is B");
Output :-
break; Name is A
case 3:
printf("\n Name is C");
break;
default:
printf("\n Name is D");
break;
}
}
continue Statement Syntax & Flowchart
Example Program: Use of continue statement in do-while
Loop
# include <stdio .h> OUTPUT
int main ()
{
int j=0; 012345689
do
{ The print statement is skipped
if (j ==7) when counter value was 7.
{
j++;
continue ;
}
printf ("%d ", j);
j++;
} while (j <10) ;
return 0;
}
Syntax & Flowchart of goto Statement
Syntax:
goto abc; // abc is label
C statement ;
C Statement ;
C Statement ;
abc :C Statement ;
Example Program: Use of goto statement
#include<stdio.h>
int main ()
{
int sum=0,i=0;
for(i=0;i<=10;i++)
{
sum=sum+i; Output :-
if(i==5)
15
{
goto addition ;
}
}
addition:
printf ("%d",sum);
getch();
return 0;
}
List of Practice Programs:
1. Write a C Program to check whether x is less than y.
5. Write a C Program to print table for the given number using while loop in C.
7. Write a C Program to Print table for the given number using for loop.
8. Write a C Program uses a nested for loop to find the prime numbers from 2
to 100
1. Write a c Program to check whether x is less than y
#include <stdio.h>
int main()
{
int x = 20;
int y = 22;
if (x<y)
{
printf("Variable x is less than y");
}
return 0;
}
2. Write a C Program to print Absolute value.
#include<stdio.h>
void main()
{ OUTPUT: Type a
number: 2
int number;
The absolute value
printf("Type a number:"); is 2
scanf("%d", &number);
if (number < 0)
{ // check whether the number is negative number.
number = -number; // If it is a negative then convert it into positive.
}
printf("The absolute value is %d\n", number);
getch();
}
3. Write a C Program to check whether a person is
eligible to vote or not.
#include <stdio.h> OUTPUT:-
int main() Enter your age? 18
{ You are eligible to vote...
int age; Enter your age?13
printf("Enter your age?"); Sorry ... you can't vote
scanf("%d",&age);
if(age>=18)
{
printf("You are eligible to vote...");
}
else
{
printf("Sorry ... you can't vote");
}
}
4. Write C program to print weekday based on
given number using switch
#include<stdio.h> case 6: printf("FRIDAY.");
int main() break;
{ case 7: printf("SATURDAY.");
int day; break;
printf("Enter day number: "); default: printf("INVALID DAY.");
scanf("%d", &day); break;
switch(day) }
{ return(0);
case 1: printf("SUNDAY."); }
break; OUTPUT:
case 2: printf("MONDAY."); Enter day number : 3
break; TUESDAY
case 3: printf("TUESDAY.");
break;
case 4: printf("WEDNESDAY.");
break;
case 5: printf("THURSDAY.");
break;
5. Write a C Program to print table for the given number using while
loop in C
#include<stdio.h> OUTPUT:
int main() Enter a number: 3
{ 3
int i=1,number=0,b=9; 6
printf("Enter a number: "); 9
scanf("%d",&number); 12
while(i<=10) 15
{ 18
printf("%d \n",(number*i)); 21
i++; 24
} 27
return 0; 30
}
6.Write a C Program to print a multiplication table of an input number N.
#include<stdio.h>
int main()
{
int i=1,number=0;
printf("Enter a number: ");
scanf("%d",&number);
for(i=1;i<=10;i++)
{
printf("%d \n",(number*i));
}
return 0;
}
8. Write a C Program uses a nested for loop to find the prime numbers from 2 to 100.