+ All Categories
Home > Documents > Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables...

Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables...

Date post: 03-Jan-2016
Category:
Upload: brooke-angelica-hunter
View: 216 times
Download: 3 times
Share this document with a friend
Popular Tags:
57
Programming Fundamentals Lecture 4
Transcript
Page 1: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Programming Fundamentals

Lecture 4

Page 2: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Relational Operators

Page 4: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Decision

Page 5: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

If Statement in C

If (condition)statement ;

Page 7: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

If Statement in C

If ( condition ){

statement1 ;statement2 ;

:}

Page 8: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

If statement in C

Page 9: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

• Int n;• Cin>>n;• If(n>10)• {

• Cout<<“uol”;• Cout<<“ok”;

• }

Page 10: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Add two num..r they equal to five??

Page 11: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

if-elseif (condition){

statement ;--

}else{

statement ;--

}

Page 14: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

<100 or >100

• Int n;• Cout<<“enter any integer value”;• Cin>>n;• If(n>100)• Cout<<“>100”;• Else• Cout<<“<100”;

Page 15: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Take avg of 5 students marks and tell if that avg is > or < 100

Page 17: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Syntax

If(condition-1){

If (condition-2){

Statement}Statement

}

Page 19: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

example

Int a,b,c;CoutCin>>aCoutCin>>b;CoutCin>>c;If(a==b){

if(a==c)cout<<“equal”;

}ElseCout<<“different”;}

Page 20: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Nested if-else structure

Used for multiple selectionSyntaxIf(condition1)Statement1;Else if(condition2)Statement2;Else if(condition3)Statement3…....

Page 21: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 22: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Logical Operators

AND &&OR ||

Page 23: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Multi-way decision

Page 25: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

if ( grade == ‘A’ )cout << “ Excellent ” ;

else if ( grade == ‘B’ )…

else if ……

else …

if else

Page 28: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

switch statement

Page 29: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

switch statements

switch ( variable name ){

case ‘a’ :statements;

case ‘b’ :statements;

case ‘c’ : statements;

…}

Page 30: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

switch ( grade){

case ‘A’ :cout << “ Excellent ” ;

case ‘B’ :cout << “ Very Good ” ;

case ‘C’ :…

…}

switch statements

Page 31: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

case ‘A’ :cout << “ Excellent ” ;……

switch statements

Page 32: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

break;

Page 34: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 36: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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

default :

Page 37: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 38: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 39: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 40: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 41: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 42: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 43: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 44: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

gotoUnconditional Branch of Execution

Page 45: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 46: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

GOTO

{Int c=1;abc:Cout<<c<<endl;C++;If(c<=10)Goto abc;}

Page 47: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Limitations of switch

if ( amount > 2335.09 ) statements ;

Page 48: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Whole Number

• short• int• long

Page 49: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

case ‘A’ :case ‘ 300 ‘ :case ‘ f ‘ :

Page 50: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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

Page 51: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

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 52: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Flow Chart Symbols

Start or stop

Process

Flow line

Continuation mark

Decision

Page 53: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 54: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 55: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 56: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.
Page 57: Programming Fundamentals Lecture 4. In the Previous Lecture Basic structure of C program Variables and Data types Operators ‘cout’ and ‘cin’ for output.

Unary Not operator !

!true = false !false = true


Recommended