Selection and Control-Computer Programming-Lecture Slides, Slides of Computer Programming

Dr. Mehandi Nandakumar delivered this lecture at Baddi University of Emerging Sciences and Technologies for Introduction to Computer Programming course. Its main points are: If, Statement, Selection, Decision, Block, Pseudocode, ATM, Withdrawal, Program, Nested

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekbaal
ekbaal 🇮🇳

3

(1)

30 documents

1 / 14

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
1
CS 192
Lecture 8
Fall 2011
October 12, 2003
Ghufran Ahmed
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe

Partial preview of the text

Download Selection and Control-Computer Programming-Lecture Slides and more Slides Computer Programming in PDF only on Docsity!

1

CS 192Lecture 8Fall 2011

October 12, 2003Ghufran Ahmed

2

The

if

Statement

•^

It’s a

selection

or

decision

statement

•^

if (

test-condition

)^

statement

–^

test-condition

is an expression that evaluates to

tru

e or

false

– If the condition is true, then the statement will execute;

otherwise not

-^

if

cout

“hello”

endl;

cout

“I

am

after

if”;

•^

Can have a block of statements instead of just one

•^

Pitfall:

if(marks

{cout

“Congrats!”;}

– Write

if(

marks).

Now compiler catches single

4

The

if else

Statement

-^

Example: ATM Withdrawal program

#include <iostream.h>int main(){

int balance = 10000;int withdrawal;cout << "Please enter the withdrawal amount: ";cin >> withdrawal;if(withdrawal <= balance){

cout << "Here is your money." << endl;

} else{

cout << "Sorry, insufficient balance." <<endl;

} return 0;

5

Nested

if

s

•^

An

if

statement that is the target of another

if

or an

else

•^

An

else

refers to the nearest

if

statement within the

same block that doesn’t have an

else

if(i){

if(j)

statement1;

if(k)

statement2;

this

if

else

statement3;

is

associated

with

this

else

} else

statement4;

associated

with?

7

Nested

if

s

-^

Another example #include

<iostream.h>

int

main() {

int

a,

b,

c;

cout

“Enter

three

numbers

a,

b,

c:\n”;

cin

a

b

c;

if (a==b)

if(b==c)

cout

“a,

b,

c are

equal\n”;

else

cout

“a

and

b are

different\n”;

return 0;

} • Let a=2, b=c=3. What’s printed?

8

The

if-else-if

Construction

-^

More than 2 choices.

if else

is itself a statement, so can follow an

else

if (ch == ‘A’)

agrade++;

//choice

else

if (ch == ‘B’)

//choice

bgrade++;

else

SoSo++;

//choice

-^

Easier to read format:

if (ch == ‘A’)

agrade++;

//choice

else if (ch == ‘B’)

bgrade++;

//choice

else

SoSo++;

//choice

10

The

switch

Statement

-^

If a large number of choices that all depend on one expression’svalue, better to use

switch

than the

if-else-if

construct

-^

switch (

expression

case

constant1:

statement(s); break;case

constant2:

statement(s); break;…default: statement(s); break;

// not required

-^

expression

must evaluate to an integer value (includes

characters);

constant

must be integer (or character) constant

11

The

switch

Statement

int

main() {

int

x=1; switch (x){

case 0:cout<<"zero \n";break;case 1:cout<<“one \n";break;default:cout<<“sorry! \n";break; } return

-^

char c = ‘M’; switch(c). What case will match now?

-^

case‘M’ will match too; so will case(‘L’ + 1)

-^

int x = 1, y = 2; switch(x<y). What case will match?

13

The

switch

Statement

-^

break

and

default

are optional but important.

-^

No two cases can have same value

-^

Can have same statements for multiple cases^ int

medal;

cout<<"Enter

your

rank

in

class:

cin>>medal;switch

(medal)

case 1: case 2: case 3:cout<<"Wow! Medal winner \n";break;default:cout<<"Better luck next time \n";

14

The

switch

Statement

int main(){

int i;for(i=0; i<5; i++){ switch(i){

case 0: cout << "less than 1\n";case 1: cout << "less than 2\n";case 2: cout << "less than 3\n";case 3: cout << "less than 4\n";case 4: cout << "less than 5\n";} cout << '\n'; } return 0;}

-^

Output?

-^

switch

more efficient than

if else if

when more than a few constant choices

-^

Do nested switch statements yourself