SlideShare a Scribd company logo
DECISIONS
LECTURE # 4
if Statement
Syntax
• Single statement if
if(condition)
statement;
• Multi statement if
if(condition)
{
statement1;
statement2;
}
if Statement Flow Chart
Body of if
Test
Expression
Exit
FalseTrue
if Statement Example
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<“Enter a number”;
cin>>num;
if(num>100)
{
cout<<“Number is greater than 100”;
cout<<endl;
}
return 0;
}
Example
• Generate even number using if and for
loop.
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int i;
for(i=0;i<=100;i++)
{
if(i%2==0)
cout<<i<<endl;
}
getche();
return 0;
}
If…else Statement
Syntax
• Single statement if...else
if(condition)
statement;
else
statement;
• Multi statement if…else
if(condition)
{
statement1;
statement2;
}
else
{
statement1;
statement2;
}
If…else Statement Flow Chart
Body of if
Test
Expression
Exit
FalseTrue
Body of else
If…else Statement Example
#include<iostream>
using namespace std;
int main()
{
int num;
cout<<“Enter a number”;
cin>>num;
if(num>100)
{
cout<<“Number is greater than 100”;
cout<<endl;
}
else
cout<<“Number is not greater than 100”;
return 0;
}
If…else Statement Example
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int chcount=-1;
int wdcount=1;
char ch=‘a’;
cout<<“Enter a phrasen”;
while(ch!=‘r’)
{
ch=getche();
if(ch==‘ ‘)
wdcount++;
else
chcount++;
}
cout<<“nWord
count=“<<wdcount<<endl;
cout<<“Character
count=“<<chcount;
return 0;
}
Nested If…else Statement
Syntax
• Single statement if...else
if(condition)
statement;
else
statement;
• Multi statement if…else
if(condition)
{
statement1;
statement2;
}
else
{
statement1;
statement2;
}
(condition)
(condition)
Nested If…else Example
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char dir=‘a’;
int x=0,y=0;
while(dir!=‘r’)
{
cout<<“Enter your location”;
dir=getche();
if(dir==‘n’)
y--;
else if(dir==‘s’)
y++;
else if(dir==‘e’)
x++;
else if(dir==‘w’)
x--;
}
return 0;
}
Matching else
#include<iostream>
#include<conio.h>
int main()
{
int a,b,c;
cout<<“Enter three numbers:”;
cin>>a>>b>>c;
if(a==b)
if(b==c)
cout<<“b and c are same”;
else
cout<<“b and c are different”;
getche();
retutn 0;
}
Switch Statement
Syntax
switch(n)
{
case 1:
statement
statement;
break;
case 2:
statement
statement;
break;
default:
statement;
statement;
}
1st case body
2nd case body
Default body
Break causes exit from switch
Switch Statement Flow Chart
1st case body
Switch
variable==1st
case constant
Exit
Switch
variable==2nd
case constant
Switch
variable==nth
case constant
2nd case body
Nth case body
Default body
true
true
true
false
false
false
Switch Example
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char dir=‘a’;
int x=0,y=0;
while(dir!=‘r’)
{
cout<<“Enter your
location”;
dir=getche();
switch(dir)
{
case ‘n’: y--; break;
case ‘s’: y++; break;
case ‘e’: x++; break;
case ‘w’: x--; break;
default: cout<<“try
again”;
} //end switch
}
return 0;
}
Conditional Operator ?:
Syntax
result=(Condition)? Expression 1:
Expression 2;
Conditional Operator Flow Chart
Expression 1
Test
Expression
Exit
FalseTrue
Expression 2
Conditional Operator Example
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int x=100, y=0;
int result=(x>y)?x:y;
getche();
return 0;
}
Logical Operators
Operator Effect
&& Logical And
|| Logical Or
! Logical Not
Logical And
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
int number;
cout<<“Enter your numbers”;
cin>>number;
if(number>=90)
cout<<“A”;
else if(number>=70 && number<90)
cout<<“B”;
else if(number>=60 && number<70)
cout<<“C”;
else
cout<<“F”;
getche();
return 0;
}
Logical Or
#include<iostream>
#include<conio.h>
using namespace std;
int main()
{
char dir=‘a’;
int x=0,y=0;
while(dir!=‘r’)
{
cout<<“Enter your
location”;
dir=getche();
if(dir==‘n’ || dir==‘N’)
y--;
else if(dir==‘s’ ||
dir==‘S’)
y++;
else if(dir==‘e’ ||
dir==‘E’)
x++;
else if(dir==‘w’ ||
dir==‘W’)
x--;
}
return 0;
}
Logical Not
#include<iostream>
#include<conio.h>
int main()
{
int x;
cout<<“enter a number”;
cin>>x;
if(!(x==0))
cout<<“Number is not zero”;
getche();
return 0;
}
Operator Precedence
Operator Type Operators
Unary !,++,--
Arithmetic *, /, %
+, -
Relational <, >, <=, >=
==, !=
Logical &&
||
Conditional ?:
Assignment =, +=, -=, *=, /=, %=
Continue Statement
Condition
within loop
continue;
Start of loop
Normal loop return
Example
#include<iostream>
#include<conio.h>
int main()
{
int dividend, divisor;
char ch=‘a’;
while(ch!=‘r’)
{
cout<<“Enter dividend”;
cin>>dividend;
cout<<“Enter divisor”;
cin>>divisor;
if(divisor==0)
continue;
cout<<“Quotient=“<<dividend/divisor;
}
getche();
return 0;
}
goto Statement
Syntax
goto Label;
//other statements
Label:
//control will begin here

More Related Content

What's hot

C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
명신 김
 
c programming
c programmingc programming
c programming
Arun Umrao
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
Mohammad Shaker
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
Chris Ohk
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
ssuserd6b1fd
 
C++ L07-Struct
C++ L07-StructC++ L07-Struct
C++ L07-Struct
Mohammad Shaker
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
Kevlin Henney
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
Farhan Ab Rahman
 
Functional C++
Functional C++Functional C++
Functional C++
Kevlin Henney
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
Mahmoud Samir Fayed
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
Chris Ohk
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
Mohammad Shaker
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentesmfuentessss
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
Appier
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
Farhan Ab Rahman
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
P3 InfoTech Solutions Pvt. Ltd.
 
How to not write a boring test in Golang
How to not write a boring test in GolangHow to not write a boring test in Golang
How to not write a boring test in Golang
Dan Tran
 

What's hot (20)

C++ Lambda and concurrency
C++ Lambda and concurrencyC++ Lambda and concurrency
C++ Lambda and concurrency
 
Constructor,destructors cpp
Constructor,destructors cppConstructor,destructors cpp
Constructor,destructors cpp
 
c programming
c programmingc programming
c programming
 
C++ L03-Control Structure
C++ L03-Control StructureC++ L03-Control Structure
C++ L03-Control Structure
 
C++ Programming - 2nd Study
C++ Programming - 2nd StudyC++ Programming - 2nd Study
C++ Programming - 2nd Study
 
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
Notes for C Programming for MCA, BCA, B. Tech CSE, ECE and MSC (CS) 2 of 5 by...
 
C++ L07-Struct
C++ L07-StructC++ L07-Struct
C++ L07-Struct
 
What We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit TestingWhat We Talk About When We Talk About Unit Testing
What We Talk About When We Talk About Unit Testing
 
Array notes
Array notesArray notes
Array notes
 
Lecture04
Lecture04Lecture04
Lecture04
 
C++ TUTORIAL 2
C++ TUTORIAL 2C++ TUTORIAL 2
C++ TUTORIAL 2
 
Functional C++
Functional C++Functional C++
Functional C++
 
The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184The Ring programming language version 1.5.3 book - Part 91 of 184
The Ring programming language version 1.5.3 book - Part 91 of 184
 
C++ Programming - 4th Study
C++ Programming - 4th StudyC++ Programming - 4th Study
C++ Programming - 4th Study
 
C++ L05-Functions
C++ L05-FunctionsC++ L05-Functions
C++ L05-Functions
 
Programming - Marla Fuentes
Programming - Marla FuentesProgramming - Marla Fuentes
Programming - Marla Fuentes
 
Basic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python ProgrammersBasic C++ 11/14 for Python Programmers
Basic C++ 11/14 for Python Programmers
 
C++ TUTORIAL 5
C++ TUTORIAL 5C++ TUTORIAL 5
C++ TUTORIAL 5
 
Python Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and LoopsPython Programming Essentials - M16 - Control Flow Statements and Loops
Python Programming Essentials - M16 - Control Flow Statements and Loops
 
How to not write a boring test in Golang
How to not write a boring test in GolangHow to not write a boring test in Golang
How to not write a boring test in Golang
 

Viewers also liked

c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4sajidpk92
 
c++ Lecture 1
c++ Lecture 1c++ Lecture 1
c++ Lecture 1sajidpk92
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2sajidpk92
 
AnµLisis De La Pel÷Cula Una Mente Brillante
AnµLisis De La Pel÷Cula Una Mente BrillanteAnµLisis De La Pel÷Cula Una Mente Brillante
AnµLisis De La Pel÷Cula Una Mente Brillantetunegocioweb
 
c++ Lecture 3
c++ Lecture 3c++ Lecture 3
c++ Lecture 3sajidpk92
 
basic c++(1)
basic c++(1)basic c++(1)
basic c++(1)sajidpk92
 
STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)sajidpk92
 

Viewers also liked (8)

c++ Lecture 4
c++ Lecture 4c++ Lecture 4
c++ Lecture 4
 
c++ Lecture 1
c++ Lecture 1c++ Lecture 1
c++ Lecture 1
 
c++ Lecture 2
c++ Lecture 2c++ Lecture 2
c++ Lecture 2
 
Lecture 1
Lecture 1Lecture 1
Lecture 1
 
AnµLisis De La Pel÷Cula Una Mente Brillante
AnµLisis De La Pel÷Cula Una Mente BrillanteAnµLisis De La Pel÷Cula Una Mente Brillante
AnµLisis De La Pel÷Cula Una Mente Brillante
 
c++ Lecture 3
c++ Lecture 3c++ Lecture 3
c++ Lecture 3
 
basic c++(1)
basic c++(1)basic c++(1)
basic c++(1)
 
STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)STEP(Solar Technology for Energy Production)
STEP(Solar Technology for Energy Production)
 

Similar to Lecture 4

FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3 rohassanie
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
khaledahmed316
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2rohassanie
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
Ayman Hassan
 
C++ lecture 02
C++   lecture 02C++   lecture 02
C++ lecture 02
HNDE Labuduwa Galle
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
Aiman Hud
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
Komal Kotak
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
Chhom Karath
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
RithikRaj25
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
Saranya saran
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
ssuser3cbb4c
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
Chandrakant Divate
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
GrejoJoby1
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
Tekle12
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
Sowri Rajan
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
jleed1
 
C++ practical
C++ practicalC++ practical
C++ practical
Rahul juneja
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
Mohammed Sikander
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)jahanullah
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
DEEPAK948083
 

Similar to Lecture 4 (20)

FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
lesson 2.pptx
lesson 2.pptxlesson 2.pptx
lesson 2.pptx
 
FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2FP 201 - Unit 3 Part 2
FP 201 - Unit 3 Part 2
 
MUST CS101 Lab11
MUST CS101 Lab11 MUST CS101 Lab11
MUST CS101 Lab11
 
C++ lecture 02
C++   lecture 02C++   lecture 02
C++ lecture 02
 
POLITEKNIK MALAYSIA
POLITEKNIK MALAYSIAPOLITEKNIK MALAYSIA
POLITEKNIK MALAYSIA
 
Control structure of c
Control structure of cControl structure of c
Control structure of c
 
3. chapter ii
3. chapter ii3. chapter ii
3. chapter ii
 
DataTypes.ppt
DataTypes.pptDataTypes.ppt
DataTypes.ppt
 
Decision making and branching
Decision making and branchingDecision making and branching
Decision making and branching
 
C++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptxC++ lectures all chapters in one slide.pptx
C++ lectures all chapters in one slide.pptx
 
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN CINPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
INPUT AND OUTPUT STATEMENTS IN PROGRAMMING IN C
 
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo JobyC++ and OOPS Crash Course by ACM DBIT | Grejo Joby
C++ and OOPS Crash Course by ACM DBIT | Grejo Joby
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
 
Program flowchart
Program flowchartProgram flowchart
Program flowchart
 
how to reuse code
how to reuse codehow to reuse code
how to reuse code
 
C++ practical
C++ practicalC++ practical
C++ practical
 
CPP Language Basics - Reference
CPP Language Basics - ReferenceCPP Language Basics - Reference
CPP Language Basics - Reference
 
C Sharp Jn (3)
C Sharp Jn (3)C Sharp Jn (3)
C Sharp Jn (3)
 
C programming Control Structure.pptx
C programming Control Structure.pptxC programming Control Structure.pptx
C programming Control Structure.pptx
 

Lecture 4