SlideShare a Scribd company logo
1 of 42
CONDITIONAL
STRUCTURE
IN
C++
.
INTRODUCTION
Conditional statement are
used to execute a set of
statements on some
conditions. It provide a unit of
block in which we can either
one statement or more than
one statements.
IF THE GIVEN
CONDITION IS TRUE
THAN THE SET OF
STATEMENT ARE
EXECUTED
OTHERWISE BODY IS
SKIPPED….
IF CONDITION
IT IS CONDITIONAL STATEMENT,
WHICH IS USED TO EXECUTE A
SET OF STATEMENT ON SOME
CONDITIONS. THE CONDITION
MUST BE OF “BOOLEAN” TYPE
AN EXPRESSION, WHICH
RETURNS ONLY TWO
VALUE EITHER TRUE OR
FALSE, IS KNOWN AS
BOOLEAN TYPE
EXPRESSION.
SYNTAX:
THE SYNTAX OF IF STATEMENT
IS AS FOLLOWS:
IF(CONDITION)
STATEMENT;
THE ABOVE SYNTAX IS USED
FOR SINGLE STATEMENT. A SET
OF STATEMENT CAN ALSO BE
MADE CONDITIONAL.
IN THIS CASE, THESE STATEMENT
ARE WRITTEN IN CURLY BRACES.
THE SET OF STATEMENTS IS ALSO
CALLED COMPOUND
STATEMENT.
THE SYNTAX FOR COMPOUND
STATEMENTS IN IF STATEMENT IS
AS FOLLOWS:
IF(CONDITION)
{
STATEMENT 1;
STATEMENT 2;
STATEMENT 3;
STATEMENT 4;
.
.
.
STATEMENT N;
}
EXAMPLE:
WRITE A PROGRAM THAT
INPUTS TWO NUMBERS AND
FINDS WHETHER BOTH ARE
EQUAL.
#INCLUDE <IOSTREAM.H>
#INCLUDE <CONIO.H>
VOID MAIN()
CLRSCR();
INT A,B;
COUT<<’’ENTER A NUMBER:’’;
CIN>>A;
COUT<<’’ENTER A NUMBER:’’;
CIN>>B;
IF(A==B)
COUT<<’’BOTH
NUMBERS ARE EQUAL.’’;
GETCH();
}
OUTPUT:
ENTER A NUMBER: 15
ENTER A NUMBER: 15
BOTH NUMBERS ARE EQUAL
IF-ELSE CONDITION
IT IS KNOWN AS DOUBLE
BLOCKED CONDITIONAL
STATEMENT. IT MEANS, IT
HAS TRUE PARTS AS WELL
AS FALSE PART.
IF THE GIVEN CONDITION IS
TRUE THEN THE TRUE PART
IS EXECUTED OTHERWISE
FALSE PART IS EXECUTED.
SYNTAX:
IF(CONDITION)
STATEMENT;
ELSE
STATEMENT;
TWO OR MORE STATEMENTS ARE
WRITTEN IN CURLY
BRACKETS{}.THE SYNTAX FOR
COMPOUND STATEMENTS IN
STATEMENT IS AS FOLLOWS:
IF(CONDITION)
{
STATEMENT 1;
STATEMENT 2;
.
.
STATEMENT N;
}
ELSE
{
STATEMENT 1;
STATEMENT 2;
.
.
STATEMENT N;
}
EXAMPLE:
WRITE A PROGRAM THAT INPUTS
A NUMBER AND FINDS WHETHER
IT IS EVEN OR ODD USING IF-ELSE
STRUCTURE.
#INCLUDE<IOSTREAM.H>
#INCLUDE <CONIO.H>
VOID MAIN()
{
CLRSCR();
INT N;
COUT<<’’ENTER A NUMBER:’’;
CIN>>N;
IF(N%2==0)
COUT<<N<<’’IS EVEN.’’;
else
COUT<<N<<’’IS
ODD.’’;
GETCH();
}
OUTPUT:
ENTER A NUMBER: 10
10 IS EVEN.
SWITCH
STATEMENT
THE SWITCH STATEMENT IS
ANOTHER CONDITIONAL
STRUCTURE. IT IS A GOOD
ALTERNATIVE OF NESTED IF-
ELSE.
IT CAN BE USED EASILY
WHEN THERE ARE MANY
CHOICES AVAILABLE AND
ONLY ONE SHOULD BE
EXECUTED.
SYNTAX:
SWITCH(EXPRESSION)
{
(INTEGER OR CHARACTER
CONSTANT)
CASE CONSTANT 1:
STATEMENT(S);
BREAK;
CASE CONSTANT 2:
STATEMENT(S);
BREAK;
.
.
.
CASE CONSTANT N:
STATEMENT(S);
BREAK;
WRITE A PROGRAM THAT
INPUTS NUMBER OF
WEEK’S DAY AND
DISPLAY THE NAME OF
THE DAY.
#INCLUDE<IOSTREAM.H>
#INCLUDE<CONIO.H>
VOID MAIN()
{
CLRSCR ();
INT N;
COUT << “ENTER A NUMBER OF A
WEEKDAY.”;
CIN>>N;
SWITCH (N)
{
CASE 1:
COUT << “FRIDAY”;
BREAK;
CASE 1:
COUT << “FRIDAY”;
BREAK;
CASE 2:
COUT << “SATURDAY”;
BREAK;
CASE 3:
COUT << “SUNDAY”;
BREAK;
CASE 4:
COUT<< “MONDAY”;
BREAK;
CASE 5:
COUT<< “TUESDAY”;
BREAK;
CASE 6:
COUT<<“WEDNESDAY”;
BREAK;
CASE 7:
COUT<<
“THURSDAY”;
BREAK;
DEFAULT:
COUT<< “INVALID
NUMBER”;
}
GETCH ();
}
GOTO STATEMENT
THE GOTO STATEMENT IS
USED TO MOVE THE
CONTROL DIRECTLY TO A
PARTICULAR LOCATION OF
THE PROGRAM BY USING
LABEL. A LABEL IS A NAME
GIVEN TO A PARTICULAR
LINE OF THE PROGRAM.
A LABEL IS CREATED WITH A VALID
IDENTIFIER FOLLOWED BY A
COLON (:)
SYNTAX:
GOTO LABEL;
THE “LABEL” INDICATES THE
LABEL TO WHICH THE
CONTROL IS TRANSFERRED.
EXAMPLE:
WRITE A PROGRAM THAT
DISPLAYS “C++” FIVE TIMES
USING GOTO STATEMENT.
#INCLUDE<IOSTREAM.H>
#INCLUDE<CONIO.H>
VOID MAIN()
{
CLRSCR();
INT N=1;
LOOP:
COUT<<N<<“C++”<<ENDL;
N++;
IF(N<=5) GOTO LOOP;
COUT<<“END OF PROGRAM”;
GETCH();
}
Conditional statement c++

More Related Content

What's hot

Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
Kumar
 

What's hot (20)

C++ string
C++ stringC++ string
C++ string
 
Conditional Statement in C Language
Conditional Statement in C LanguageConditional Statement in C Language
Conditional Statement in C Language
 
Data types in c++
Data types in c++Data types in c++
Data types in c++
 
Type conversion
Type conversionType conversion
Type conversion
 
CONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGECONDITIONAL STATEMENT IN C LANGUAGE
CONDITIONAL STATEMENT IN C LANGUAGE
 
Files and streams
Files and streamsFiles and streams
Files and streams
 
C by balaguruswami - e.balagurusamy
C   by balaguruswami - e.balagurusamyC   by balaguruswami - e.balagurusamy
C by balaguruswami - e.balagurusamy
 
What are variables and keywords in c++
What are variables and keywords in c++What are variables and keywords in c++
What are variables and keywords in c++
 
Loops c++
Loops c++Loops c++
Loops c++
 
c++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ programc++ programming Unit 2 basic structure of a c++ program
c++ programming Unit 2 basic structure of a c++ program
 
Presentation on C++ Programming Language
Presentation on C++ Programming LanguagePresentation on C++ Programming Language
Presentation on C++ Programming Language
 
Operators in C++
Operators in C++Operators in C++
Operators in C++
 
C++ presentation
C++ presentationC++ presentation
C++ presentation
 
Managing input and output operation in c
Managing input and output operation in cManaging input and output operation in c
Managing input and output operation in c
 
Files in c++ ppt
Files in c++ pptFiles in c++ ppt
Files in c++ ppt
 
Introduction to c++ ppt 1
Introduction to c++ ppt 1Introduction to c++ ppt 1
Introduction to c++ ppt 1
 
Introduction to cpp
Introduction to cppIntroduction to cpp
Introduction to cpp
 
Functions in c language
Functions in c language Functions in c language
Functions in c language
 
Operator Overloading
Operator OverloadingOperator Overloading
Operator Overloading
 
C++ How to program
C++ How to programC++ How to program
C++ How to program
 

Similar to Conditional statement c++

FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
rohassanie
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
mounikanarra3
 

Similar to Conditional statement c++ (20)

Programming (if else)
Programming (if else)Programming (if else)
Programming (if else)
 
C Control Statements.docx
C Control Statements.docxC Control Statements.docx
C Control Statements.docx
 
Flow of control ppt
Flow of control pptFlow of control ppt
Flow of control ppt
 
CONTROL STMTS.pptx
CONTROL STMTS.pptxCONTROL STMTS.pptx
CONTROL STMTS.pptx
 
Basics of Control Statement in C Languages
Basics of Control Statement in C LanguagesBasics of Control Statement in C Languages
Basics of Control Statement in C Languages
 
Flow of control C ++ By TANUJ
Flow of control C ++ By TANUJFlow of control C ++ By TANUJ
Flow of control C ++ By TANUJ
 
3 chapter three - part 1.pdf
3 chapter three - part 1.pdf3 chapter three - part 1.pdf
3 chapter three - part 1.pdf
 
Flow of Control
Flow of ControlFlow of Control
Flow of Control
 
Control Statement programming
Control Statement programmingControl Statement programming
Control Statement programming
 
Chapter 3
Chapter 3Chapter 3
Chapter 3
 
Decision Making and Branching in C
Decision Making and Branching  in CDecision Making and Branching  in C
Decision Making and Branching in C
 
CHAPTER-3a.ppt
CHAPTER-3a.pptCHAPTER-3a.ppt
CHAPTER-3a.ppt
 
CH-4 (1).pptx
CH-4 (1).pptxCH-4 (1).pptx
CH-4 (1).pptx
 
FP 201 Unit 3
FP 201 Unit 3 FP 201 Unit 3
FP 201 Unit 3
 
Btech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statementsBtech i pic u-3 handling input output and control statements
Btech i pic u-3 handling input output and control statements
 
handling input output and control statements
 handling input output and control statements handling input output and control statements
handling input output and control statements
 
exp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdfexp227-jan-170127160848 (3) (1).pdf
exp227-jan-170127160848 (3) (1).pdf
 
Mca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statementsMca i pic u-3 handling input output and control statements
Mca i pic u-3 handling input output and control statements
 
C++ lecture 02
C++   lecture 02C++   lecture 02
C++ lecture 02
 
Bsc cs pic u-3 handling input output and control statements
Bsc cs  pic u-3 handling input output and control statementsBsc cs  pic u-3 handling input output and control statements
Bsc cs pic u-3 handling input output and control statements
 

Recently uploaded

The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
heathfieldcps1
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
AnaAcapella
 

Recently uploaded (20)

Play hard learn harder: The Serious Business of Play
Play hard learn harder:  The Serious Business of PlayPlay hard learn harder:  The Serious Business of Play
Play hard learn harder: The Serious Business of Play
 
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptxExploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
Exploring_the_Narrative_Style_of_Amitav_Ghoshs_Gun_Island.pptx
 
Economic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food AdditivesEconomic Importance Of Fungi In Food Additives
Economic Importance Of Fungi In Food Additives
 
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdfUnit 3 Emotional Intelligence and Spiritual Intelligence.pdf
Unit 3 Emotional Intelligence and Spiritual Intelligence.pdf
 
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptxOn_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
On_Translating_a_Tamil_Poem_by_A_K_Ramanujan.pptx
 
OSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & SystemsOSCM Unit 2_Operations Processes & Systems
OSCM Unit 2_Operations Processes & Systems
 
PANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptxPANDITA RAMABAI- Indian political thought GENDER.pptx
PANDITA RAMABAI- Indian political thought GENDER.pptx
 
The basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptxThe basics of sentences session 3pptx.pptx
The basics of sentences session 3pptx.pptx
 
How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17How to Add a Tool Tip to a Field in Odoo 17
How to Add a Tool Tip to a Field in Odoo 17
 
Towards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptxTowards a code of practice for AI in AT.pptx
Towards a code of practice for AI in AT.pptx
 
Interdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptxInterdisciplinary_Insights_Data_Collection_Methods.pptx
Interdisciplinary_Insights_Data_Collection_Methods.pptx
 
Tatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf artsTatlong Kwento ni Lola basyang-1.pdf arts
Tatlong Kwento ni Lola basyang-1.pdf arts
 
VAMOS CUIDAR DO NOSSO PLANETA! .
VAMOS CUIDAR DO NOSSO PLANETA!                    .VAMOS CUIDAR DO NOSSO PLANETA!                    .
VAMOS CUIDAR DO NOSSO PLANETA! .
 
dusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learningdusjagr & nano talk on open tools for agriculture research and learning
dusjagr & nano talk on open tools for agriculture research and learning
 
What is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptxWhat is 3 Way Matching Process in Odoo 17.pptx
What is 3 Way Matching Process in Odoo 17.pptx
 
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPSSpellings Wk 4 and Wk 5 for Grade 4 at CAPS
Spellings Wk 4 and Wk 5 for Grade 4 at CAPS
 
21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx21st_Century_Skills_Framework_Final_Presentation_2.pptx
21st_Century_Skills_Framework_Final_Presentation_2.pptx
 
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdfUGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
UGC NET Paper 1 Unit 7 DATA INTERPRETATION.pdf
 
Our Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdfOur Environment Class 10 Science Notes pdf
Our Environment Class 10 Science Notes pdf
 
How to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POSHow to Manage Global Discount in Odoo 17 POS
How to Manage Global Discount in Odoo 17 POS
 

Conditional statement c++