KEMBAR78
IF & SWITCH (conditional control) in computer science | PPTX
Slide Set –
13 CP
Conditional Control
Flow
• Conditional control flow is also referred to as conditional logicor
selection logic.
• It is one of the order in which the program instructions are executed.
• Its execution order differs from the sequential logic.
• It executes the instructions on the basis of one or more conditions.
• If the conditions are satisfied it will execute the instructions else
the instructions will not be executed (skipped).
Conditional Control
Flow
In conditional logic:
• Statements are executed on the basisof conditions.
• If conditions are satisfied the statements are executed.
• If conditions are not satisfied the statements are
skipped.
Conditional Control Structures in
C++
• Conditional control structures are used to execute set of statements on
the basis of one or more conditions.
• The statements that are needed to be executed conditionally are placed
with in the conditional structures and one or more conditions are
specified.
• The conditional control structures implement the
conditional/selection logic in C++.
if Statement in
C++
• if statement implements one-way selection logic.
• It executes the statement(s) on the basis of one
condition.
• If the condition is true, it executes if block.
• If the condition is false, it does not execute if block.
if Statement –
Syntax
if ( condition )
{
statement set
;
}
if ( condition
)
statement
;
if Statement –
Syntax
if Statement –
Syntax
• There are two syntaxes of if statement.
• In first syntax we have multiple statements inside the if statement.
• In this case it is compulsory to enclose all the statements in the braces
{ }
.
• In second syntax we have just one statement inside the if statement.
• In this case it is optional to enclose the statement in thebraces { } .
• All the statements enclosed in {} is called as the block.
if Statement – Flow
Chart
if Statement -
Example
Problem Statement 1: If the number is positive thendisplay
it.
if Statement -
Example
Problem Statement 2: If the number is multiple of 5 then add 1 to it
and display the resultant number.
If-else Statement in
C++
• If-else statementimplements two-way selection logic.
• It executes the statement(s) on the basis of one
condition.
• If the condition is true, it executes if block.
• If the condition is false, itexecutes else block.
If-else Statement –
Syntax
if ( condition )
{
statement set 1
;
}
else
{
statement set 2
;
if ( condition )
statement set 1
;
else
statement set 2
;
If-else Statement –
Syntax
If-else Statement –
Syntax
If-else Statement –
Syntax
• There are two syntaxes of if-else statement.
• In first syntax we have multiple statements inside the if-else statement.
• In this case it is compulsory to enclose all the statements in the braces
{ }
.
• In second syntax we have just one statement inside the if-else
statement.
• In this case it is optional to enclose the statement in thebraces { } .
If-else Statement – Flow
Chart
if-else Statement -
Example
Problem Statement 1: If the number is even, display “Number is even”
else display “Number is odd”.
switch Statement in
C++
• switch implements choice-way selection logic.
• It executes the statement(s) on the basis of expression and available choices.
• It first evaluates the expression and matches the value of expression to
available choices (cases).
• If the value matches with first case, then case 1 body is executed.
• If the value matches with second case, then case 2 body is executed.
• And the process continues up to nth case.
• If the value does not matches with any of the case, then default case
body is executed.
switch Statement –
Syntax
switch ( expression )
{
case 1 :
statement set 1
;
break ;
case 2 :
statement set 2
;
break ;
case 3 :
statement set 3
;
break ;
default :
switch Statement –
Syntax
switch Statement –
Syntax
• In switch statement, first the expression is evaluated to a single value.
• In expression we can only use the value of data types: char, short,int
and
long. No any other data types is accepted.
• We cannot use relational operation in switch statement.
• switch statement always contain one expression and multiple cases.
Where each case is ended by break statement.
• Last case is the default case (it is optional), it is executed when the
evaluated value does not match with any of thecase.
switch Statement – Flow
Chart
switch Statement -
Example
Problem Statement: Give remarks to the student according to the
marks he/she gets out of 5 in the sessionaltest.

IF & SWITCH (conditional control) in computer science

  • 1.
  • 2.
    Conditional Control Flow • Conditionalcontrol flow is also referred to as conditional logicor selection logic. • It is one of the order in which the program instructions are executed. • Its execution order differs from the sequential logic. • It executes the instructions on the basis of one or more conditions. • If the conditions are satisfied it will execute the instructions else the instructions will not be executed (skipped).
  • 3.
    Conditional Control Flow In conditionallogic: • Statements are executed on the basisof conditions. • If conditions are satisfied the statements are executed. • If conditions are not satisfied the statements are skipped.
  • 4.
    Conditional Control Structuresin C++ • Conditional control structures are used to execute set of statements on the basis of one or more conditions. • The statements that are needed to be executed conditionally are placed with in the conditional structures and one or more conditions are specified. • The conditional control structures implement the conditional/selection logic in C++.
  • 5.
    if Statement in C++ •if statement implements one-way selection logic. • It executes the statement(s) on the basis of one condition. • If the condition is true, it executes if block. • If the condition is false, it does not execute if block.
  • 6.
    if Statement – Syntax if( condition ) { statement set ; } if ( condition ) statement ;
  • 7.
  • 8.
    if Statement – Syntax •There are two syntaxes of if statement. • In first syntax we have multiple statements inside the if statement. • In this case it is compulsory to enclose all the statements in the braces { } . • In second syntax we have just one statement inside the if statement. • In this case it is optional to enclose the statement in thebraces { } . • All the statements enclosed in {} is called as the block.
  • 9.
    if Statement –Flow Chart
  • 10.
    if Statement - Example ProblemStatement 1: If the number is positive thendisplay it.
  • 11.
    if Statement - Example ProblemStatement 2: If the number is multiple of 5 then add 1 to it and display the resultant number.
  • 12.
    If-else Statement in C++ •If-else statementimplements two-way selection logic. • It executes the statement(s) on the basis of one condition. • If the condition is true, it executes if block. • If the condition is false, itexecutes else block.
  • 13.
    If-else Statement – Syntax if( condition ) { statement set 1 ; } else { statement set 2 ; if ( condition ) statement set 1 ; else statement set 2 ;
  • 14.
  • 15.
  • 16.
    If-else Statement – Syntax •There are two syntaxes of if-else statement. • In first syntax we have multiple statements inside the if-else statement. • In this case it is compulsory to enclose all the statements in the braces { } . • In second syntax we have just one statement inside the if-else statement. • In this case it is optional to enclose the statement in thebraces { } .
  • 17.
  • 18.
    if-else Statement - Example ProblemStatement 1: If the number is even, display “Number is even” else display “Number is odd”.
  • 19.
    switch Statement in C++ •switch implements choice-way selection logic. • It executes the statement(s) on the basis of expression and available choices. • It first evaluates the expression and matches the value of expression to available choices (cases). • If the value matches with first case, then case 1 body is executed. • If the value matches with second case, then case 2 body is executed. • And the process continues up to nth case. • If the value does not matches with any of the case, then default case body is executed.
  • 20.
    switch Statement – Syntax switch( expression ) { case 1 : statement set 1 ; break ; case 2 : statement set 2 ; break ; case 3 : statement set 3 ; break ; default :
  • 21.
  • 22.
    switch Statement – Syntax •In switch statement, first the expression is evaluated to a single value. • In expression we can only use the value of data types: char, short,int and long. No any other data types is accepted. • We cannot use relational operation in switch statement. • switch statement always contain one expression and multiple cases. Where each case is ended by break statement. • Last case is the default case (it is optional), it is executed when the evaluated value does not match with any of thecase.
  • 23.
  • 24.
    switch Statement - Example ProblemStatement: Give remarks to the student according to the marks he/she gets out of 5 in the sessionaltest.