Download presentation
Presentation is loading. Please wait.
Published byKristin Lang Modified over 9 years ago
1
Lecture 4 Introduction to Programming
2
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
3
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
4
if ( grade == ‘A’ ) cout << “ Excellent ” ; else if ( grade == ‘B’ ) … else if … … else … if else
5
while loop while (condition) { statements; : }statements;
6
While loop executes zero or more times. What if we want the loop to execute at least one time?
7
do-while
8
Do while loop execute one or more times
9
Syntax of do-while loop do{ statements ; } while ( condition ) ;
10
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 ) ;
11
Flow chart for do-while loop Do-while condition Process Exit true false
12
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 ; }
13
for Loop
14
For loop for ( initialization condition ; termination condition ; increment condition ) { statement ( s ) ; }
15
Example int counter ; for( counter = 0 ; counter < 10 ; counter = counter + 1 ) cout << counter; cout << counter; Output 0123456789
16
Table for 2 2 x 1 = 2 2 x 2 = 4 2 x 3 = 6 :: 2 x 10 = 20
17
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“ ; }}
18
Output 2 x1 = 2 2 x 2 = 4 2 x 3 = 6 :: 2 x 10 = 20
19
Flow chart for the ‘Table’ example counter=1 While Print 2*counter counter <=10? Stop Start No Exit Counter = counter + 1 yes
20
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“ ; }}
21
Increment operator ++ counter ++ ; counter ++ ; same as counter = counter + 1; counter = counter + 1;
22
Decrement operator -- counter -- ; counter -- ; same as counter = counter - 1 counter = counter - 1
23
+= counter += 3 ; counter += 3 ; same as counter = counter + 3 ; counter = counter + 3 ;
24
-= counter -= 5 ; counter -= 5 ; same as counter = counter – 5 ; counter = counter – 5 ;
25
*=x*=2 x = x * 2
26
/= x /= 2 x = x / 2
27
Compound Assignment Operators operator=
28
%= x %= 2 ; x %= 2 ; same as x = x % 2 ; x = x % 2 ;
29
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
30
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
31
switch statement
32
switch statements switch ( variable name ) { case ‘a’ : statements; case ‘b’ : statements; case ‘c’ : statements; … }
33
switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : … … } switch statements
34
case ‘A’ : cout << “ Excellent ” ; … … switch statements
35
Example switch ( grade) { case ‘A’ : cout << “ Excellent ” ; case ‘B’ : cout << “ Very Good ” ; case ‘C’ : cout << “Good ” ; case ‘D’ : cout << “ Poor ” ; case ‘F’ : cout << “ Fail ” ; }
36
break;
37
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 ;}
38
default : cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ; default :
39
switch (grade) Display “Excellent” case ‘B’ : case ‘A’ : Display “Very Good” Default : “……..” Flow Chart of switch statement …
40
if ( amount > 2335.09 ) statements ;
41
Whole Number short short int int long long
42
case ‘A’ : case ‘ 300 ‘ : case ‘ f ‘ :
43
if (c == ‘z’ ) { cout << “ Great ! You have made the correct guess “ ; break ; } break ;
44
continue ;
45
continue while trynum <= 5 ; {….…. continue ; }
46
for ( counter = 0 ;counter <= 10 ; counter ++ ) {……. continue ; } continue in ‘for’ loop
47
Sequential Statements Sequential Statements Decisions Decisions – if, if else, switch Loops Loops – while, do while, for What have we done till now …
48
goto Unconditional Branch of Execution
49
Sequences Sequences Decisions Decisions Loops Loops Structured Programming
50
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
51
Guide lines for structured programming Modular Modular Single entry - single exit Single entry - single exit
52
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
53
Structures Structures Arrays Arrays Character Strings Character Strings Pointers Pointers Next Milestones
Similar presentations
© 2025 SlidePlayer.com. Inc.
All rights reserved.