0% found this document useful (0 votes)
10 views45 pages

Amit'

The document provides an overview of control statements in C programming, including selection statements like if, if-else, and switch statements, as well as iteration statements such as while, do-while, and for loops. It explains the syntax and usage of these statements with examples for better understanding. Additionally, it highlights the properties and limitations of each control structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
10 views45 pages

Amit'

The document provides an overview of control statements in C programming, including selection statements like if, if-else, and switch statements, as well as iteration statements such as while, do-while, and for loops. It explains the syntax and usage of these statements with examples for better understanding. Additionally, it highlights the properties and limitations of each control structure.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 45

Statemen

ts
(Decision
Making)
Control
Statements
Control statements in C are used to write powerful
programs by;

1. Repeating important sections of the program.


2. Selecting between optional sections of a program.
Control statements

The break
statement
Presented By Group 1

Dibya Jyoti Nayak 240301120455


Abhishek Behera 240301120439
Subham Mallick 240301120432
Sidhartha Mohanty 240301120431
Amit Kumar 240301120470
SELECTION STATEMENT

Types of Selection Statement


1. Simple if Selection statement
2. if else Selection statement
3. Nested if else Selection
statement
4. else if ladder Selection
statement
Simple if Selection
statement
It is used to control the flow of execution of the
statements and also to test logically whether the
condition is true or false.
Syntax:
if ( condtion )

statement ;

if the condition is true then the statement following the “if


“is executed if it is false then the statement is skipped.
Seiection

Statement
Properties of an if statement

a) if the condition is true then the simple or


compound statements are executed.
b) If the condition is false it will skip the
statement.
c) The condition is given in parenthesis
and must be evaluated as true or false.
d) Ifa compound structure is provided, it must be
enclosed in opening and closing braces
//Biggest of Two
Numbers #incIude
<stdio.h>
void main()

int a,
b;
clrscr(
);

printf(“Enter the A and B


Value:\n"); scanf("%d”, &a);

if (a > b)

printf(“A is Big");
getch(
);
The if else statement
It is used to execute some statements when the condition is true and
execute some other statements when the condition is false depending on
the logical test.
Syntax:
if ( condition )
statement (if the condition is true this statement will
) 1; be executed)
els
e
statement (if the condition is false this statement will
2; be executed)

Fals
e
// Biggest of Two // Given Number is ODD or EVEN
Numbers Number
#incIude #incIude <stdio.h>
<stdio.h> void
void (main()
(main() int n;
int a, clrscr(
b; );
clrscr( printf(“Enter the
);
printf(“Enter the A and B Number:\n”); scanf(“%d”,
Value:\n"); scanf("%d”, &a); &n);
if (n % 2
if (a > == 0)
b) printf(“Given Number is
printf(“A is Even Number");
Big");
els else
e
printf(“B is printf(“Given Number is Odd
Big”); Number”);

getch( getch(
); );
// Biggest of Three
Nested if..... else Numbers
#incIude<stdio.
statement h»
when a series of if...else statements are
v o i d ma i n ()
occurred in a program, we can write an
entire if...else statement in another if...else :. t a , b, c:
statement called nesting ‹lrscr();
Syntax: printf(“Enter the Three
Plumbers:\n”); scanf(“°/«d%d
%d”,&a,&b,&c);
if ( condition 1)
if (a >
b)
if ( condition 2) if (a >
statement 1 ; c) printf(“A is
else else Big");
printf(“C is
statement 2 ; Big”);
else
else
if (b >
if (condition 3) c) printf("B is
else Big”);
statement 3;
printf(“C is
else Big”);
statement 4; getch(
);
FALSE

TRUE

FALSE TRUE
else if Ladder or Multiple if else
Statements
then a series of decisions are involved we have to use more than
one if — else statement called as multiple if's. Multiple if — else statements
are much faster than a series of if — else statements, since theif structure is
exited when any one of the condition is satisfied.
/*This program reads in a simple expression with a very
restricted format and nts out its value. */
main
()
int
n1,n2;
els
int val; e
char
printf(“?? operator o
c\
op; n",op);
printf(”Enter a simple exit(1);
expression "); scanf(“%d%c
%d",&n1,&op,&n2); if(op == printf("%d0/oc%d %d\
'+') n”,n1,op,n2);
val n1
+ n2; elseval n1 /
n2; else '*')
if(op == '-')
val = if(op n1 *
Sample Program #include<stdi
• Write a program to calculate o.h>
the sales commission for the
data
Sales given
value below: Commission(
(Rs) #incIude<conio
Less than 1000 f)
No
o
.h> Void
Above 1000 but below commission
2000 5% of sales
main()
Above 2000 but below 8% of sales t
5000 10% of sales float sales,
Above 5000
com;
printf(’Enter the sales
value :”); Scanf(%f,
&saIes); if(saIes<=1000)
com = 0;
else if(saIes>1000 &&
sales <=2000)
com = sales*5/100;
else if(saIes>2000
&& sales
<=5000)
THE SWITCH
STATEMENT
• The control statements which allow us to make a decision
from the number of choices is called switch (or) Switch-
case statement.
• It is a multi way decision statement, it test the given
variable (or) expression against a list of case value.
switch (expression) switch (expression)

case constant 1: case constant 1:


simple statement simple statement
(or) compound (or) compound
statement; statement;
case constant 2: case constant 2:
simple statement simple statement
(or) compound (or) compound
statement; statement;
case constant 3: default
simple statement simple statement
(or) compound (or) compound
statement; statement;
Example Without Break Example With Break
Statement
#include<stdio.h>
void main ()
Statement
#include<stdio.h>
int num1,num2,choice; voidint
main ()
num1,num2,choice;
printf(“Enter the Two Numbers:\ printf("Enter the Two Numbers:\
n"); scanf(“°/+de n”); scanf(“%d
d”,&num1,&num2); printf(“1 - %d”,&num1,&num2); printf("1 -
> Addition\n"”); > Addition\n””);
printf(“2->Subtraction\ printf(“z->subtraction\n”);
n”); printf("3-•MultipIication\
printf(“3-›MuItipIication\ n”); printf(“4->Division\
n”); printf(”4->Division\ n”); printf("Enter your
n”); printf(“Enter your Choice:\n”);
Choice:\n”); switch(choic
scanf("%d”,&choice);
scanf(“°/+d”,&choice); e)

printf(“Sum is °Ad\n”,
switcht hoice)
num1+num2); break;
case 2:
case 1: printf("Diif. is %d\n”, num1-
Printf("Sum is num2);
%d\n”, break;
num1+num2); case 3:
printf(“Product is %d\n”,
case 2: num1“num2);
Printf("Diif. is break;
°/ d\n“, num1-
num2); printf("Division is %d\n",
num1lnum2); break;
case 3: default:
Printf("Produ printf (“Invalid Choica.....\n”);
ct is °/+d\n”, getch(
num1“num2),
);
case 4:
Printf("Divisio
Rules for
Switch
The expression in the switch statement must be an integer or
character constant. No real numbers are used in an expression.
The default is optional and can be placed anywhere, but usually
placed at end.
The case keyword must be terminated with colon (:);
No two case constant are identical.
The values of switch expression is compared with case constant in the
order specified i.e from top to bottom.
The compound statements are no need to enclose within pair of braces.
integer Expression used in different case statements can be specified in
any order.
A switch may occur within another switch, but it is rarely done. Such
statements are called as nested switch statements.
The switch statement is very useful while writing menu driven programs.

Limitations of using a switch statement


Only One variable can be tested with the available case statements with the values
stored in them (i.e., you cannot use relational operators and combine two or more
conditions as in the case of if or if — else statements).
Floating - point, double, and long type variabIec cannot be used as cases in the switch
Iteration
1.Iteration statements is alsoStatements
known as Looping statement.
2.A segment of the program that is executed repeatedly is called as a
loop.
3. Some portion of the program has to be specified several number of
times or until a
particular condition is satisfied.
4.Such repetitive operation is done through a loop structure.
5.The Three methods by which you can repeat a part of a program are,

1. while Loops 2. do....while loops


3. for Loop

LoOps generally consist of two parts


Contro/ express/ons: One or more confro/ expressi0RS which control the
execution of the loop,
Body : which is the statement or
set of statements which is executed
over and over

Any looping statement , would include the following


steps:
While
Loop
A while loop has one control expression, and
executes as long as that expression is true. The
general syntax of a loop
initialize whilecounter;
loop is
while (condition)

statement (s);
increment or decrement loop counter

Awhile loop is an entry controlled loop


statement.
l“alse

True
Example:
// Summation of the series I + 2 + 3 + 4
II Print the 1
+ #include <stdio.h>
Values #include
void main()
<stdio.h> void
main()
int i, sum;
int i;
clrscr();
clrscr();
i = 1;
i = ();
sum = 0;
while(i<=
10) while(i<=
IO)
printf(“The I Value is :%d\
n”,i); sun = sum + i

printf(“The Sum Valuc is:%d\n”,i);

getch();

getch();

Example: //Summation of the series 1' + 2"+ 3' +
//Summation of the series 1 2 + 2* + 3* + #include <stdio.h>
#inc1ude <stdio.h> #include<math.h>
#inc1ude<math.h> void main()
void main()

1
int i,
clrscr();
sum;
1 — 1;
clrscr();
sum = 0;
l;
while(i<=10)
sum = 0;
while(i<=l
0) sum =
sum +
pow(i,i)

sum = sum + i*i; //or I ^2 or pow(i, 2) printf(“The


Sum
printf(“The Sum Value is:'%d\ii”,i); Value is:
getch();
%d\n”,i);
++I;
Wap to print the summation of digits of any given
number.
#incIude<stdio
.h> void
main()

int number-
0, rem-0,
sum-0;

clrscr();

printf(“Enter
the value
for
number");
scanf("0/
od”,&n);

while(numb
er > 0)

rem - numbef %
10; sum - sum +
rem; number -
THE do-while
LOOP
• The body of the loop may not be executed if the condition is not
satisfied in while
loop.
• Since the test is done at the end of the loop, the statements
in the braces will always be executed at least once.
• The statements in the braces are executed repeatedly as long
as the expression
initialize loop
in the parentheses counter;
is true.
do

statement (s);
increment or decrement loop counter

while (condition);

Make a note that do while ends in a ; (semicolon)


Note that Do... While Looping statement is Exit Controlled Looping
statement
True

False
Difference Between While Loop and Do — While
Loop

SI.No. while loop do-while loop

1. The while loop tests the condition before The do — while loop tests the condition
each iteration. after the first iteration.

2. If the condition fails initially the Even if the condition fails initially the
loop is Skipped entirely even in the loop is executed once.
first iteration.
Example:
II Print the 1 ff Print the I Values
Values #include #include <stdio.h>
<stdio.h> void void main()
main()

1 int i;
int i; clrscr();
clrscr(); i = 11 ;
i = (); do
dti I
p
r
pri i
ntf n
(“ t
while(i<=
Th while(i<=10);
f
eI
10); getch(); (
getch();
Val “
ue T
'
way to print the Fibonacci series for any given number Usiiig Do....While Loop
#incIude
<stdio.h> void
main()

int i1 f1,f21f3;
clrscr(
); f1

0;
f2 =
1;
printf(
“The
Fibona
cci
Series
is:\n")
printf(
“%d\
n”,f1)
while(i <-
;
printf(“%d\
10);
n”,f2); do
for
The for loop is another Loop
repetitive control structure, and
is used to execute set of instruction repeatedly until the
condition becomes false.
To set up an initial condition and then modify some value
to perform each succeeding loop as long as some condition
is true.

The syntax of a for loop is

Body of the loop;

The three expressions :


expr1 - sets up the initial condition,
expr2 - tests whether another trip through the loop
should be taken, expr3 - increments or updates
Body of Loop

Stop
Exampl EXampl
e e
Given example will print Given example of Multiplication Table
the values from 1 to 10. #inc1ude<stdio.h>
#include<conio
.h> void
#incIude<stdio main()
.h> void int IvIUl,li
rrIit,C,i;
main()
clrscr();
for (int i = 1; i <= printf("Enter the Multiplication
10; i++) printf("i is Number:\n");
O
ld\n", i); scanf("%d",&mul);
printf("Enter the
Limits:\n");
There is no need of ( ) for(i-1;i<-
scanf(" O
ld",&Iimit);
Braces for single line (Iimit;i++)
c = i * mul;
statement and for Multiple
printf( %d * %d: Old\
line it is essential else it n",i,mul,c);
will consider only next line
getch(
of for statement. );
Additional Features of for Loop
Case 1:
The statement
p - 1,
for (n - 0; n < 17; ++ n)
can be rewritten as
for (p — 1, n = 0; n < 17;++n)

Case 2:
The second feature is that the test — condition may have
any compound relation and
the testing need not be limited only to the loop control
variable.
su 0;
m 1; i < 20 && sum < 100;
for ++ i)
(i SUrT1 = SUITI -I- i ;
printf("%d %d\n", i,
sum);
Additional Features of for Loop
Conti...
Case 3:
It also permissible to use expressions in the assignment statements of
initialization
and increments sections.
For Example:
for (x = (m + n) / 2; x > 0; x = x / 2)
Case 4:
Another unique aspect of for loop is that one or more sections can be
omitted, if
necessary.
For Example:
m = 5;
for ( ; m ! = 100 ;)

printf(“%d\
n”,m); m = m
+ 5;

Both the initialization and increment sections are omitted in the for
statement. The initialization has been done before the for statement and the
control variable is incremented inside the loop. In such cases, the sections
are left ‘blank’. However, the semicolons separating the sections must remain.
If the test — condition is not present, the for statement sets up an ‘infinite’ loop.
Such loops can be broken using break or goto statements in the loop.
Additional Features of for Loop
Conti...
Case 5:
We can set up time delay loops using the null statement as
follows:
for ( j - 1000; j > 0; j - j — 1)
1.This is loop is executed 1000 times without producing any
output; it simply causes a
time delay.
2.Notice that the body of the loop contains only a semicolon,
known as a null statement.

Case 6:
for ( j - 1000; j > 0; j 1)

This implies that the C compiler will not give an error message if
we place a semicolon by mistake at the end of a for statement.
The semicolon will be considered as a null statement and the program
may produce some nonsense.
Nesting of for
Loop
The One for statement within another for statement is called Nesting for
Loop.

Syntax:

for Loop
Example Exampl
// Print the I and J IIeUultiplication
Value Table
#incIude<stdio #incIude<stdio
.h»
.h> #incIude<conio
#incIude<coni .h> void
o.h> void main()
main()
int sum =
int I, 1,a,b;
clrscr();
j;
clrscr( for
); (a=1;a<=5;
a++)
for (i
= 1; printf
I< ("the
multiplica
= 10 tion table
;I+ for %d\
+) getch( n",a);
);
p for
r (b=1;
b<=1
Exercis
e print out Weir average.
1) Write a program that will read in N numbers and

2) Wap to print the following series for N given number


1+(1+2)+(1+2+3)+(1+2+3+4).........
3) Wap to print the following series for N given number

4) Wap to print the following series for N


given number
1
111
11111
1111111
JUMPS IN LOOPS
1. Loops perl'orin a set of operations repeaiedly until lhe control 'enable lails to satisly the tesi

condition.
2. Thc number ot’ iimcs a loup is repeated is decided in advance and the test condition is written to
achieve this.

Sometimes, when executing a loop it becomes desirable to skip a part of the loop or to leave the
loop as soon as a certain condition occurs.
4. Jumps out of a Loop is Classificd into three typcs
t. break; 2. continue:

The break Statement


1. A break statement is used to terminate of to exit a for, switch, while or
do — while statements and the execution continues following the break
statement.
2.
The gcncral turrn of the break statement is
break;
3. The break statement does not have any embedded expression or’ arguments.
4. The break statement is usually used at the end of each case and before the start of the
next case statement.

5. The break statement causes the control to transfer out of the entire switch statement.
#incIude<stdio.h>
#include <stdio.h> void main ()
void main() int num1,num2,choice;
printf (“Enter the Two Plumbers:\
n"); scanf(“%d
int i; %d”,&num1,&num2); printf("1 -
> Addition\n"");
clrscr(); printf("2->Subtraction\
n"); printf("3-
19 >Multiplication\n”);
printf(“4->Division\n");
while (i < = If)) printf (“Enter your
( Choice:\n”);
switch (choice)scanf (“%d™,
&choice);
printf (“The 1 Value is: %d \n”, i): case 1:
printf ("Sum is %d \n”,
'if' (i = = 6)
num1+num2); break;
case 2:
printf (“Diif. is %d \n", num1-
printf(“The I value is Reached 6, num2);
break
So break of the programs\n’ ); ; case 3:
p
brcak;
rintf
(“Pro
duct
++ i is
%d \
n",
num1
getch() ”num
; 2);
The continue
Statement
• The continue statement is used to transfer the control to the beginning of the loop,
there by terminating the current iteration of the loop and starting again from the
next iteration tif the same loop.
• The continue statement can be used within a while or a do — while or a for loop.
• The general form or the syntax of the continue S(ittement is

continue;
• The continue statement does not have any expressions or arguments.
• Unlike break, the loop does not tei'minate when a continue statement is encountered,
but it terminates the current iteration of the loop by skipping the remaining part of the
loop and resumes the control not the start of the ltiop for the next iteration.
#include <stdio.h>
void main()

int i;

clrscr();
l
while (i <
= 10)

printf(“The 1 Value is: %d \n”,


i); if (i — — 6)

printf(“The I value is Reached 6, But Continue this Programs\


n”); continue;
Differences Between Break and Continue
Statement
SI.No. break continue

1. Used to terminate the loops or to Used to transfer the control to the


exit loop from a switch. start of loop.

2. The break Continue statement when executed


statement when causes Immediate termination
executed causes of the current
immediate termination of loop iteratio
containing it. n of the
loop.
The goto
Statement
• The goto statement is used to transfer the control in a loop or a function from one point to
any other portion in that program.
• If misused the gotta statement can make a program impossible tti understand.
• The General form ter the syntax of goto statement is

goto label;
Statement (s);

label:
statement (s);

• The goto statement is classified into two


types
a. Unconditional goto
b. Conditional goto
Unconditional Goto Conditional Goto
The Unctinditional gtito means the The Conditional gtito means the ctintrtil
control transfer front one block to transfer mom one block to another block
another block without checking the with checking thc test cunJition.
test condition. #include <stdio.h>
void main()
Example:
#include <stdio.h>
void main() printf (“Enter the Two Value:\n”);
scanf (“0/»d”, &a, &b);

clrscr(); else
goto output_2;
Start: ouiput_ 1:
printf(“A is Biggest Number”);
printf(“ g•oto Stop;
We1com output_2:
e\n”); Frintf(“B is Biggest Number”);

goto Start;
Stop:
getch(); getch();

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy