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!

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

  • 1.
    Algorithms and programming basics •Boolean operations • Control structures • if..else • switch..case
  • 2.
    Binary logic • boolvariable • 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 relationaloperators 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 • Usedas 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 • Sequentialexecution ▫ 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”
  • 15.
  • 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 • Usedfor 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 nestedif else Switch structure is the same as nested if/else structure that uses only equality comparison
  • 20.
  • 21.