0% found this document useful (0 votes)
32 views

Lecture - Notes - II - B Operators

The document discusses conditional operators, switch statements, and repetition constructs in C++. The conditional operator (? :) is a shorthand for if-else statements and allows executing one of two expressions based on whether a condition is true or false. Switch statements can be used instead of multiple if-else statements when there are more than two alternatives. A variable is compared to case constants and the statements for the matching case are executed. Repetition constructs like while, do-while, and for loops repeatedly execute a block of code until a condition is met. While and do-while check the condition at the end of each iteration, whereas for allows initializing and updating a loop counter.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Lecture - Notes - II - B Operators

The document discusses conditional operators, switch statements, and repetition constructs in C++. The conditional operator (? :) is a shorthand for if-else statements and allows executing one of two expressions based on whether a condition is true or false. Switch statements can be used instead of multiple if-else statements when there are more than two alternatives. A variable is compared to case constants and the statements for the matching case are executed. Repetition constructs like while, do-while, and for loops repeatedly execute a block of code until a condition is met. While and do-while check the condition at the end of each iteration, whereas for allows initializing and updating a loop counter.
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 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

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy