Control Structures If Else, Nested If, Switch-Computer Architecture and Programming-Lecture Slides, Slides of Computer Architecture and Organization

Chanchal Mahanthapa delivered this lecture at Chandra Shekhar Azad University of Agriculture and Technology for Programming and Computer Architecture. It includes: C , Programming, Control, Structures, Nested, Sequence, Selection, Repetition, Flowchart

Typology: Slides

2011/2012

Uploaded on 07/13/2012

ekraj
ekraj 🇮🇳

4.1

(11)

123 documents

1 / 20

Toggle sidebar

This page cannot be seen from the preview

Don't miss anything!

bg1
C++ Programming
Control structures. If, if-else, nested if, switch
docsity.com
pf3
pf4
pf5
pf8
pf9
pfa
pfd
pfe
pff
pf12
pf13
pf14

Partial preview of the text

Download Control Structures If Else, Nested If, Switch-Computer Architecture and Programming-Lecture Slides and more Slides Computer Architecture and Organization in PDF only on Docsity!

C++ ProgrammingControl structures. If, if-else, nested if, switch

Control Structures

3 control structures^ ◦

Sequence structure^ 

Programs executed sequentially by default

Selection structures^ 

if

,^

if/else

,^

switch

Repetition structures^ 

while

,^

do/while

,^

for

Control Structures

Flowchart^ ◦

Graphical representation of an algorithm

Special-purpose symbols connected by arrows(flowlines)

Rectangle symbol (action symbol)^ 

Any type of action

Oval symbol^ 

Beginning or end of a program, or a section of code(circles)

Single-entry/single-exit control structures^ ◦

Connect exit point of one to entry point of thenext

if Selection Structure

Selection structure^ ◦

Choose among alternative courses of action

Pseudocode example:

If student’s grade is greater than or equal to 60

Print “Passed”

If the condition is

true

Print statement executed, program continues to nextstatement

If the condition is

false

Print statement ignored, program continues

Indenting makes programs easier to read^ 

C++ ignores whitespace characters (tabs, spaces, etc.)

if Selection Structure

Flowchart of pseudocode statement

true

false

grade >= 60

print “Passed”

A decision can be made onany expression.zero -

false

nonzero -

true

Example: 3 - 4

is

true

if/else

Selection Structure

if^ ◦

Performs action if condition true

if/else^ ◦

Different actions if conditions true or false

Pseudocode

if student’s grade is greater than or equal to 60

print “Passed”

else

print “Failed”

C++ code

if

grade

cout

"Passed";

else

cout

"Failed";

if/else

Selection Structure

Nested

if/else

structures

One inside another, test for multiple cases

Once condition met, other statements skipped

if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”

if/else

Selection Structure

Example if

grade

and

above

cout

"A";

else

if

grade

cout

"B";

else

if

grade

cout

"C";

else

if

grade

cout

"D";

else

less

than

cout

"F";

switch Multiple-Selection Structure  switch^ ◦ Test variable for multiple values ◦ Series of case labels and optional default case switch ( variable ) { case value1: // taken if variable == value statementsbreak; // necessary to exit switch case value2:case value3: // taken if variable == value2 or == value statementsbreak;default: // taken if variable matches no other cases statements break; }

switch Multiple-Selection Structure true false

... case a case a action(s) break case b case b action(s) break false false case z case z action(s) break true true default action(s)

// code9.cpp 2 // Counting letter grades. 3 #include 4 5 using std::cout; 6 using std::cin; 7 using std::endl; 8 9 // function main begins program execution 10 int main() 11

int grade; // one grade 13 int aCount = 0; // number of As 14 int bCount = 0; // number of Bs 15 int cCount = 0; // number of Cs 16 int dCount = 0; // number of Ds 17 int fCount = 0; // number of Fs 1819 cout << "Enter the letter grades." << endl 20 << "Enter the EOF character to end input." << endl; 21

// loop until user types end-of-file key sequence 23 while ( ( grade = cin.get() ) != EOF ) { 2425 // determine which grade was input 26 switch ( grade ) { // switch structure nested in while 2728 case 'A': // grade was uppercase A 29 case 'a': // or lowercase a 30 ++aCount; // increment aCount 31 break; // necessary to exit switch 3233 case 'B': // grade was uppercase B 34 case 'b': // or lowercase b 35 ++bCount; // increment bCount 36 break; // exit switch 3738 case 'C': // grade was uppercase C 39 case 'c': // or lowercase c 40 ++cCount; // increment cCount 41 break; // exit switch 42

cin.get()

uses dot

notation. This function gets 1character from the keyboard(after

Enter

pressed), and it is

assigned to

grade

cin.get()

returns EOF

(end-of-file) after the EOFcharacter is input, to indicatethe end of data. EOF may bectrl-d or ctrl-z, depending onyour OS.

Compares

grade

(an

int

) to the numerical

representations of

A

and

a

break

causes

switch

to end

and the program continues with thefirst statement after the

switch

structure.

Assignment statementshave a value, which is thesame as the variable on theleft of the

. The value of

this statement is the sameas the value returned by cin.get()

This can also be used toinitialize multiplevariables: a = b = c = 0;

// output summary of results 68 cout << "\n\nTotals for each letter grade are:" 69 << "\nA: " << aCount // display number of A grades 70 << "\nB: " << bCount // display number of B grades 71 << "\nC: " << cCount // display number of C grades 72 << "\nD: " << dCount // display number of D grades 73 << "\nF: " << fCount // display number of F grades 74 << endl; 7576 return 0; // indicate successful termination 7778 } // end function main

Enter the letter grades.Enter the EOF character to end input.a B c C A d f C E Incorrect letter grade entered. Enter a new grade.D A b ^Z Totals for each letter grade are:A: 3B: 2C: 3D: 2F: 1