[go: up one dir, main page]

0% found this document useful (0 votes)
73 views16 pages

PPS Unit 3

The document discusses different types of control structures in C programming language. It describes three main types of control statements - decision making statements (if, if-else, nested if-else, else-if ladder, switch), loop control statements (for, while, do-while), and unconditional control statements (goto, break, continue). For each type of statement, it provides the syntax, flow, and examples to illustrate how they work.

Uploaded by

Jigar Dalwadi
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)
73 views16 pages

PPS Unit 3

The document discusses different types of control structures in C programming language. It describes three main types of control statements - decision making statements (if, if-else, nested if-else, else-if ladder, switch), loop control statements (for, while, do-while), and unconditional control statements (goto, break, continue). For each type of statement, it provides the syntax, flow, and examples to illustrate how they work.

Uploaded by

Jigar Dalwadi
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/ 16

PPS – 3110003 Prepared By: Prof.

Jigar Dalvadi

UNIT-3 - Control structure in C


• In C, the control flows from one instruction to the next instruction until now in all programs.
This control flow from one command to the next is called sequential control flow.
• In most C programs the programmer may want to skip instructions or repeat a set of instructions
repeatedly when writing logic. This can be referred to as sequential control flow.
• The declarations in C let programmers make such decisions which are called decision-making or
control declarations.

“C” SUPPORTS MAINLY THREE TYPES OF CONTROL STATEMENTS.

1. Decision making statements


a) Simple if Statement
b) if – else Statement
c) Nested if-else statement
d) else – if Ladder
e) switch statement

2. Loop control statements


a) for Loop
b) while Loop
c) do-while Loop

3. Unconditional control statements


a) goto Statement
b) break Statement
c) continue Statement

if statement
• This is the most simple form of decision control statement. In this form, a set of statements are
executed only if the condition given with if evaluates to true.

• Its general syntax and flow chart is as under:-

if(condition)

Statements ;

Statement x;

1|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Example:
# include<stdio.h>
# include<conio.h>
void main( )
{
int m,n;
clrscr( );
printf(“\n Enter two numbers:”);
scanf(“%d %d”, &m, &n);
if((m-n)==0)
printf(“\n two numbers are equal”);
getch();
}

if-else Statement
• It is observed that the “if” statement executes only when the condition following if is true. It does
nothing when the condition is false.

• In if-else either True-Block or False – Block will be executed and not both. The “else” Statement
cannot be used without “if”.

Synatx:

if ( Test Expression or Condition )


{
Statements; /*true block (or) if block */
}
else
{
Statements; /* false block (or) else block */
}
Statement x;

2|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Example:
# include<stdio.h>
# include<conio.h>
void main( )
{
int n;
clrscr( );
printf(“Enter a number :”);
scanf(“%d”, &n);
if( (n%2)==0 )
printf(“\n The given number is EVEN ”);
else
printf(“\n The given number is ODD ”);
getch( );
}

Nested “if–else” Statement


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

Syntax

if ( Test Condition1)
{
if ( Test Condition2)
{
Statement -1;
}
else
{
Statement -2;
3|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

}
}
else
{
if ( Test Condition3)
{
Statement -3;
}
else
{
Statement-4;
}
}

• If Test Condition-1 is true then enter into outer if block, and it checks Test Condition2,if it is
true then Statement-1 executed if it is false then else block executed i.eStatement-2.

• If Test Condition -1 is false then it skips the outer if block and it goes to else block and Test
Condition-3 checks if it is true then Statement-3 executed, else Statement-4 executed.

Example:
# include<stdio.h>
# include<conio.h>
void main( )
{
float a,b,c;
printf(“Enter Three Values:”);
scanf(“%f%f%f”, &a, &b, &c);
printf(“\n Largest Value is:”) ;
if(a>b)
{
4|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

if(a>c)
printf(“ %f ”, a);
else
printf(“ %f ”, c);
}
else
{
if (b>c)
printf(“ %f ”, b);
else
printf(“ %f ”, c);
}
getch();
}

The “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.


Syntax

if (Test Condition -1)


{
Statement -1;
}
else if ( Test Condition -2)
{
Statement -2;
}
else if ( Test Condition -3)
{
Statement -3;
}
:
:
:
else if ( Test Condition –n)
{
Statement –n;
}
else
{
default statement;
5|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

}
Rest of the Program Statements-X;

• The above construction is known as else if ladders.

• The conditions are evaluated from top to bottom.

• As soon as a true condition is found, the statement associated with it is executed and

• The control is transferred to the Rest of the Program Statement–X (skipping rest of the ladder).

• When all the “n” conditions become false, then the final else containing the default statement
will be executed.

Example:
# include<stdio.h>
# include<conio.h>
void main( )
{
int a, b, c
clrscr ( ) ;
printf(“Enter 1st number:”);
scanf(“%d”, &a);
printf(“Enter 2nd number:”);
6|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

scanf(“%d”, &b);
printf(“Enter 3rd number:”);
scanf(“%d”, &c);
if ((a>b) && (a>c))
printf(“Highest Number is: %d”, a);
else if ((b>a) && (b>c))
printf(“Highest Number is: %d”, b);
else
printf(“Highest Numbers is: %d”, c);
getch( );
}

switch-case Statement
• 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.

• The switch statement causes a particular group of statements to be chosen from several available
groups.

• The selection is based upon the current value of an expression which is included with in the
switch statement.

• The switch statement is a multi-way branch statement.

• In a program if there is a possibility to make a choice from a number of options, this structured
selected is useful.

• The switch statement requires only one argument of int or char data type, which is checked with
number of case options.

• The switch statement evaluates expression and then looks for its value among the case constants.

• If the value matches with case constant, then that particular case statement is executed.

• If no one case constant not matched then default is executed.

• Here switch, case and default are reserved words or keywords.

• Every case statement terminates with colon “:”.

• In switch each case block should end with break statement, i.e.
Syntax
switch (expression)
{
case value1:
code segment1;
7|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

break;
case value2:
code segment2;
break;
.
.
.
case valueN:
code segmentN;
break;
default:
default code segment;
}

• The value of this expression is either generated during program execution or read in as user
input. The case whose value is the same as that of the expression is selected and executed.
• The optional default label is used to specify the code segment to be executed when the value of
the expression does not match with any of the case values.

• The break statement is present at the end of every case. If it were not so, the execution would
continue on into the code segment of the next case without even checking the case value.

8|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Example
#include<stdio.h>
int main( )
{
int day;
printf("nEnter the number of the day:");
scanf("%d",&day);
switch(day)
{
case 1:
printf("Sunday");
break;
case 2:
printf("Monday");
break;
case 3:
printf("Tuesday");
break;
case 4:
printf("Wednesday");
break;
case 5:
printf("Thursday");
break;
case 6:
printf("Friday");
break;
case 7:
printf("Saturday");
break;
default:
printf("Invalid choice");
}
getch( );
}

for Loop
• The for loop works well where the number of iterations of the loop is known before the loop is
entered. The head of the loop consists of three parts separated by semicolons.
• The first is run before the loop is entered. This is usually the initialization of the loop variable.
• The second is a test, the loop is exits when this returns false.
• The third is a statement to be run every time the loop body is completed. This is usually an
increment of the loop counter.
• C comes with an extensive set of libraries with several built-in functions that make the
life of a programmer easy. Even a beginner can easily code using these built-in functions.

9|Page
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

for(initialization statement; condition)


{
//statements inside the loop
}

Example
#include<stdio.h>
#include<conio.h>
void main( )
{
int i;
clrscr( ) ;
for(i = 1; i <=5; i++)
printf(“\n Number: %d it’s Square: %d”, i, i*i);
getch( );
}

Nested “for” loop


• We can also use loop within loops. i.e. one for statement within another for statement is
allowed in C.
• In nested for loops one or more for statements are included in the body of the loop. *
ANSI C allows up to 15 levels of nesting. Some compilers permit even more.

Syntax :
for( initialize ; test condition ; updation) /* outer loop */
{
for(initialize ; test condition ; updation) /* inner loop */
10 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

{
Body of loop;
}
}

Example:

# include<stdio.h>
# include<conio.h>
void main ( )
{
int x, i, j ;
printf(“How many lines stars (*) should be print f? :”);
scanf(“%d”, &x);
for(i=1; i<=x; i++)
{
for (j=1; j < =i; j++)
{
printf( “*”);
}
printf( “ \n”);
}
getch( );
}

while loop
• The while is an entry-controlled loop statement.

• The test condition is evaluated and if the condition is true, then the body of the loop is
executed.

• The execution process is repeated until the test condition becomes false and the control is
transferred out of the loop.

• On exit, the program continues with the statement immediately after the body of the loop.

• The body of the loop may have one or more statements.

• The braces are needed only if the body contains two or more statements.

• It’s a good practice to use braces even if the body has only one statement.

• The simplest of all the looping structures in C is.


11 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

Syntax :

Initialization Expression;
while ( Test Condition)
{
Body of the loop
Updation Expression
}

Example:
# include<stdio.h>
# include<conio.h>
void main( )
{
int a=1, Sum=0;
while(a<=10)
{
Sum = Sum + a;
a++;
}
printf(“Sum of 1 to 10 numbers is: %d”, sum);
getch( );
}

do-while loop
• To execute a part of program or code several times, we can use do-while loop of C
language. The code given between the do and while block will be executed until condition
is true.

12 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

• The do-while is an exit-controlled loop statement. Because, in do-while, the condition is


checked at the end of the loop.

• The do-while loop will execute at least one time even if the condition is false initially.

• The do-while loop executes until the condition becomes false.

Syntax :
Initialization Expression;
do
{
Body of the loop
Updation Expression;
} while ( Test Condition);

Example
# include<stdio.h>
# include<conio.h>
void main( )
{
int n, x=2;
clrscr( );
printf( “Enter the number for testing (prime or not”);
scanf(“%d”, &n);
do
{
if(n%x == 0)
{
printf(“ \n the number %d is not prime”, n);
13 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

exit(0);
}
x++;
} while ( x < n);
printf(“ \n the number %d is prime”, n);
getch( ) ;
}

Break Statement
• A break statement terminates the execution of the loop and the control is transferred to the
statement immediately following the loop. i.e., the break statement is used to terminate
loops or to exit from a switch.
• It can be used within for, while, do-while, or switch statement.
• The break statement is written simply as break;
• In case of inner loops, it terminates the control of inner loop only.

Example:
#include <stdio.h>
#include <conio.h>
void main()
{
int i=1;//initializing a local variable
clrscr();
for(i=1;i<=10;i++)
{
printf("%d ",i);
if(i==5) //if value of i is equal to 5, it will break the loop
{
break;
}
}//end of for loop

14 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

getch();
}
Continue Statement
• The continue statement is used to bypass the remainder of the current pass through a loop.
• The loop does not terminate when a continue statement is encountered. Instead, the remaining loop
statements are skipped and the computation proceeds directly to the next pass through the loop.
• The continue statement can be included within a while, do-while, for statement.
• It is simply written as “continue”.
• The continue statement tells the compiler “Skip the following Statements and continue with the
next Iteration”.
• In “while” and “do” loops continue causes the control to go directly to the test –condition and then
to continue the iteration process.
• In the case of “for” loop, the updation section of the loop is executed before test-condition, is
evaluated.
Syntax :

Jump-statement;
Continue;
The jump statement can be while, do while and for loop.
Flowchart :

Example:
# include<stdio.h>
void main( )
{
int i=1, num, sum =0;
for(i=0; i < 5; i ++)
{
printf(“ Enter an integer:”);
scanf( “%d”, &num);
15 | P a g e
PPS – 3110003 Prepared By: Prof. Jigar Dalvadi

if(num < 0)
{
printf(“\nyou have entered a negative number”);
continue ; /* skip the remaining part of loop */
}
sum += num;
}
printf(“The sum of the Positive Integers Entered = % d \ n”, sum);
getch( );
}

16 | P a g e

You might also like