Lecture 4 Introduction to Programming
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 Statements
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 … if else
while loop while (condition) { statements; : }statements;
While loop executes zero or more times. What if we want the loop to execute at least one time?
do-while
Do while loop execute one or more times
Syntax of do-while loop do{ statements ; } while ( condition ) ;
Example-Guessing game char c ; char c ; int tryNum = 1 ; int tryNum = 1 ; do do { cout << "Please enter your guess by pressing a character key from a to z “ ; cout << "Please enter your guess by pressing a character key from a to z “ ; cin >> c ; cin >> c ; if ( c == 'z‘ ) if ( c == 'z‘ ) { cout << "Congratulations! you guessed the right answer“ ; cout << "Congratulations! you guessed the right answer“ ; tryNum = 6 ; tryNum = 6 ; } else else tryNum = tryNum + 1 ; tryNum = tryNum + 1 ; } while ( tryNum <= 5 ) ; } while ( tryNum <= 5 ) ;
Flow chart for do-while loop Do-while condition Process Exit true false
Relational Operators char c ; char c ; int tryNum, maxTries ; int tryNum, maxTries ; tryNum = 1 ; tryNum = 1 ; maxTries = 5 ; maxTries = 5 ; cout << "Guess the alphabet between a to z “ ; cout << "Guess the alphabet between a to z “ ; cin >> c ; cin >> c ; while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) ) while ( ( tryNum <= maxTries ) && ( c! = ‘z‘ ) ) { cout << "Guess the alphabet between a to z “ ; cout << "Guess the alphabet between a to z “ ; cin >> c ; cin >> c ; tryNum = tryNum + 1 ; tryNum = tryNum + 1 ; }
for Loop
For loop for ( initialization condition ; termination condition ; increment condition ) { statement ( s ) ; }
Example int counter ; for( counter = 0 ; counter < 10 ; counter = counter + 1 ) cout << counter; cout << counter; Output
Table for 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 :: 2 x 10 = 20
Example - Calculate Table for 2 #include #include main ( ) { int counter ; for ( counter = 1 ; counter <= 10 ; counter = counter + 1 ) for ( counter = 1 ; counter <= 10 ; counter = counter + 1 ){ cout << "2 x " << counter << " = " << 2* counter << "\n“ ; cout << "2 x " << counter << " = " << 2* counter << "\n“ ; }}
Output 2 x1 = 2 2 x 2 = 4 2 x 3 = 6 :: 2 x 10 = 20
Flow chart for the ‘Table’ example counter=1 While Print 2*counter counter <=10? Stop Start No Exit Counter = counter + 1 yes
Example: Calculate Table- Enhanced #include #include main ( ) { int number ; int number ; int maxMultiplier ; int maxMultiplier ; int counter ; int counter ; maxMultiplier = 10 ; maxMultiplier = 10 ; cout << " Please enter the number for which you wish to construct the table “ ; cout << " Please enter the number for which you wish to construct the table “ ; cin >> number ; cin >> number ; for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 ) for ( counter = 1 ; counter <= maxMultiplier ; counter = counter + 1 ) { cout << number <<" x " << counter<< " = " << number * counter << "\n“ ; cout << number <<" x " << counter<< " = " << number * counter << "\n“ ; }}
Increment operator ++ counter ++ ; counter ++ ; same as counter = counter + 1; counter = counter + 1;
Decrement operator -- counter -- ; counter -- ; same as counter = counter - 1 counter = counter - 1
+= counter += 3 ; counter += 3 ; same as counter = counter + 3 ; counter = counter + 3 ;
-= counter -= 5 ; counter -= 5 ; same as counter = counter – 5 ; counter = counter – 5 ;
*=x*=2 x = x * 2
/= x /= 2 x = x / 2
Compound Assignment Operators operator=
%= x %= 2 ; x %= 2 ; same as x = x % 2 ; x = x % 2 ;
Comments Write comment at the top program to show what it does Write comment at the top program to show what it does Write comments that mean some thing Write comments that mean some thing
int sum; int students ; int average ; sum = 0 ; students = 0 ; do{ cin >> grade ; sum += grade ; students ++ ; } while (grade >= 0) ; average = sum / students ; cout << average ; Example: Program to calculate the average marks of class A Flaw in the code
switch statement
switch statements switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; … }
switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … … } switch statements
case ‘A’ : cout << “ Excellent ” ; … … switch statements
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’ : break ; case ‘C’ : cout << “Good ” ; break ; break ; case ‘D’ : cout << “ Poor ” ; break ; case ‘F’ : cout << “ Fail ” ; break ; case ‘F’ : cout << “ Fail ” ; break ; break ;}
default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ; default :
switch (grade) Display “Excellent” case ‘B’ : case ‘A’ : Display “Very Good” Default : “……..” Flow Chart of switch statement …
if ( amount > ) statements ;
Whole Number short short int int long long
case ‘A’ : case ‘ 300 ‘ : case ‘ f ‘ :
if (c == ‘z’ ) { cout << “ Great ! You have made the correct guess “ ; break ; } break ;
continue ;
continue while trynum <= 5 ; {….…. continue ; }
for ( counter = 0 ;counter <= 10 ; counter ++ ) {……. continue ; } continue in ‘for’ loop
Sequential Statements Sequential Statements Decisions Decisions – if, if else, switch Loops Loops – while, do while, for What have we done till now …
goto Unconditional Branch of Execution
Sequences Sequences Decisions Decisions Loops Loops Structured Programming
Minimize the use of break Minimize the use of break Minimize the use of continue Minimize the use of continue Never use goto Never use goto
Guide lines for structured programming Modular Modular Single entry - single exit Single entry - single exit
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. structured flowcharting constructs. Rule 4 : It says, rule 2 and rule 3 can be repeated as many times as needed repeated as many times as needed Rules for Structured Flowchart
Structures Structures Arrays Arrays Character Strings Character Strings Pointers Pointers Next Milestones