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

Refresh

The document discusses various control structures in C language that allow a program to perform different actions based on different conditions. It describes three main control structures - decision, loop, and case control structures. Decision structures like if-else statements and conditional operators allow a program to make decisions based on conditions. Loop structures like while, for, and do-while loops allow repetitive execution of code. The switch statement falls under case control structure and allows a program to choose between multiple alternatives. Examples and usage of each control structure are provided along with some best practices.

Uploaded by

ab1cde
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views

Refresh

The document discusses various control structures in C language that allow a program to perform different actions based on different conditions. It describes three main control structures - decision, loop, and case control structures. Decision structures like if-else statements and conditional operators allow a program to make decisions based on conditions. Loop structures like while, for, and do-while loops allow repetitive execution of code. The switch statement falls under case control structure and allows a program to choose between multiple alternatives. Examples and usage of each control structure are provided along with some best practices.

Uploaded by

ab1cde
Copyright
© Attribution Non-Commercial (BY-NC)
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 47

REFRESHING SESSION – 2

CONTROL STRUCTURES
• In our day to day life, we need to prepare ourselves for
any kind of situation thats going to happen so that a
proper output is obtained

• C language too must be able to perform different sets of


actions depending on the circumstances .
3 kinds of control structure helps C
language in achieving this ….
• Decision Control Structure
• Loop Control Structure
• Case Control Structure
Decision Control Structure
• Decision Control Structure can be implemented in C
using

 the if statement
 the if-else statement
 the conditional operators
The if statement
• “if “ is a keyword
• General Form :
if (this condition is true)
execute this statement ;
• The condition following the keyword if is
always enclosed within a pair of
parentheses.
if conditions using relational
operators

• The relational
operators allow us to
compare two values to
see whether they are
equal to each other,
whether one is greater
than the other.
• = is for assignment
• == is for comparison
Sample program - 1

/* Demonstration of if statement */
main( )
{
int num ;
printf ( "Enter a number less than 10 " ) ;
scanf ( "%d", &num ) ;
if ( num <= 10 )
printf ( "What an obedient servant you are !" ) ;
}
Sample program - 2
/* Calculation of total expenses */
main( )
{
int qty, dis = 0 ;
float rate, tot ;
printf ( "Enter quantity and rate " ) ;
scanf ( "%d %f", &qty, &rate) ;
if ( qty > 1000 )
dis = 10 ;
tot = ( qty * rate ) - ( qty * rate * dis / 100 ) ;
printf ( "Total expenses = Rs. %f", tot ) ;
}
Is the statement dis = 0 necessary? The answer
is yes, since in C, a variable if not specifically
initialized contains some unpredictable value
(garbage value).
if conditions using arithmetic
operators
if ( 3 + 2 % 5 ) // mod operator has the higher priority
printf ( "This works" ) ;

if ( a = 10 ) // if (a) or if (10)
printf ( "Even this works" ) ;

if ( -5 )
printf ( "Surprisingly even this works" ) ;
How ???
In C a non-zero value is considered to be true,
whereas a 0 is considered to be false.
Multiple Statements within if
main( )
{
int bonus, cy, yoj, yr_of_ser ;
printf ( "Enter current year and year of joining " ) ;
scanf ( "%d %d", &cy, &yoj ) ;
yr_of_ser = cy - yoj ;
if ( yr_of_ser > 3 )
{
bonus = 2500 ;
printf ( "Bonus = Rs. %d", bonus ) ;
}
}
The if-else Statement
Can we execute one group of statements if the
expression evaluates to true and another group
of statements if the expression evaluates to
false?
Of course! This is what is the purpose of the
else statement .
Sample program
/* Calculation of gross salary */
main( )
{
float bs, gs, da, hra ;
printf ( "Enter basic salary " ) ;
scanf ( "%f", &bs ) ;
if ( bs < 1500 )
{
hra = bs * 10 / 100 ;
da = bs * 90 / 100 ;
}
else
{
hra = 500 ;
da = bs * 98 / 100 ;
}
gs = bs + hra + da ;
printf ( "gross salary = Rs. %f", gs ) ;
}
A point worth noting...

The “else block” must immediately follow the


“if block”.
Nested if-elses
• It is perfectly all right if we write an entire if-
else construct within either the body of the if
statement or the body of an else statement.
This is called nesting of ifs.
/* A quick demo of nested if-else */
main( )
{
int i ;
printf ( "Enter either 1 or 2 " ) ;
scanf ( "%d", &i ) ;
if ( i == 1 )
printf ( "You would go to heaven !" ) ;
else
{
if ( i == 2 )
printf ( "Hell was created with you in mind" ) ;
else
printf ( "How about mother earth !" ) ;
}
}
*There is no limit on how deeply the ifs and elses can be nested.
The if statement can take any of the following forms:
(a) if ( condition )
do this ;
(b) if ( condition )
{
do this ;
and this ;
}
(c) if ( condition )
do this ;
else
do this ;
(d) if ( condition )
{
do this ;
and this ;
}
else
{
do this ;
and this ;
}
(e) if ( condition ) do this ;
else
{
if ( condition )
do this ;
else
{
do this ;
and this ;
}
}
(f) if ( condition )
{
if ( condition )
do this ;
else
{
do this ;
and this ;
}
}
else
do this ;
Use of logical operators
• Logical operators
– && - AND
– || - OR
–! - NOT
• But whereas ‘&’ and ‘|’ have a different
meaning and they are bitwise operators .
• AND ,OR – used to combine 2 or more
conditions
Problem statement
• Percentage >=60 - I class
• 50 >= percentage <= 59 - II class
• 40 >= percentage <= 49 - III class
• Percentage <40 - Fail
Reduced Indentation
if (per >=60)
printf(“I class”); if(per>=60)
else
{
printf(“I class”);
if(per>=50)
printf(“II class”);
if((per>=50)&&(per<60))
else printf(“II class”);
{
if (per >=40) if((per>=40)&&(per<50))
printf(“III class”);
else printf(“III class”);
printf(“fail”);
} if(per<40)
}
printf(“fail”);
“else if” clause
if(per>=60)
printf(“I class”);
else if(per>=50)
printf(“II class”);
else if(per>=40)
printf(“III class”);
else
printf(“fail”);
! Operator
• This operator reverses the result of the
expression it works on .
(eg.)
if( ! (y<10)) – condition is true when y>=10
Point to remember
• Do not add semicolon at the end of “if”
statement
if(i==5);
printf(“five”); is interpreted as
if(i==5)
;
printf(“five”);
Conditional or Ternary operators
• ?,:

• General form
(expression1?expression 2:expression 3)

• Y=(4>5? 3 : 4)
Loop control structure
• The versatility of the computer lies in its
ability to perform a set of instructions
repeatedly .
• 3 types
– While loop
– For loop
– Do while loop
While loop
• General form
Initialise loop counter;
while(test loop counter using a condition)
{
Do this;
And this;
Increment (or) decrement the loop counter;
}
Example
count=1;
while(count<=3)
{
printf(“Fuzzy\n”);
count=count+1;
}
Output :-

Fuzzy
Fuzzy
Fuzzy
Indefinite loop
{
int i=1;
while(i<=10)
printf(“%d\n”,i);
} // no increment / decrement counter
Guess the output
int i=1;
while(i<=32767)
{
printf(“%d\n”,i);
i=i+1;
}
Hint : – Integer range is -32768 to 32767
And yes! It is an indefinite loop
A point to note
• Post-incrementation • Pre – incrementation
int i=0; int i=0;
while(i++<10) while(++i<10)
printf(“%d”,i) printf(“%d”,i)

Output :- Output :-
1 2 3 4 5 6 7 8 9 10 123456789
for loop
• General form

for(initialise counter;test counter;increment /decrement


counter)
{
----
----
}
Set of examples
• for(i=1;i<=10;i=i+1) • int i=1;
printf(“%d\n”,i) for(;i<=10;i=i+1)
printf(“%d\n”,i);
• for(i=1;i<=10;)
{ • int i=1;
printf(“%d\n”,i); for(;i<=10;)
i=i+1; {
} printf(“%d\n”i)
i=i+1;
}
Nesting of loops
for(i=1;i<=10;i++) //outer loop
{
for(j=1;j<=10;j++) //inner loop
{
---
---
}
}
Multiple initialisations
• Initialisation and increment counters cane
be more than 1 set of statements
• There must be strictly only 1 test counter
• for(i=1,j=3 ; j<=10 ; i++,j++)
Break statement
• When “break “ is encountered inside any loop, control automatically
passes to the first statement after the loop .
i=2;
while(i<=num-1)
{
if(num%i ==0)
{
printf(“not a prime”);
break;
}
i++;
}
The continue statement
• It is used to take the control to the beginning of
the loop, bypassing the statements inside the
loop which have not yet been executed.
for (i=1;i<=2;i++)
{
if(i==1)
continue;
Printf(“%d”,i)
}
Output :- 2
The do-while loop
• General format :-
Do
{
-----
-----
-----
}while(this condition is true);
• “while” loop checks for condition before
executing any of the statements and “do while”
loop does the vice-versa ..
Odd loop
• When it is not known in beforehand how
many times the statements in the loop are
to be executed ,such are called odd loops
do
{
----
scanf(“%c”,op);
}while(op==‘y’);
Case control structure
• When we need to choose one among number of
alternatives, a switch statement is used.
• General format :-
switch(integer expression)
{
case 1:
-------
break;
case 2:
------
break;
default:
-----
}
Tips and traps
• Use of “break” statement is a must …
• (eg)
int i=2;
switch(i)
{
case 1:
printf(“I am in case 1”);
case 2:
printf(“I am in case 2”);
case 3:
printf(“I am in case 3”);
default:
printf(“I am in default”);
}
output :- I am in case 2
I am in case 3
I am in default
Cont…
• Cases can be put in any order we wish.
• “char” values can also be used ,like
case ‘a’:
case ‘D’:
• Only statements inside the case will be
executed.
The “goto” keyword
if(goals<=5)
goto sos;
else
{
printf(“common wealth games”);
exit();
}
sos:printf(“to err is human”);
Datatypes
• 3 basic datatypes are
– int (numbers without decimals)
– float (decimal numbers)
– char (character constants)
Essence of all datatypes
Questions ???

You might also like