0% found this document useful (0 votes)
25 views10 pages

Control Statements

Uploaded by

tilsamri83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
25 views10 pages

Control Statements

Uploaded by

tilsamri83
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PPTX, PDF, TXT or read online on Scribd
You are on page 1/ 10

Control Statements

Chapter 02
Contol Statements
Flow Control / Control flow: the order in which statements
are executed.
- It is typically sequential but may be diverted to other paths
by branch statement.
- C++ provides different control statements
1. Conditional Statement
2. Looping / Iteration statement
3. Other statements
Conditional Statements
1. If Statement
- execute statements depending on a condition being staisfied.
- syntax: if ( expression / condition )
statement;
This statement executes if and only if the
expression inside the parenthesis results
to true / condition becomes true

- the syntax above is used if you want to execute single statememt for
the condition.
- If you want to execute more than 1 statement when
condition/ expression is true, use the syntax below:
if (expression)
{
statement1; you surround your statements
with open and close curly
statement2; bracket {} (block statement
}
- If you want to execute statement if the condition is not true:
syntax: if (expression)
statement1; statement1 wil be executed if
the expression/condition is true
else
statement2;

statement2 wil be executed if


the expression/condition is false
If you are executing multiple statements:
syntax: if (expression)
{
statement1;
statement2;
}
else
{
statement3;
statement4;
}
- If statements can be nested ( if statements inside another
statement)
syntax: if (condition)
{
statement1;...
if (sondition)
{
statement2; ...
}
}
It is good practice to use curly bracket even if you execute one
statement.
2. Switch statement
- It provides a way of choosing between sets of alternative
based on expression.
syntax: switch ( expression)
{
case label1:
expression is evaluated and
statement;
outcome is compared to each
break;
labels as they appear in order until
case label2:
a match is found.
statement;
break; ...
default: Default case is executed if none of
statement; the case labels match the
break; expression.
}
If statement vs Switch Statement
- If-statement verifies relational expression as well as logical
expression.
- Switch Statement tests for equality

You might also like