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

Control Statements

Lecture based on Control Statement Algorithms

Uploaded by

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

Control Statements

Lecture based on Control Statement Algorithms

Uploaded by

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

01/18/2020

Control Statements

What do they do?

Allow different sets of instructions to be executed


depending on the outcome of a logical test.
Whether TRUE or FALSE.
This is called branching.
Some applications may also require that a set of
instructions be executed repeatedly, possibly
again based on some condition.
This is called looping.

Programming and Data Structure 2

1
01/18/2020

How do we specify the conditions?

Using relational operators.


Four relation operators: <, <=, >, >=
Two equality operators: ==, !=

Using logical operators / connectives.


Two logical connectives: &&, | |
Unary negation operator: !

Programming and Data Structure 3

Examples

count <= 100


(math+phys+chem)/3 >= 60

(marks>=80) && (marks<90)


(balance>5000) || (no_of_trans>25)

!((x>20) && (y<16))

Programming and Data Structure 4

2
01/18/2020

Zero
Indicates FALSE.
Non-zero
Indicates TRUE.
Typically the condition TRUE is represented by the

Programming and Data Structure 5

Branching: The if Statement

Diamond symbol (decision symbol) - indicates


decision is to be made.
Contains an expression that can be TRUE or FALSE.
Test the condition, and follow appropriate path.
Single-entry / single-exit structure.
General syntax:

If there is a single statement in the block, the braces can


be omitted.

Programming and Data Structure 6

3
01/18/2020

A decision can be
made on any
false expression.
grade >= 60
zero - false
true nonzero - true

if (grade>=60)
{
\
\
}

Programming and Data Structure 7

Example
#include <stdio.h>
main()
{
int a,b,c;
scanf ( %d %d %d , &a, &b, &c);
if ((a>=b) && (a>=c))
printf ( \n The largest number is: %d , a);
if ((b>=a) && (b>=c))
printf ( \n The largest number is: %d , b);
if ((c>=a) && (c>=b))
printf ( \n The largest number is: %d , c);
}

Programming and Data Structure 8

4
01/18/2020

Confusing Equality (==) and Assignment (=) Operators

Dangerous error
Does not ordinarily cause syntax errors.
Any expression that produces a value can be used in
control structures.
Nonzero values are true, zero values are false.
Example:
if (payCode = = 4)
printf( You get a bonus!\n );

if (payCode = 4)
printf( You get a bonus!\n );
WRONG

Programming and Data Structure 9

Some Examples
if (10<20) { a = b + c; printf ( %d , a); }

Programming and Data Structure 10

5
01/18/2020

Branching: The if-else Statement

Also a single-entry / single-exit structure.


Allows us to specify two alternate blocks of
statements, one of which is executed depending
on the outcome of the condition.
General syntax:

If a block contains a single statement, the braces can be


deleted.

Programming and Data Structure 11

false true
grade >= 60

if ( grade >= 60 )
printf ("Passed\n");
else
printf ("Failed\n");

Programming and Data Structure 12

6
01/18/2020

Nesting of if-else Structures

It is possible to nest if-else statements, one


within another.

part.
Confusion??
Rule to be remembered:

Some examples shown next.

Programming and Data Structure 13

if e1 s1
else if e2 s2

if e1 s1
else if e2 s2

?
else s3

if e1 if e2 s1
else s2
else s3

if e1 if e2 s1
else s2

Programming and Data Structure 14

7
01/18/2020

if e1 s1 if e1 s1
else if e2 s2 else if e2 s2

if e1 s1 if e1 s1
else if e2 s2 else if e2 s2
else s3 else s3

if e1 if e2 s1 if e1 if e2 s1
else s2 else s2
else s3 else s3

if e1 if e2 s1 if e1 if e2 s1
else s2 else s2
Programming and Data Structure 15

Example
#include <stdio.h>
main()
{
int a,b,c;
scanf ( %d %d %d , &a, &b, &c);
if (a>=b)
if (a>=c)
printf ( \n The largest is: %d , a);
else printf ( \n The largest is: %d , c);
else
if (b>=c)
printf ( \n The largest is: %d , b);
else printf ( \n The largest is: %d , c);
}

Programming and Data Structure 16

8
01/18/2020

Example
#include <stdio.h>
main()
{
int a,b,c;
scanf ( %d %d %d , &a, &b, &c);
if ((a>=b) && (a>=c))
printf ( \n Largest number is: %d , a);
else if (b>c)
printf ( \n Largest number is: %d , b);
else
printf ( \n Largest number is: %d , c);
}

Programming and Data Structure 17

The Conditional Operator ? :

This makes use of an expression that is either true or


false. An appropriate value is selected, depending on
the outcome of the logical expression.
Example:
interest = (balance>5000) ? balance*0.2 : balance*0.1;

Returns a value

Programming and Data Structure 18

9
01/18/2020

Examples:

x = ((a>10) && (b<5)) ? a+b : 0

(marks>=60) ? printf( Passed \n ) : printf( Failed \n );

Programming and Data Structure 19

The switch Statement

This causes a particular group of statements to be


chosen from several available groups.

switch (expression) {
case expression-
case expression-

case expression-

Programming and Data Structure 20

10
01/18/2020

Example
switch (letter)
{
case 'A':
printf ( First letter \n );
break;
case 'Z':
printf ( Last letter \n );
break;
default :
printf ( Middle letter \n );
break;
}

Programming and Data Structure 21

The break Statement

Used to exit from a switch or terminate from a


loop.
Already illustrated in the previous example.

causes a transfer of control out of the entire

Programming and Data Structure 22

11
01/18/2020

Example
switch (choice = getchar()) {

printf ( RED \n );
break;

printf ( GREEN \n );
break;

printf ( BLUE \n );
break;
default: printf ( Invalid choice \n );

Programming and Data Structure 23

Example
switch (choice = toupper(getchar())) {

printf ( RED \n );
break;
printf ( GREEN \n );
break;
printf ( BLUE \n );
break;
default: printf ( Invalid choice \n );
}

Programming and Data Structure 24

12
01/18/2020

int main () { -
int operand1, operand2; result = operand1 - operand2;
int result = 0; break;
char operation;
/* Get the input values */ result = operand1 * operand2;
printf ( Enter operand1 : ); break;
scanf( %d , &operand1) ;
printf ( Enter operation : ); if (operand2 !=0)
scanf ( \n%c , &operation); result = operand1 / operand2;
else
printf ( Enter operand 2 : );
printf ( Divide by 0 error );
scanf ( %d , &operand2);
break;
default:
switch (operation) { printf ( Invalid operation\n );
}
result = operand1 + operand2; printf ( Result: %d\n ,result);
break; }

Programming and Data Structure 25

-
entry / single-exit structure.

switch statement

Programming and Data Structure 26

13
01/18/2020

A Look Back at Arithmetic Operators: the


Increment and Decrement

Increment (++) and Decrement (--)

Both of these are unary operators; they operate on


a single operand.
The increment operator causes its operand to be
increased by 1.
Example: a++, ++count
The decrement operator causes its operand to be
decreased by 1.
Example: i--, --distance

Programming and Data Structure 28

14
01/18/2020

Operator written before the operand (++i, --i)


Called pre-increment operator.
Operator will be altered in value before it is utilized for
its intended purpose in the program.
Operator written after the operand (i++, i--)
Called post-increment operator.
Operand will be altered in value after it is utilized for its
intended purpose in the program.

Programming and Data Structure 29

Examples

Initial values :: a = 10; b = 20;

x = 50 + ++a; a = 11, x = 61

x = 50 + a++; x = 60, a = 11

x = a++ + --b; b = 19, x = 29, a = 11

x = a++ ++a; Undefined value (implementation


dependent)

Called side effects:: while calculating some values,


something else get changed.
Programming and Data Structure 30

15

You might also like