0% found this document useful (0 votes)
3 views

Unit 3 SPC . (4)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

Unit 3 SPC . (4)

Copyright
© © All Rights Reserved
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 51

Unit III

Control Structure

Prof. Manohar B. Patil


Assistant Professor
Department of Computer Engineering

R.C. Patel Institute of Technology, Shirpur


(An Autonomous Institute)
Contents:

1. Decision Making with Branching 3. Nested Looping


• if Statement • Nested for Loop
• Simple if Statement • Nested while Loop
• if-else Statement • Nested do-while Loop
• Nested if Statement
• if-else-if Statement
• switch Statement
4. Control Statement
2. Looping • break Statement
• while Statement • continue Statement
• do-while Statement • goto Statement
• For Statement
1. Decision Making with Branching

• Decision making is about deciding the order of execution of statements based on certain

conditions or repeat a group of statements until certain specified condition satisfy.

• 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.

• In C programming language, if statement is classified into FOUR types as follows,

1. Simple if Statement

2. if-else Statement

3. Nested 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

• Writing a if statement inside another if statement is called 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

only one option that is to be executed.

• In the switch statement, every option is defined as a case.

• we use the ‘‘break’’ statement at the end of each case.

• That means the break statement is used to terminate the switch statement .
switch statement

switch (variable or an integer expression)


{
case constant:
//C Statements
break;

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.

• A loop executes a block of commands a specified number of times until a


condition is reached.

• C language provides THREE looping statements,

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.

• The while statement is also known as Entry Control Looping statement


Example Program: To display even numbers up to 10

# 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 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 Exit Control Looping


statement.
Example Program: To display even numbers up to 10
# include <stdio.h>
# include <conio.h>
void main ()
{
int n = 0;
printf (" Even numbers up to10\ n"); OUTPUT:
Even numbers up to 10
do
0 2 4 6 8 10
{
if(n % 2 == 0)
printf ("%d\t", n);
n++ ;
} while ( n <= 10 );
getch ();
}
Difference between while and do-while loop
3. for 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

• Nested loop means a loop statement inside another loop statement.

• There is no restriction for defining any number of loops.

• The nesting level can be defined at n times.

• You can define any type of loop inside another loop; for example, you can define while

loop inside a for loop.

• So it is also called as “loop inside loop”.


Nested for Loop
• Syntax: Nested for Loop
for ( initialization ; condition ;increment
/ decrement )
{
for ( initialization ;condition ; increment
/decrement )
{
// inner loop statement ;
}
// outer loop statement ;
}
Example Program: Nested for Loop
#include <stdio.h>
int main ()
{
int n,i,j;
printf ("Enter the value of n:");
scanf("%d",&n);
// outer loop
for(i=1;i<=n;i++) // outer loop
Output :-
{
Enter the value of n:3
for(j=1;j<=n;j++) // inner loop 1 2 3
{ 2 4 6
printf ("%d\t" ,(i*j)); 3 6 9
}
printf ("\n");
}
getch();
return 0;
}
Nested while Loop
Syntax: Nested while Loop
while ( condition1 )
{
statement (s);
while ( condition2 )
{
statement (s);
}
statement (s);
}
Example Program: Nested while Loop
# include <stdio .h>
int main ()
{
int i=1,j; OUTPUT:
while (i <= 5) 1
{ 12
j=1; 123
while (j <= i ) 12 34
{ 12 345
printf ("%d ",j);
j++;
}
printf ("n");
i++;
}
return 0;
}
Nested do-while Loop
Syntax: Nested do-while Loop
do
{
statement (s);
do
{
statement (s);
} while ( condition1 );
statement (s);
} while ( condition2 );
Example Program: Nested do-while Loop
#include<stdio.h>
int main()
{
int i=1,j; OUTPUT
do *
{ **
j=1; ***
do ****
{ *****
printf ("*");
j++;
} while (j <= i);
i++;
printf ("\n");
} while (i <= 5);
getch();
return 0;
}
4. Unconditional Control Statement

• In C, there are control statements that do not need any condition to control the
program execution flow.

• These control statements are called as Unconditional Control Statements.

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.

• It is also referred to as an unconditional jump statement.

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.

2. Write a C Program to print Absolute value.

3. Write a C Program to check whether a person is eligible to vote or not.

4. Write a C program to print weekday based on given number using switch.

5. Write a C Program to print table for the given number using while loop in C.

6. Write a C Program to print a multiplication table of an input number N


using Do- while loop.

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> OUTPUT:


int main() Enter a value : 5
{
int i = 1, N; Multiplication Table of 5 :
5*1=5
5 * 2 = 10
printf("Enter a value : "); // input N 5 * 3 = 15
scanf("%d", &N); 5 * 4 = 20
printf("Multiplication Table of %d : \n", N); 5 * 5 = 25
do 5 * 6 = 30
5 * 7 = 35
{ // loop body 5 * 8 = 40
printf("%d * %d = %d\n", N, i, N * i); 5 * 9 = 45
i++; 5 * 10 = 50
}
while (i <= 10);
return 0;
}
7. Write a C Program to Print table for the given number.

#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.

#include <stdio.h> OUTPUT:


int main () 2 is prime
{ /* local variable definition */ 3 is prime
int i, j; 5 is prime
for(i = 2; i<100; i++) 7 is prime
{ .
for(j = 2; j <= (i/j); j++) .
if(!(i%j)) break; // if factor found, not prime .
if(j > (i/j)) printf("%d is prime\n", i); .
} 89 is prime
return 0; 97 is prime
}

You might also like