61
Programming Fundamentals Lecture 5

Programming Fundamentals

Embed Size (px)

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 Presentation

Citation preview

Page 1: Programming Fundamentals

Programming Fundamentals

Lecture 5

Page 2: Programming Fundamentals

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

Relational Operators

Page 4: Programming Fundamentals

Decision

Page 5: Programming Fundamentals

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

If Statement in C

If (condition)statement ;

Page 7: Programming Fundamentals

If Statement in C

If ( condition ){

statement1 ;statement2 ;

:}

Page 8: Programming Fundamentals

If statement in C

Page 9: Programming Fundamentals

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

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

• }

Page 10: Programming Fundamentals

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

Page 11: Programming Fundamentals

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

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

if-elseif (condition){

statement ;--

}else{

statement ;--

}

Page 14: Programming Fundamentals

<100 or >100

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

Page 15: Programming Fundamentals

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

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

Page 17: Programming Fundamentals

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

Syntax

If(condition-1){

If (condition-2){

Statement}Statement

}

Page 19: Programming Fundamentals

example

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

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

}ElseCout<<“different”;}

Page 20: Programming Fundamentals

Nested if-else structure

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

Page 21: Programming Fundamentals
Page 22: Programming Fundamentals

Logical Operators

AND &&OR ||

Page 23: Programming Fundamentals

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

Multi-way decision

Page 25: Programming Fundamentals

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

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

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

else if ( grade == ‘B’ )…

else if ……

else …

if else

Page 28: Programming Fundamentals

switch statement

Page 29: Programming Fundamentals

switch statements

switch ( variable name ){

case ‘a’ :statements;

case ‘b’ :statements;

case ‘c’ : statements;

…}

Page 30: Programming Fundamentals

switch ( grade){

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

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

case ‘C’ :…

…}

switch statements

Page 31: Programming Fundamentals

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

switch statements

Page 32: Programming Fundamentals

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

break;

Page 34: Programming Fundamentals

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

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

default :

Page 36: Programming Fundamentals
Page 37: Programming Fundamentals
Page 38: Programming Fundamentals
Page 39: Programming Fundamentals
Page 40: Programming Fundamentals
Page 41: Programming Fundamentals
Page 42: Programming Fundamentals
Page 43: Programming Fundamentals
Page 44: Programming Fundamentals

GOTO

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

Page 45: Programming Fundamentals

switch (grade)

Display “Excellent”

case ‘B’ :

case ‘A’ :

Display “Very Good”

Default :

“……..”

Flow Chart of switch statement

Page 46: Programming Fundamentals

Limitations of switch

if ( amount > 2335.09 ) statements ;

Page 47: Programming Fundamentals

Whole Number

• short• int• long

Page 48: Programming Fundamentals

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

Page 49: Programming Fundamentals

if (c == ‘z’ )

{

cout << “ Great ! You have made the correct guess “ ;break ;

}

break ;

Page 50: Programming Fundamentals

continue ;

Page 51: Programming Fundamentals

continuewhile trynum <= 5 ;{

….….continue ;

}

Page 52: Programming Fundamentals

for ( counter = 0 ;counter <= 10 ; counter ++ ){

…….continue ;

}

continue in ‘for’ loop

Page 53: Programming Fundamentals

gotoUnconditional Branch of Execution

Page 54: Programming Fundamentals

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

Page 55: Programming Fundamentals

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

Flow Chart Symbols

Start or stop

Process

Flow line

Continuation mark

Decision

Page 57: Programming Fundamentals
Page 58: Programming Fundamentals
Page 59: Programming Fundamentals
Page 60: Programming Fundamentals
Page 61: Programming Fundamentals

Unary Not operator !

!true = false !false = true