You are on page 1of 21

Conditional operator

• short-hand for if-else


syntax

condition ? expression1 : expression2 ;

(? :) is a ternary operator  three operands

- if condition is true then expression1 is executed.

- if condition is false then expression2 is executed.


example
a program to check if the entered number is even or odd.

int main() {

int number ;

cout << "enter number: ";

cin >> number ;

(number%2==0)?cout<<"number is even":cout<<
"number is odd";

return 0;

}
switch
• used instead of if-else when there are more than two
alternatives/conditions available.

• a variable/expression is tested for equality against a list of


cases.

syntax
switch(variable or integer expression) {
case constant1:
block of statements;
break;
case constant2:
block of statements;
break;
... .

default:
block of statements; }
execution flow

─ switch expression is evaluated and a match is searched from


the case constants.

─ if a match is found, statements for that case are executed.

─ if a match is not found, the default statement is executed.

─ execution is terminated when a break statement is encountered


or when the default is reached.
flowchart of switch statement

https://www.programiz.com/cpp-
programming/switch-case
example
int main(){
char vowel;
cout << "enter a vowel: ";
cin >> vowel;
switch(vowel) {
case 'a': cout << “first vowel is a";
break;
case 'e': cout << “first vowel is e";
break;
case 'i': cout << “first vowel is i";
break;
case 'o': cout << “first vowel is o";
break;
case 'u': cout << “first vowel is u";
break;
default : cout << "no vowel was entered";
}
return 0; }
Repetition
• Iteration / Looping

• is used when a block of statements is to be executed


repeatedly until a condition is satisfied.

• loops are:

─ unconditional / fixed : is repeated a set number of


times

─ conditional : looping continues until some condition is


true
Repetition constructs
while statement

do … while statement

for statement
while

syntax

while (test expression)


{
block of statements;
}
execution flow
• test expression is evaluated.

• if expression is false then loop is skipped.

• if expression is true then execute the loop block.

• repeat: evaluate test expression ...


example
a program to find the sum of the digits in base 10.

int main()
{
int n, sum=0;

while(n < 10)


{
sum+=n;
n++;
}
cout << “sum is” << sum;

return 0;
}
do … while

• a variant of while

syntax

do
{
Block of statements;
}
while (test expression)
execution flow
• execute block of statements first.

• then evaluate test expression.

• if expression is true: repeat .

• if expression is false exit loop.


example
a program to find the sum of the digits in base 10.

int main()
{
int n,sum=0;
do {
sum += n;
n++;
} while(n < 10);

cout << “sum is ” << sum;

return 0;
}
for

syntax

for (initialisation expression ; test


condition ; increment/decrement
expression)
{
block of statements;
}
execution flow
• initialisation expression is evaluated.

• test condition is evaluated.

• if condition is true the loop block is executed.

• evaluate the increment/decrement expression.

• test condition is evaluated, if still true execute loop block.

• if condition is false exit loop.


example 1
a program to find the sum of the digits in base 10.

int main()
{
int n, sum=0;

for (n = 0 ; n < 10 ; n++)


{
sum += n;
}

cout << “sum is ” << sum;

return 0;
}
example 2
a program to find the sum of natural numbers

#include <iostream>
using namespace std;
int main() {
int n, sum = 0;
cout << “enter a positive integer: ";
cin >> n;
for (int i = 1; i <= n; ++i) {
sum += i;
}
cout << "Sum = " << sum;
return 0;
}

You might also like