programming fundamentals
DESCRIPTION
Programming Fundamentals. Lecture 5. In the Previous Lecture. Basic structure of C program Variables and Data types Operators ‘ cout ’ and ‘ cin ’ for output and input Braces. Relational Operators. Decision. If Statement. If condition is true statements - PowerPoint PPT PresentationTRANSCRIPT
![Page 1: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/1.jpg)
Programming Fundamentals
Lecture 5
![Page 2: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/2.jpg)
In the Previous Lecture
• Basic structure of C program• Variables and Data types• Operators• ‘cout’ and ‘cin’ for output and input• Braces
![Page 3: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/3.jpg)
Relational Operators
![Page 4: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/4.jpg)
Decision
![Page 5: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/5.jpg)
If Statement
If condition is truestatements
If Ali’s height is greater then 6 feetThen
Ali can become a member of the Basket Ball team
![Page 6: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/6.jpg)
If Statement in C
If (condition)statement ;
![Page 7: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/7.jpg)
If Statement in C
If ( condition ){
statement1 ;statement2 ;
:}
![Page 8: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/8.jpg)
If statement in C
![Page 9: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/9.jpg)
• Int n;• Cin>>n;• If(n>10)• {
• Cout<<“uol”;• Cout<<“ok”;
• }
![Page 10: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/10.jpg)
Add two num..r they equal to five??
![Page 11: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/11.jpg)
Divisible by 3
• Int n;• Cout<<“enter any number”;• Cin>>;• If(n%3==0)• {
• Cout<<“the number”<<n<<“is divisible by 3”;
• }
![Page 12: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/12.jpg)
If-else statement
• Used for making two way decsns(need?)• One condition and two blocks of statements are given• After evaluating a condition, one of the block will be executed• If condition is true than the first block will be executed• If condition is false than the second block will be executed
![Page 13: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/13.jpg)
if-elseif (condition){
statement ;--
}else{
statement ;--
}
![Page 14: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/14.jpg)
<100 or >100
• Int n;• Cout<<“enter any integer value”;• Cin>>n;• If(n>100)• Cout<<“>100”;• Else• Cout<<“<100”;
![Page 15: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/15.jpg)
Even or odd
• Int n;• Cout<<“enter an integer”;• Cin>>n;• If(n%2==1)• Cout<<“it is an odd number”;• Else• Cout<<“even”;
![Page 16: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/16.jpg)
Take avg of 5 students marks and tell if that avg is > or < 100
![Page 17: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/17.jpg)
Nested if statement
When an if statement is used within another if statement , it is called nested if statement
Used for multiway (not two way) decision making
![Page 18: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/18.jpg)
Syntax
If(condition-1){
If (condition-2){
Statement}Statement
}
![Page 19: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/19.jpg)
example
Int a,b,c;CoutCin>>aCoutCin>>b;CoutCin>>c;If(a==b){
if(a==c)cout<<“equal”;
}ElseCout<<“different”;}
![Page 20: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/20.jpg)
Nested if-else structure
Used for multiple selectionSyntaxIf(condition1)Statement1;Else if(condition2)Statement2;Else if(condition3)Statement3…....
![Page 21: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/21.jpg)
![Page 22: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/22.jpg)
Logical Operators
AND &&OR ||
![Page 23: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/23.jpg)
Logical OperatorsIf a is greater than b
AND c is greater than d
In Cif(a > b && c> d)if(age > 18 || height > 5)
![Page 24: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/24.jpg)
Multi-way decision
![Page 25: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/25.jpg)
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
![Page 26: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/26.jpg)
if ( grade ==‘A’ )cout << “ Excellent ” ;
else if ( grade ==‘B’ )
cout << “ Very Good ” ;else
if ( grade ==‘C’ )cout << “ Good ” ;
elseif ( grade ==‘D’ )
cout << “ Poor ” ;
if else
![Page 27: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/27.jpg)
if ( grade == ‘A’ )cout << “ Excellent ” ;
else if ( grade == ‘B’ )…
else if ……
else …
if else
![Page 28: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/28.jpg)
switch statement
![Page 29: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/29.jpg)
switch statements
switch ( variable name ){
case ‘a’ :statements;
case ‘b’ :statements;
case ‘c’ : statements;
…}
![Page 30: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/30.jpg)
switch ( grade){
case ‘A’ :cout << “ Excellent ” ;
case ‘B’ :cout << “ Very Good ” ;
case ‘C’ :…
…}
switch statements
![Page 31: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/31.jpg)
case ‘A’ :cout << “ Excellent ” ;……
switch statements
![Page 32: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/32.jpg)
Example
switch ( grade){
case ‘A’ :cout << “ Excellent ” ;
case ‘B’ :cout << “ Very Good ” ;
case ‘C’ :cout << “Good ” ;
case ‘D’ :cout << “ Poor ” ;
case ‘F’ :cout << “ Fail ” ;
}
![Page 33: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/33.jpg)
break;
![Page 34: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/34.jpg)
Exampleswitch ( 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 ;
}
![Page 35: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/35.jpg)
default :cout << “ Please Enter Grade from ‘A’ to ‘D’ or ‘F’ “ ;
default :
![Page 36: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/36.jpg)
![Page 37: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/37.jpg)
![Page 38: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/38.jpg)
![Page 39: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/39.jpg)
![Page 40: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/40.jpg)
![Page 41: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/41.jpg)
![Page 42: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/42.jpg)
![Page 43: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/43.jpg)
![Page 44: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/44.jpg)
GOTO
{Int c=1;abc:Cout<<c<<endl;C++;If(c<=10)Goto abc;}
![Page 45: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/45.jpg)
switch (grade)
Display “Excellent”
case ‘B’ :
case ‘A’ :
Display “Very Good”
Default :
“……..”
Flow Chart of switch statement
…
![Page 46: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/46.jpg)
Limitations of switch
if ( amount > 2335.09 ) statements ;
![Page 47: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/47.jpg)
Whole Number
• short• int• long
![Page 48: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/48.jpg)
case ‘A’ :case ‘ 300 ‘ :case ‘ f ‘ :
![Page 49: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/49.jpg)
if (c == ‘z’ )
{
cout << “ Great ! You have made the correct guess “ ;break ;
}
break ;
![Page 50: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/50.jpg)
continue ;
![Page 51: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/51.jpg)
continuewhile trynum <= 5 ;{
….….continue ;
}
![Page 52: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/52.jpg)
for ( counter = 0 ;counter <= 10 ; counter ++ ){
…….continue ;
}
continue in ‘for’ loop
![Page 53: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/53.jpg)
gotoUnconditional Branch of Execution
![Page 54: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/54.jpg)
• Minimize the use of break• Minimize the use of continue• Never use goto
![Page 55: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/55.jpg)
Flow Charting
• There are different techniques that are used to analyse and design a program. We will use the flow chart technique.
• A flow chart is a pictorial representation of a program.
• There are labelled geometrical symbols, together with the arrows connecting one symbol with other.
![Page 56: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/56.jpg)
Flow Chart Symbols
Start or stop
Process
Flow line
Continuation mark
Decision
![Page 57: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/57.jpg)
![Page 58: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/58.jpg)
![Page 59: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/59.jpg)
![Page 60: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/60.jpg)
![Page 61: Programming Fundamentals](https://reader036.vdocuments.net/reader036/viewer/2022062517/5681368e550346895d9e18a2/html5/thumbnails/61.jpg)
Unary Not operator !
!true = false !false = true