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

Preview:

Citation preview

Programming Fundamentals

Lecture 4

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 truestatements

If Ali’s height is greater then 6 feetThen

Ali can become a member of the Basket Ball team

If Statement in C

If (condition)statement ;

If Statement in C

If ( condition ){

statement1 ;statement2 ;

:}

If statement in C

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

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

• }

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

Divisible by 3

• Int n;• Cout<<“enter any number”;• Cin>>;• If(n%3==0)• {

• Cout<<“the number”<<n<<“is divisible by 3”;

• }

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

if-elseif (condition){

statement ;--

}else{

statement ;--

}

<100 or >100

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

Even or odd

• Int n;• Cout<<“enter an integer”;• Cin>>n;• If(n%2==1)• Cout<<“it is an odd number”;• Else• Cout<<“even”;

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

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

Syntax

If(condition-1){

If (condition-2){

Statement}Statement

}

example

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

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

}ElseCout<<“different”;}

Nested if-else structure

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

Logical Operators

AND &&OR ||

Logical OperatorsIf a is greater than b

AND c is greater than d

In Cif(a > b && c> d)if(age > 18 || height > 5)

Multi-way decision

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 ” ;

elseif ( grade ==‘D’ )

cout << “ Poor ” ;

if else

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

else if ( grade == ‘B’ )…

else if ……

else …

if else

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;

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 ;

}

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

default :

gotoUnconditional Branch of Execution

GOTO

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

Limitations of switch

if ( amount > 2335.09 ) statements ;

Whole Number

• short• int• long

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

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

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.

Flow Chart Symbols

Start or stop

Process

Flow line

Continuation mark

Decision

Unary Not operator !

!true = false !false = true

Recommended