0% found this document useful (0 votes)
71 views30 pages

Introduction To Programming: Lecture No. 8

The document discusses programming concepts covered in the previous lecture such as loops, operators, and an example program to calculate class averages. It then explains different types of conditional statements like if, if/else, switch, and examples of using break, continue, default, and goto statements. Guidelines for structured programming and flowcharting are also presented emphasizing modularity, single entry/exit points, and using the simplest constructs.

Uploaded by

Ameet
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
71 views30 pages

Introduction To Programming: Lecture No. 8

The document discusses programming concepts covered in the previous lecture such as loops, operators, and an example program to calculate class averages. It then explains different types of conditional statements like if, if/else, switch, and examples of using break, continue, default, and goto statements. Guidelines for structured programming and flowcharting are also presented emphasizing modularity, single entry/exit points, and using the simplest constructs.

Uploaded by

Ameet
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPT, PDF, TXT or read online on Scribd
You are on page 1/ 30

Introduction to Programming

Lecture No. 8

In the last lecture

Loops
While Do while For

Operators
Increment / Decrement Compound Assignment Operators

Example: Program to calculate the average marks of class


int sum; int students ; int average ; sum = 0 ; students = 0 ; do { cin >> grade ; sum += grade ; students ++ ; } while (grade >= 0) ; average = sum / students ; cout << average ;

A Flaw in the code

Multi-way decision

if Statements
if ( grade ==A ) cout << Excellent ; if ( grade ==B ) cout << Very Good ; if ( grade ==C ) cout << Good ; if ( grade ==D ) cout << Poor ; if ( grade ==F ) cout << Fail ;

if else
if ( grade ==A ) cout << Excellent ; else if ( grade ==B ) cout << Very Good ; else if ( grade ==C ) cout << Good ; else if ( grade ==D ) cout << Poor ;

if else
if ( grade == A ) cout << Excellent ; else if ( grade == B ) else if else

switch statement

switch statements
switch ( variable name ) { case a : statements; case b : statements; case c : statements; }

switch statements
switch ( grade) { case A : cout << Excellent ; case B : cout << Very Good ; case C : }

switch statements
case A : cout << Excellent ;

Example
switch ( grade) { case A : cout << Excellent ; case B : cout << Very Good ; case C : cout << Good ; case D : cout << Poor ; case F : cout << Fail ; }

break;

Example
switch ( grade ) { case A : cout << Excellent ; break ; case B : cout << Very Good ; break ; case C : cout << Good ; break ; case D : cout << Poor ; break ; case F : cout << Fail ; break ; }

default :
default : cout << Please Enter Grade from A to D or F ;

Flow Chart of switch statement


switch (grade)
case A : Display Excellent case B : Display Very Good

Default :

..

if ( amount > 2335.09 ) statements ;

Whole Number

short int long

case A : case 300 : case f :

break ;
if (c == z ) { cout << Great ! You have made the correct guess ; break ;

continue ;

continue
while trynum <= 5 ; { . . continue ; }

continue in for loop


for ( counter = 0 ;counter <= 10 ; counter ++ ) { . continue ; }

What have we done till now


Sequential Statements Decisions


if , if else , switch

Loops
while , do while , for

goto
Unconditional Branch of Execution

Structured Programming

Sequences Decisions Loops

Minimize the use of break Minimize the use of continue Never use goto

Guide lines for structured programming


Modular Single entry - single exit

Rules for Structured Flowchart


Rule 1 : Use the simplest flowchart Rule 2 : Any rectangle can be replaced by two rectangles. Rule 3 : Any rectangle can be replaced with structured flowcharting constructs. Rule 4 : It says, rule 2 and rule 3 can be repeated as many times as needed

Next Milestones

Data Structures
Arrays

Character Strings Pointers

You might also like