SlideShare una empresa de Scribd logo
1 de 21
Algorithms and programming
basics
• Boolean operations
• Control structures
• if..else
• switch..case
Binary logic
• bool variable
• 1/0 – variable keep one of two variants
• It interpreter like true or false
• Binary logic is a statements, that gives one
result – true or false (0 or 1)
• 14>2 = 1(true)
• Example:
• int x = 12;
• x == 12
• Output: 1
Equality and relational operators
equality operator
or
relational operator
C++ equality
or rela tional
Operator
Example
of C++
condition
Meaning of
C++ condition
Relational operators
> > x > y x is greater than y
< < x < y x is less than y
 >= x >= y xis greater than or equal to y
 <= x <= y x is less than or equal to y
Equality operators
= == x == y x is equal to y
 != x != y x is not equal to y
Logical Operators
• Used as conditions in loops, if statements
• && (logical AND)
▫ true if both conditions are true
if ( gender == 1 && age >= 65 )
++seniorFemales;
• || (logical OR)
▫ true if either of condition is true
if ( semesterAverage>= 90|| finalExam >= 90 )
cout << "Student grade is A" << endl;
Logical Operators
• ! (logical NOT, logical negation)
▫ Returns true when its condition is false, & vice
versa
if ( !( grade == sentinelValue ) )
cout << "The next grade is " << grade << endl;
Alternative:
if ( grade != sentinelValue )
cout << "The next grade is " << grade << endl;
Control Structures
• Sequential execution
▫ Statements executed in order
• Transfer of control
▫ Next statement executed not next one in
sequence
• 3 control structures
▫ Sequence structure
 Programs executed sequentially by default
▫ Selection structures
 if, if/else, switch
▫ Repetition structures
 while, do/while, for
if Selection Structure
• Selection structure
▫ Choose among alternative courses of action
▫ Pseudocode example:
▫ If the condition is true
 Print statement executed, program continues to
next statement
▫ If the condition is false
 Print statement ignored, program continues
▫ Indenting makes programs easier to read
 C++ ignores whitespace characters (tabs, spaces,
etc.)
if Selection Structure
•if (logical statement)
•Action1;
•Else
•Action2;
if Selection Structure
• Translation into C++
If student’s grade is greater than or equal to 60
Print “Passed”
if ( grade >= 60 )
cout << "Passed";
• Diamond symbol (decision symbol)
▫ Indicates decision is to be made
▫ Contains an expression that can be true or false
 Test condition, follow path
• if structure
▫ Single-entry/single-exit
if/else structure
• “if” structure
▫ Make decision based on truth or falsity of
condition
▫ If condition met – body is executed, else - bodyis
not executed
▫ If there is an “else” structure
 Then if condition is false
the body of “else” structure
is executed
if/else structure
if(x > 10){
cout << "x is more than ten";
y = x * 2;
}
else
cout << "x is smaller";
• If there is more than one statements or expressions
in “if/else” structure use curly brackets { }
if/else Selection Structure
• if
▫ Performs action if condition true
• if/else
▫ Different actions if conditions true or false
• Pseudocode
if student’s grade is greater than or equal to 60
print “Passed”
else
print “Failed”
• C++ code
if ( grade >= 50 )
cout << "Passed";
else
cout << "Failed";
if/else Selection Structure
• Ternary conditional operator (?:)
▫ Three arguments (condition, value if true,
value if false)
• Code could be written:
cout << ( grade >= 60 ? “Passed” : “Failed” );
true
false
print “Failed” print “Passed”
grade >= 60
Condition Value if true Value if false
if/else Selection Structure
• Nested if/else structures
▫ One inside another, test for multiple cases
▫ Once condition met, other statements skipped
if student’s grade is greater than or equal to 90
Print “A”
else
if student’s grade is greater than or equal to 80
Print “B”
else
if student’s grade is greater than or equal to 70
Print “C”
else
if student’s grade is greater than or equal to 60
Print “D”
else
Print “F”
if/else Selection Structure
if/else Selection Structure
• if (logical statement){
• Action1;
• Action2;
• Action3;
• }
• Else
• Action4;
• if (logical statement){
• Action1;
• Action2;
• }
• Else{
• Action3;
• Action4;
• }
if/else Selection Structure
• Compound statement
▫ Set of statements within a pair of braces
if ( grade >= 50 )
cout << "Passed.n";
else {
cout << "Failed.n";
cout << "You must take this course again.n";
}
▫ Without braces,
cout << "You must take this course again.n";
always executed
• Block
▫ Set of statements within braces
Switch structure
• Used for testing variable for multiple values
• For each value the case keyword is used
• If no cases matched then optional default is used
• switch(){
▫ case value1: statements; break;
▫ case value2: statements; break;
▫ ……
▫ default: statements; break;
• }
Switch and nested if else
Switch structure is the same as nested if/else structure
that uses only equality comparison
Switch diagram
52
Thanks for
your
attention!

Más contenido relacionado

Similar a Control Structures, If..else, switch..case.pptx

Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
PRN USM
 
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
ronistgdr
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
Karwan Mustafa Kareem
 

Similar a Control Structures, If..else, switch..case.pptx (20)

Control structures in C++ Programming Language
Control structures in C++ Programming LanguageControl structures in C++ Programming Language
Control structures in C++ Programming Language
 
Pi j1.3 operators
Pi j1.3 operatorsPi j1.3 operators
Pi j1.3 operators
 
Selection Control Structures
Selection Control StructuresSelection Control Structures
Selection Control Structures
 
Selection
SelectionSelection
Selection
 
Control structures in C
Control structures in CControl structures in C
Control structures in C
 
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
Metode Perancangan Program #2 Selection and Repetition Control Structure-ITSB...
 
Control Structures.pptx
Control Structures.pptxControl Structures.pptx
Control Structures.pptx
 
Ch3.1
Ch3.1Ch3.1
Ch3.1
 
Using decision statements
Using decision statementsUsing decision statements
Using decision statements
 
Java Programmin: Selections
Java Programmin: SelectionsJava Programmin: Selections
Java Programmin: Selections
 
Control statements anil
Control statements anilControl statements anil
Control statements anil
 
Lecture#3 Algorithms and computing
Lecture#3 Algorithms and computingLecture#3 Algorithms and computing
Lecture#3 Algorithms and computing
 
CSC111-Chap_03.pdf
CSC111-Chap_03.pdfCSC111-Chap_03.pdf
CSC111-Chap_03.pdf
 
Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)Understand Decision structures in c++ (cplusplus)
Understand Decision structures in c++ (cplusplus)
 
C++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPEC++ IF STATMENT AND ITS TYPE
C++ IF STATMENT AND ITS TYPE
 
Decision Control Structure If & Else
Decision Control Structure If & ElseDecision Control Structure If & Else
Decision Control Structure If & Else
 
Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1Intro To C++ - Class 09 - Control Statements: Part 1
Intro To C++ - Class 09 - Control Statements: Part 1
 
conditional_statement.pdf
conditional_statement.pdfconditional_statement.pdf
conditional_statement.pdf
 
Object oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statementsObject oriented programming16 boolean expressions and selection statements
Object oriented programming16 boolean expressions and selection statements
 
Chapter 1 nested control structures
Chapter 1 nested control structuresChapter 1 nested control structures
Chapter 1 nested control structures
 

Último

Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
FIDO Alliance
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
UK Journal
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
FIDO Alliance
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
UXDXConf
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
FIDO Alliance
 

Último (20)

Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
Human Expert Website Manual WCAG 2.0 2.1 2.2 Audit - Digital Accessibility Au...
 
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdfHow Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
How Red Hat Uses FDO in Device Lifecycle _ Costin and Vitaliy at Red Hat.pdf
 
Working together SRE & Platform Engineering
Working together SRE & Platform EngineeringWorking together SRE & Platform Engineering
Working together SRE & Platform Engineering
 
ERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage IntacctERP Contender Series: Acumatica vs. Sage Intacct
ERP Contender Series: Acumatica vs. Sage Intacct
 
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
Event-Driven Architecture Masterclass: Engineering a Robust, High-performance...
 
Using IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & IrelandUsing IESVE for Room Loads Analysis - UK & Ireland
Using IESVE for Room Loads Analysis - UK & Ireland
 
Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024Long journey of Ruby Standard library at RubyKaigi 2024
Long journey of Ruby Standard library at RubyKaigi 2024
 
Introduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptxIntroduction to FIDO Authentication and Passkeys.pptx
Introduction to FIDO Authentication and Passkeys.pptx
 
WebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM PerformanceWebAssembly is Key to Better LLM Performance
WebAssembly is Key to Better LLM Performance
 
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdfThe Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
The Value of Certifying Products for FDO _ Paul at FIDO Alliance.pdf
 
Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024Extensible Python: Robustness through Addition - PyCon 2024
Extensible Python: Robustness through Addition - PyCon 2024
 
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
Choosing the Right FDO Deployment Model for Your Application _ Geoffrey at In...
 
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptxHarnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
Harnessing Passkeys in the Battle Against AI-Powered Cyber Threats.pptx
 
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties ReimaginedEasier, Faster, and More Powerful – Notes Document Properties Reimagined
Easier, Faster, and More Powerful – Notes Document Properties Reimagined
 
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdfBreaking Down the Flutterwave Scandal What You Need to Know.pdf
Breaking Down the Flutterwave Scandal What You Need to Know.pdf
 
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...Hyatt driving innovation and exceptional customer experiences with FIDO passw...
Hyatt driving innovation and exceptional customer experiences with FIDO passw...
 
Structuring Teams and Portfolios for Success
Structuring Teams and Portfolios for SuccessStructuring Teams and Portfolios for Success
Structuring Teams and Portfolios for Success
 
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider  Progress from Awareness to Implementation.pptxTales from a Passkey Provider  Progress from Awareness to Implementation.pptx
Tales from a Passkey Provider Progress from Awareness to Implementation.pptx
 
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdfIntroduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
Introduction to FDO and How It works Applications _ Richard at FIDO Alliance.pdf
 
Event-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream ProcessingEvent-Driven Architecture Masterclass: Challenges in Stream Processing
Event-Driven Architecture Masterclass: Challenges in Stream Processing
 

Control Structures, If..else, switch..case.pptx

  • 1. Algorithms and programming basics • Boolean operations • Control structures • if..else • switch..case
  • 2. Binary logic • bool variable • 1/0 – variable keep one of two variants • It interpreter like true or false • Binary logic is a statements, that gives one result – true or false (0 or 1) • 14>2 = 1(true) • Example: • int x = 12; • x == 12 • Output: 1
  • 3. Equality and relational operators equality operator or relational operator C++ equality or rela tional Operator Example of C++ condition Meaning of C++ condition Relational operators > > x > y x is greater than y < < x < y x is less than y  >= x >= y xis greater than or equal to y  <= x <= y x is less than or equal to y Equality operators = == x == y x is equal to y  != x != y x is not equal to y
  • 4. Logical Operators • Used as conditions in loops, if statements • && (logical AND) ▫ true if both conditions are true if ( gender == 1 && age >= 65 ) ++seniorFemales; • || (logical OR) ▫ true if either of condition is true if ( semesterAverage>= 90|| finalExam >= 90 ) cout << "Student grade is A" << endl;
  • 5. Logical Operators • ! (logical NOT, logical negation) ▫ Returns true when its condition is false, & vice versa if ( !( grade == sentinelValue ) ) cout << "The next grade is " << grade << endl; Alternative: if ( grade != sentinelValue ) cout << "The next grade is " << grade << endl;
  • 6. Control Structures • Sequential execution ▫ Statements executed in order • Transfer of control ▫ Next statement executed not next one in sequence • 3 control structures ▫ Sequence structure  Programs executed sequentially by default ▫ Selection structures  if, if/else, switch ▫ Repetition structures  while, do/while, for
  • 7. if Selection Structure • Selection structure ▫ Choose among alternative courses of action ▫ Pseudocode example: ▫ If the condition is true  Print statement executed, program continues to next statement ▫ If the condition is false  Print statement ignored, program continues ▫ Indenting makes programs easier to read  C++ ignores whitespace characters (tabs, spaces, etc.)
  • 8. if Selection Structure •if (logical statement) •Action1; •Else •Action2;
  • 9. if Selection Structure • Translation into C++ If student’s grade is greater than or equal to 60 Print “Passed” if ( grade >= 60 ) cout << "Passed"; • Diamond symbol (decision symbol) ▫ Indicates decision is to be made ▫ Contains an expression that can be true or false  Test condition, follow path • if structure ▫ Single-entry/single-exit
  • 10. if/else structure • “if” structure ▫ Make decision based on truth or falsity of condition ▫ If condition met – body is executed, else - bodyis not executed ▫ If there is an “else” structure  Then if condition is false the body of “else” structure is executed
  • 11. if/else structure if(x > 10){ cout << "x is more than ten"; y = x * 2; } else cout << "x is smaller"; • If there is more than one statements or expressions in “if/else” structure use curly brackets { }
  • 12. if/else Selection Structure • if ▫ Performs action if condition true • if/else ▫ Different actions if conditions true or false • Pseudocode if student’s grade is greater than or equal to 60 print “Passed” else print “Failed” • C++ code if ( grade >= 50 ) cout << "Passed"; else cout << "Failed";
  • 13. if/else Selection Structure • Ternary conditional operator (?:) ▫ Three arguments (condition, value if true, value if false) • Code could be written: cout << ( grade >= 60 ? “Passed” : “Failed” ); true false print “Failed” print “Passed” grade >= 60 Condition Value if true Value if false
  • 14. if/else Selection Structure • Nested if/else structures ▫ One inside another, test for multiple cases ▫ Once condition met, other statements skipped if student’s grade is greater than or equal to 90 Print “A” else if student’s grade is greater than or equal to 80 Print “B” else if student’s grade is greater than or equal to 70 Print “C” else if student’s grade is greater than or equal to 60 Print “D” else Print “F”
  • 16. if/else Selection Structure • if (logical statement){ • Action1; • Action2; • Action3; • } • Else • Action4; • if (logical statement){ • Action1; • Action2; • } • Else{ • Action3; • Action4; • }
  • 17. if/else Selection Structure • Compound statement ▫ Set of statements within a pair of braces if ( grade >= 50 ) cout << "Passed.n"; else { cout << "Failed.n"; cout << "You must take this course again.n"; } ▫ Without braces, cout << "You must take this course again.n"; always executed • Block ▫ Set of statements within braces
  • 18. Switch structure • Used for testing variable for multiple values • For each value the case keyword is used • If no cases matched then optional default is used • switch(){ ▫ case value1: statements; break; ▫ case value2: statements; break; ▫ …… ▫ default: statements; break; • }
  • 19. Switch and nested if else Switch structure is the same as nested if/else structure that uses only equality comparison