SlideShare a Scribd company logo
1 of 41
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Unit-1
Principles of Object Oriented
Programming
Tokens, expressions & Control Statements
(14 MARKS)
B.C.A & B.Sc.(IT) – 3
CS-13 C++ and Object Oriented
Programming
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Control Structures
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Control Structures
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• In C++ programming, if statement is used
to test the condition. There are various
types of if statements in C++.
1. if statement
2. if-else statement
3. nested if statement
4. if-else-if ladder
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
IF Statement
• If the Boolean expression evaluates
to true, then the block of code inside the if
statement will be executed.
• If Boolean expression evaluates to false,
then the first set of code after the end of
the if statement (after the closing curly
brace) will be executed.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
IF Statement
• The C++ if statement tests the condition. It
is executed if condition is true.
if(condition)
{
//code to be executed
}
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
C++ If Example: 1_IFExample.cpp
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
if...else statement
• An if statement can be followed by an
optional else statement, which executes
when the Boolean expression is false.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Syntax
if(boolean_expression)
{
// statement(s) will execute if the boolean expression is
true
}else{
// statement(s) will execute if the boolean expression is
false
}
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• If the boolean expression evaluates to true,
then the if block of code will be executed,
otherwise else block of code will be
executed.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Example: 2_IF_Else.cpp
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
if...else if...else Statement
• An if statement can be followed by an
optional else if...else statement, which is
very useful to test various conditions using
single if...else if statement.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Syntax and Example:3_if_elseif_else.cpp
if(boolean_expression 1)
{
// Executes when the boolean expression 1 is true
} else if( boolean_expression 2) {
// Executes when the boolean expression 2 is true
} else if( boolean_expression 3) {
// Executes when the boolean expression 3 is true
} else
{
// executes when the none of the above condition is true.
}
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
nested if statements
• It is always legal to nest if-else statements,
which means you can use one if or else if
statement inside another if or else if
statement(s).
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Syntax
if( boolean_expression 1)
{
// Executes when the boolean expression 1 is true
if(boolean_expression 2)
{
// Executes when the boolean expression 2 is true
}
}
Let’s see example -> Example: Nested_if.cpp
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
switch statement
• A switch statement allows a variable to be
tested for equality against a list of values.
Each value is called a case, and the variable
being switched on is checked for each case.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Syntax
switch(expression)
{
case constant-expression :
statement(s);
break; //optional
case constant-expression :
statement(s);
break; //optional
// you can have any number of case statements.
default : //Optional
statement(s);
}
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Example: 5_Switch.cpp
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Looping Control
Structure
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• There may be a situation, when you need
to execute a block of code several number
of times. In general, statements are
executed sequentially: The first statement
in a function is executed first, followed by
the second, and so on.
• Programming languages provide various
control structures that allow for more
complicated execution paths.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• A loop statement allows us to execute a
statement or group of statements multiple
times and following is the general from of
a loop statement in most of the
programming languages −
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• C++ programming language provides the
following type of loops to handle looping
requirements.
1. for,
2. while,
3. do… while
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
For Loop
• A for loop is a repetition control structure
that allows you to efficiently write a loop
that needs to execute a specific number of
times.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Syntax
The syntax of a for loop in C++ is −
for ( init; condition; increment )
{
statement(s);
}
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• Here is the flow of control in a for loop −
• The init step is executed first, and only
once. This step allows you to declare and
initialize any loop control variables. You
are not required to put a statement here, as
long as a semicolon appears.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• Next, the condition is evaluated. If it is
true, the body of the loop is executed. If it is
false, the body of the loop does not execute
and flow of control jumps to the next
statement just after the for loop.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• After the body of the for loop executes, the
flow of control jumps back up to
the increment statement.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• The condition is now evaluated again. If it
is true, the loop executes and the process
repeats itself (body of loop, then increment
step, and then again condition). After the
condition becomes false, the for loop
terminates.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Example: ForLoop.cpp
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
while loop
• A while loop statement repeatedly
executes a target statement as long as a
given condition is true.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Syntax
• The syntax of a while loop in C++ is -
while(condition)
{
statement(s);
}
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• Here, statement(s) may be a single
statement or a block of statements.
The condition may be any expression, and
true is any non-zero value. The loop
iterates while the condition is true.
• When the condition becomes false,
program control passes to the line
immediately following the loop.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Example:9_while.cpp
Example:9_whileLoop.cpp
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• Here, key point of the while loop is that the
loop might not ever run. When the
condition is tested and the result is false,
the loop body will be skipped and the first
statement after the while loop will be
executed.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
do...while loop
• Unlike for and while loops, which test the
loop condition at the top of the loop,
the do...while loop checks its condition at the
bottom of the loop.
• A do...while loop is similar to a while loop,
except that a do...while loop is guaranteed to
execute at least one time.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Syntax
do
{
statement(s);
}
while( condition );
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• Notice that the conditional expression
appears at the end of the loop, so the
statement(s) in the loop execute once
before the condition is tested.
• If the condition is true, the flow of control
jumps back up to do, and the statement(s)
in the loop execute again. This process
repeats until the given condition becomes
false.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Example: 10_do_while.cpp
Example: dowhile.cpp
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
Example: 6_infinite.cpp
#include <iostream>
using namespace std;
int main ()
{
for( ; ; )
{
printf("This loop will run forever.n");
}
return 0; }
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• A loop becomes infinite loop if a condition
never becomes false. The for loop is
traditionally used for this purpose. Since
none of the three expressions that form the
‘for’ loop are required, you can make an
endless loop by leaving the conditional
expression empty.
prezentr.com!
Prepared By: Asst. Prof. Sejal Jadav
• When the conditional expression is absent,
it is assumed to be true. You may have an
initialization and increment expression,
but C++ programmers more commonly use
the ‘for (;;)’ construct to signify an infinite
loop.
• NOTE − You can terminate an infinite loop
by pressing Ctrl + C keys.

More Related Content

What's hot

Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentalscbcunc
 
testing for people who hate testing
testing for people who hate testingtesting for people who hate testing
testing for people who hate testingBram Vogelaar
 
Static typing and proof in ATS language
Static typing and proof in ATS languageStatic typing and proof in ATS language
Static typing and proof in ATS languageKiwamu Okabe
 
ATS/LF for Coq users
ATS/LF for Coq usersATS/LF for Coq users
ATS/LF for Coq usersKiwamu Okabe
 
Embedded application designed by ATS language
Embedded application designed by ATS languageEmbedded application designed by ATS language
Embedded application designed by ATS languageKiwamu Okabe
 
Hands-on VeriFast with STM32 microcontroller
Hands-on VeriFast with STM32 microcontrollerHands-on VeriFast with STM32 microcontroller
Hands-on VeriFast with STM32 microcontrollerKiwamu Okabe
 
Start! ATS programming
Start! ATS programmingStart! ATS programming
Start! ATS programmingKiwamu Okabe
 
Metasepi team meeting #14: ATS programming on MCU
Metasepi team meeting #14: ATS programming on MCUMetasepi team meeting #14: ATS programming on MCU
Metasepi team meeting #14: ATS programming on MCUKiwamu Okabe
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy codeLars Thorup
 
Metasepi team meeting #16: Safety on ATS language + MCU
Metasepi team meeting #16: Safety on ATS language + MCUMetasepi team meeting #16: Safety on ATS language + MCU
Metasepi team meeting #16: Safety on ATS language + MCUKiwamu Okabe
 
10control statement in c#
10control statement in c#10control statement in c#
10control statement in c#Sireesh K
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummiesHarry Potter
 
Refactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGLuca Minudel
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy CodeRowan Merewood
 
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...Eclipse Day India
 
Metasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCUMetasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCUKiwamu Okabe
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignVictor Rentea
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails AppsRabble .
 
Functional IoT: Programming Language and OS
Functional IoT: Programming Language and OSFunctional IoT: Programming Language and OS
Functional IoT: Programming Language and OSKiwamu Okabe
 

What's hot (19)

Python Testing Fundamentals
Python Testing FundamentalsPython Testing Fundamentals
Python Testing Fundamentals
 
testing for people who hate testing
testing for people who hate testingtesting for people who hate testing
testing for people who hate testing
 
Static typing and proof in ATS language
Static typing and proof in ATS languageStatic typing and proof in ATS language
Static typing and proof in ATS language
 
ATS/LF for Coq users
ATS/LF for Coq usersATS/LF for Coq users
ATS/LF for Coq users
 
Embedded application designed by ATS language
Embedded application designed by ATS languageEmbedded application designed by ATS language
Embedded application designed by ATS language
 
Hands-on VeriFast with STM32 microcontroller
Hands-on VeriFast with STM32 microcontrollerHands-on VeriFast with STM32 microcontroller
Hands-on VeriFast with STM32 microcontroller
 
Start! ATS programming
Start! ATS programmingStart! ATS programming
Start! ATS programming
 
Metasepi team meeting #14: ATS programming on MCU
Metasepi team meeting #14: ATS programming on MCUMetasepi team meeting #14: ATS programming on MCU
Metasepi team meeting #14: ATS programming on MCU
 
Unit testing legacy code
Unit testing legacy codeUnit testing legacy code
Unit testing legacy code
 
Metasepi team meeting #16: Safety on ATS language + MCU
Metasepi team meeting #16: Safety on ATS language + MCUMetasepi team meeting #16: Safety on ATS language + MCU
Metasepi team meeting #16: Safety on ATS language + MCU
 
10control statement in c#
10control statement in c#10control statement in c#
10control statement in c#
 
Google mock for dummies
Google mock for dummiesGoogle mock for dummies
Google mock for dummies
 
Refactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENGRefactoring legacy code driven by tests - ENG
Refactoring legacy code driven by tests - ENG
 
Living With Legacy Code
Living With Legacy CodeLiving With Legacy Code
Living With Legacy Code
 
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
Please Behave Yourself: BDD and automating Eclipse RCP applications using JBe...
 
Metasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCUMetasepi team meeting #20: Start! ATS programming on MCU
Metasepi team meeting #20: Start! ATS programming on MCU
 
The Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable DesignThe Art of Unit Testing - Towards a Testable Design
The Art of Unit Testing - Towards a Testable Design
 
Testing Legacy Rails Apps
Testing Legacy Rails AppsTesting Legacy Rails Apps
Testing Legacy Rails Apps
 
Functional IoT: Programming Language and OS
Functional IoT: Programming Language and OSFunctional IoT: Programming Language and OS
Functional IoT: Programming Language and OS
 

Similar to C++ unit-1-part-15

Similar to C++ unit-1-part-15 (20)

C++ unit-1-part-14
C++ unit-1-part-14C++ unit-1-part-14
C++ unit-1-part-14
 
Learn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & LoopsLearn C# Programming - Decision Making & Loops
Learn C# Programming - Decision Making & Loops
 
Unit II.pptx
Unit II.pptxUnit II.pptx
Unit II.pptx
 
Decision Making & Loops
Decision Making & LoopsDecision Making & Loops
Decision Making & Loops
 
DECISION MAKING.pptx
DECISION MAKING.pptxDECISION MAKING.pptx
DECISION MAKING.pptx
 
Loops in c
Loops in cLoops in c
Loops in c
 
Chapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java StatementsChapter 2 : Programming with Java Statements
Chapter 2 : Programming with Java Statements
 
Programming in Arduino (Part 2)
Programming in Arduino  (Part 2)Programming in Arduino  (Part 2)
Programming in Arduino (Part 2)
 
05 Conditional statements
05 Conditional statements05 Conditional statements
05 Conditional statements
 
Loop structures
Loop structuresLoop structures
Loop structures
 
Final requirement
Final requirementFinal requirement
Final requirement
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Control structure
Control structureControl structure
Control structure
 
Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2Introduction To Programming with Python Lecture 2
Introduction To Programming with Python Lecture 2
 
Java chapter 3
Java chapter 3Java chapter 3
Java chapter 3
 
M C6java6
M C6java6M C6java6
M C6java6
 
Programming Fundamentals in C++ structures
Programming Fundamentals in  C++ structuresProgramming Fundamentals in  C++ structures
Programming Fundamentals in C++ structures
 
Lecture 10
Lecture 10Lecture 10
Lecture 10
 
control statements
control statementscontrol statements
control statements
 
05. Control Structures.ppt
05. Control Structures.ppt05. Control Structures.ppt
05. Control Structures.ppt
 

More from Jadavsejal

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassJadavsejal
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event HandlingJadavsejal
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingJadavsejal
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout ManagersJadavsejal
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3Jadavsejal
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2Jadavsejal
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1Jadavsejal
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13Jadavsejal
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12Jadavsejal
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11Jadavsejal
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10Jadavsejal
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9Jadavsejal
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8Jadavsejal
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7Jadavsejal
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6Jadavsejal
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5Jadavsejal
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4Jadavsejal
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2Jadavsejal
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3Jadavsejal
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1Jadavsejal
 

More from Jadavsejal (20)

Programming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone ClassProgramming with Java Concept: Java TimeZone Class
Programming with Java Concept: Java TimeZone Class
 
Programming Java Concept of Event Handling
Programming Java Concept of Event HandlingProgramming Java Concept of Event Handling
Programming Java Concept of Event Handling
 
Programming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event HandlingProgramming Java GUI using SWING, Event Handling
Programming Java GUI using SWING, Event Handling
 
concept of applet and concept of Layout Managers
concept of applet and concept of Layout Managersconcept of applet and concept of Layout Managers
concept of applet and concept of Layout Managers
 
C++ unit-2-part-3
C++ unit-2-part-3C++ unit-2-part-3
C++ unit-2-part-3
 
C++ unit-2-part-2
C++ unit-2-part-2C++ unit-2-part-2
C++ unit-2-part-2
 
C++ unit-2-part-1
C++ unit-2-part-1C++ unit-2-part-1
C++ unit-2-part-1
 
C++ unit-1-part-13
C++ unit-1-part-13C++ unit-1-part-13
C++ unit-1-part-13
 
C++ unit-1-part-12
C++ unit-1-part-12C++ unit-1-part-12
C++ unit-1-part-12
 
C++ unit-1-part-11
C++ unit-1-part-11C++ unit-1-part-11
C++ unit-1-part-11
 
C++ unit-1-part-10
C++ unit-1-part-10C++ unit-1-part-10
C++ unit-1-part-10
 
C++ unit-1-part-9
C++ unit-1-part-9C++ unit-1-part-9
C++ unit-1-part-9
 
C++ unit-1-part-8
C++ unit-1-part-8C++ unit-1-part-8
C++ unit-1-part-8
 
C++ unit-1-part-7
C++ unit-1-part-7C++ unit-1-part-7
C++ unit-1-part-7
 
C++ unit-1-part-6
C++ unit-1-part-6C++ unit-1-part-6
C++ unit-1-part-6
 
C++ unit-1-part-5
C++ unit-1-part-5C++ unit-1-part-5
C++ unit-1-part-5
 
C++ unit-1-part-4
C++ unit-1-part-4C++ unit-1-part-4
C++ unit-1-part-4
 
C++ unit-1-part-2
C++ unit-1-part-2C++ unit-1-part-2
C++ unit-1-part-2
 
C++ unit-1-part-3
C++ unit-1-part-3C++ unit-1-part-3
C++ unit-1-part-3
 
C++-Unit-1-Part-1
C++-Unit-1-Part-1C++-Unit-1-Part-1
C++-Unit-1-Part-1
 

Recently uploaded

办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一
办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一
办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一z xss
 
LESSON 1- Eyong Pantahanan at Pangkabuhayan.pptx
LESSON 1- Eyong Pantahanan at Pangkabuhayan.pptxLESSON 1- Eyong Pantahanan at Pangkabuhayan.pptx
LESSON 1- Eyong Pantahanan at Pangkabuhayan.pptxPascualJaniceC
 
EMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptxEMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptxSarmad Naeem
 
Air Pollution Control Technique and application.
Air Pollution Control Technique and application.Air Pollution Control Technique and application.
Air Pollution Control Technique and application.yadavsuyash008
 
'Upcycling Research' presentation for SNU GSES
'Upcycling Research' presentation for SNU GSES'Upcycling Research' presentation for SNU GSES
'Upcycling Research' presentation for SNU GSESKyungeun Sung
 
Research Methodology Book For New BS Student
Research Methodology Book For New BS StudentResearch Methodology Book For New BS Student
Research Methodology Book For New BS Studentiziaurrahmankhattak
 
5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdf
5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdf5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdf
5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdfsrivastavaakshat51
 
9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhi
9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhi9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhi
9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhidelih Escorts
 
Group 4The Species of the Atlantic Forest.pdf
Group 4The Species of the Atlantic Forest.pdfGroup 4The Species of the Atlantic Forest.pdf
Group 4The Species of the Atlantic Forest.pdfs2015004
 
Making a Difference: Understanding the Upcycling and Recycling Difference
Making a Difference: Understanding the Upcycling and Recycling DifferenceMaking a Difference: Understanding the Upcycling and Recycling Difference
Making a Difference: Understanding the Upcycling and Recycling DifferenceSwag Cycle
 
Title-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptxTitle-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptxSagar Chaudhary
 
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书zdzoqco
 
Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作
Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作
Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作f3774p8b
 
Slide deck for the IPCC Briefing to Latvian Parliamentarians
Slide deck for the IPCC Briefing to Latvian ParliamentariansSlide deck for the IPCC Briefing to Latvian Parliamentarians
Slide deck for the IPCC Briefing to Latvian Parliamentariansipcc-media
 
https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/
https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/
https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/dikjog
 
毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree 毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree ttt fff
 
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best ServicesDwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Servicesnajka9823
 

Recently uploaded (20)

PLANTILLAS DE MEMORAMA CIENCIAS NATURALES
PLANTILLAS DE MEMORAMA CIENCIAS NATURALESPLANTILLAS DE MEMORAMA CIENCIAS NATURALES
PLANTILLAS DE MEMORAMA CIENCIAS NATURALES
 
办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一
办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一
办理(Victoria毕业证书)维多利亚大学毕业证成绩单原版一比一
 
LESSON 1- Eyong Pantahanan at Pangkabuhayan.pptx
LESSON 1- Eyong Pantahanan at Pangkabuhayan.pptxLESSON 1- Eyong Pantahanan at Pangkabuhayan.pptx
LESSON 1- Eyong Pantahanan at Pangkabuhayan.pptx
 
EMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptxEMP (Environment Management Plan . .pptx
EMP (Environment Management Plan . .pptx
 
Air Pollution Control Technique and application.
Air Pollution Control Technique and application.Air Pollution Control Technique and application.
Air Pollution Control Technique and application.
 
'Upcycling Research' presentation for SNU GSES
'Upcycling Research' presentation for SNU GSES'Upcycling Research' presentation for SNU GSES
'Upcycling Research' presentation for SNU GSES
 
Research Methodology Book For New BS Student
Research Methodology Book For New BS StudentResearch Methodology Book For New BS Student
Research Methodology Book For New BS Student
 
5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdf
5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdf5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdf
5 Wondrous Places You Should Visit at Least Once in Your Lifetime (1).pdf
 
9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhi
9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhi9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhi
9873940964 Full Enjoy 24/7 Call Girls Near Shangri La’s Eros Hotel, New Delhi
 
Group 4The Species of the Atlantic Forest.pdf
Group 4The Species of the Atlantic Forest.pdfGroup 4The Species of the Atlantic Forest.pdf
Group 4The Species of the Atlantic Forest.pdf
 
Making a Difference: Understanding the Upcycling and Recycling Difference
Making a Difference: Understanding the Upcycling and Recycling DifferenceMaking a Difference: Understanding the Upcycling and Recycling Difference
Making a Difference: Understanding the Upcycling and Recycling Difference
 
Title-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptxTitle-Role of forestry in restoration of degraded lands.pptx
Title-Role of forestry in restoration of degraded lands.pptx
 
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
办理英属哥伦比亚大学毕业证成绩单|购买加拿大UBC文凭证书
 
Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作
Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作
Düsseldorf U学位证,杜塞尔多夫大学毕业证书1:1制作
 
Slide deck for the IPCC Briefing to Latvian Parliamentarians
Slide deck for the IPCC Briefing to Latvian ParliamentariansSlide deck for the IPCC Briefing to Latvian Parliamentarians
Slide deck for the IPCC Briefing to Latvian Parliamentarians
 
https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/
https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/
https://www.facebook.com/people/Proper-Keto-Capsules-UK/61557989683758/
 
毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree 毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
毕业文凭制作#回国入职#diploma#degree美国密苏里大学毕业证成绩单pdf电子版制作修改#毕业文凭制作#回国入职#diploma#degree
 
Health Facility Electrification: State of Play
Health Facility Electrification: State of PlayHealth Facility Electrification: State of Play
Health Facility Electrification: State of Play
 
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best ServicesDwarka Call Girls 9643097474 Phone Number 24x7 Best Services
Dwarka Call Girls 9643097474 Phone Number 24x7 Best Services
 
Biopesticide. pptx.
Biopesticide. pptx.Biopesticide. pptx.
Biopesticide. pptx.
 

C++ unit-1-part-15

  • 1. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Unit-1 Principles of Object Oriented Programming Tokens, expressions & Control Statements (14 MARKS) B.C.A & B.Sc.(IT) – 3 CS-13 C++ and Object Oriented Programming
  • 2. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Control Structures
  • 3. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Control Structures
  • 4. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • In C++ programming, if statement is used to test the condition. There are various types of if statements in C++. 1. if statement 2. if-else statement 3. nested if statement 4. if-else-if ladder
  • 5. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav IF Statement • If the Boolean expression evaluates to true, then the block of code inside the if statement will be executed. • If Boolean expression evaluates to false, then the first set of code after the end of the if statement (after the closing curly brace) will be executed.
  • 6. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav IF Statement • The C++ if statement tests the condition. It is executed if condition is true. if(condition) { //code to be executed }
  • 7. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav C++ If Example: 1_IFExample.cpp
  • 8. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav if...else statement • An if statement can be followed by an optional else statement, which executes when the Boolean expression is false.
  • 9. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Syntax if(boolean_expression) { // statement(s) will execute if the boolean expression is true }else{ // statement(s) will execute if the boolean expression is false }
  • 10. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • If the boolean expression evaluates to true, then the if block of code will be executed, otherwise else block of code will be executed.
  • 11. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Example: 2_IF_Else.cpp
  • 12. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav if...else if...else Statement • An if statement can be followed by an optional else if...else statement, which is very useful to test various conditions using single if...else if statement.
  • 13. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Syntax and Example:3_if_elseif_else.cpp if(boolean_expression 1) { // Executes when the boolean expression 1 is true } else if( boolean_expression 2) { // Executes when the boolean expression 2 is true } else if( boolean_expression 3) { // Executes when the boolean expression 3 is true } else { // executes when the none of the above condition is true. }
  • 14. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav nested if statements • It is always legal to nest if-else statements, which means you can use one if or else if statement inside another if or else if statement(s).
  • 15. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Syntax if( boolean_expression 1) { // Executes when the boolean expression 1 is true if(boolean_expression 2) { // Executes when the boolean expression 2 is true } } Let’s see example -> Example: Nested_if.cpp
  • 16. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav switch statement • A switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each case.
  • 17. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Syntax switch(expression) { case constant-expression : statement(s); break; //optional case constant-expression : statement(s); break; //optional // you can have any number of case statements. default : //Optional statement(s); }
  • 18. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Example: 5_Switch.cpp
  • 19. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Looping Control Structure
  • 20. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • There may be a situation, when you need to execute a block of code several number of times. In general, statements are executed sequentially: The first statement in a function is executed first, followed by the second, and so on. • Programming languages provide various control structures that allow for more complicated execution paths.
  • 21. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • A loop statement allows us to execute a statement or group of statements multiple times and following is the general from of a loop statement in most of the programming languages −
  • 22. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • C++ programming language provides the following type of loops to handle looping requirements. 1. for, 2. while, 3. do… while
  • 23. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav For Loop • A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a specific number of times.
  • 24. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Syntax The syntax of a for loop in C++ is − for ( init; condition; increment ) { statement(s); }
  • 25. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • Here is the flow of control in a for loop − • The init step is executed first, and only once. This step allows you to declare and initialize any loop control variables. You are not required to put a statement here, as long as a semicolon appears.
  • 26. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is false, the body of the loop does not execute and flow of control jumps to the next statement just after the for loop.
  • 27. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • After the body of the for loop executes, the flow of control jumps back up to the increment statement.
  • 28. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • The condition is now evaluated again. If it is true, the loop executes and the process repeats itself (body of loop, then increment step, and then again condition). After the condition becomes false, the for loop terminates.
  • 29. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Example: ForLoop.cpp
  • 30. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav while loop • A while loop statement repeatedly executes a target statement as long as a given condition is true.
  • 31. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Syntax • The syntax of a while loop in C++ is - while(condition) { statement(s); }
  • 32. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • Here, statement(s) may be a single statement or a block of statements. The condition may be any expression, and true is any non-zero value. The loop iterates while the condition is true. • When the condition becomes false, program control passes to the line immediately following the loop.
  • 33. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Example:9_while.cpp Example:9_whileLoop.cpp
  • 34. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • Here, key point of the while loop is that the loop might not ever run. When the condition is tested and the result is false, the loop body will be skipped and the first statement after the while loop will be executed.
  • 35. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav do...while loop • Unlike for and while loops, which test the loop condition at the top of the loop, the do...while loop checks its condition at the bottom of the loop. • A do...while loop is similar to a while loop, except that a do...while loop is guaranteed to execute at least one time.
  • 36. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Syntax do { statement(s); } while( condition );
  • 37. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • Notice that the conditional expression appears at the end of the loop, so the statement(s) in the loop execute once before the condition is tested. • If the condition is true, the flow of control jumps back up to do, and the statement(s) in the loop execute again. This process repeats until the given condition becomes false.
  • 38. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Example: 10_do_while.cpp Example: dowhile.cpp
  • 39. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav Example: 6_infinite.cpp #include <iostream> using namespace std; int main () { for( ; ; ) { printf("This loop will run forever.n"); } return 0; }
  • 40. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • A loop becomes infinite loop if a condition never becomes false. The for loop is traditionally used for this purpose. Since none of the three expressions that form the ‘for’ loop are required, you can make an endless loop by leaving the conditional expression empty.
  • 41. prezentr.com! Prepared By: Asst. Prof. Sejal Jadav • When the conditional expression is absent, it is assumed to be true. You may have an initialization and increment expression, but C++ programmers more commonly use the ‘for (;;)’ construct to signify an infinite loop. • NOTE − You can terminate an infinite loop by pressing Ctrl + C keys.