C++ Course Notes _ Fundamentals of Programming 1 - Week 2
C++ Course Notes _ Fundamentals of Programming 1 - Week 2
Topics to be covered:
1. If statement
2. If-else statement
3. Nested if-else statement
4. Conditional operators
5. Switch statement
6. Introduction to Iterative statements/Loops
7. The for loop
8. The while loop
9. The do-while loop
If statement
1. An if statement is the most common conditional statement. It
executes when the condition being checked evaluates to true.
2. Syntax:
1
if(condition) {
2
//statements
3
}
3. Example:
1
if(x > 20) {
2
cout << "Value of x is greater than 20\n";
3
}
Output:
CASE 1: Let value of x be 30
Value of x is greater than 20
CASE 2: Let value of x be 10
No output in this case
If-else statement
1. An if-else statement is designed to give us this functionality in our
code. It executes statements if some condition is true or false. If the
condition is true, the if part is executed, otherwise the else part of
the statement is executed.
2. Syntax:
1
if(condition) {
2
//statements
3
} else {
4
// statements
5
}
3. Example:
1
if(x > 20) {
2
cout << "Value of x is greater than 20\n";
3
} else {
4
cout << "Value of x is less than 20"
5
}
Output:
CASE 1: Let value of x be 30
Value of x is greater than 20
CASE 2: Let value of x be 10
Value of x is less than 20
Nested if-else
1. It is simply an if-else statement inside another if-else statement.
2. Syntax:
1
if (condition - 1)
2
{
3
if (condition - 2)
4
{
5
//statements
6
}
7
else
8
{
9
//statements
10
}
11
}
12
else
13
{
14
//statements
15
}
16
3. Example:
1
cout << "Value of x is "
2
if (x > 20)
3
{
4
if(x > 100)
5
{
6
cout << “very much”;
7
}
8
cout << “greater\n”;
9
}
10
else
11
{
12
cout << “smaller\n”;
13
}
14
Output:
CASE 1: Let value of x be 150
Value of x is very much greater than 20
CASE 2: Let value of x be 50
Value of x is greater than 20
CASE 3: Let value of x be 10
Value of x is less than 20
Conditional operators
1. These operators are used when a condition comprises of more than
one Boolean expression/ condition check.
2. We have following types of conditional operators - logical-and,
logical-or and ternary operator.
1. Logical-and operator (&&): It is used when we want the condition
to be true if both the expressions are true.
Syntax:
1
if(condition - 1 && condition - 2)
2
{
3
// statements
4
}
Example:
1
if (val >= 10 && val <= 20)
2
{
3
cout << "Value is in range 10 to 20."
4
}
5
Case - 1: val = 3
Output: No output
Case - 2: val = 11
Output: Value is in range 10 to 20.
Case - 3: val = 40
Output: No output
2. Logical-or operator (||) : This operator is used when any one of
the Boolean expressions is evaluated as true.
Syntax:
1
if(condition - 1 || condition - 2)
2
{
3
statement;
4
}
5
Example:
1
if (val < 10 || val > 20)
2
{
3
cout << "Value is either greater than 20 or less than
4
}
❤2 Add comment
Case - 1: val = 3
Output: Value is either greater than 20 or less than
10.
Case - 2: val = 40
Output: Value is either greater than 20 or less than
10.
Case - 3: val = 11
Output: No output
3. Ternary operator (?:) : It is a smaller version for the if-else
statement. If the condition is true then statement - 1 is executed
else the statement - 2 is executed.
Syntax:
1
condition ? statement - 1 : statement - 2;
Example:
1
//Without ternary operator
2
3
if (val == 10)
4
{
5
cout << "The current value is 10\n";
6
}
7
else
8
{
9
cout << "The current value is not 10\n";
10
}
11
12
13
14
//With ternary operator
15
16
val == 10 ? cout << "The current value is 10\n" : cout <<
17
❤1 Add comment
Case - 1: val = 10
Output: The current value is 10
Case - 2: val = 20
Output: The current value is not 10
Switch statement
1. Switch Statement is like an if-else ladder with multiple conditions,
where we check for equality of a variable with different values.
2. Syntax:
1
switch (expression)
2
{
3
case x:
4
// code
5
break;
6
case y:
7
// code
8
break;
9
.
10
.
11
.
12
default:
13
// code
14
}
15
3. Example:
1
switch (ch) {
2
case ‘a’:
3
cout << “Vowel\n”;
4
break;
5
case ‘e’:
6
cout << “Vowel\n”;
7
break;
8
case ‘i’:
9
cout << “Vowel\n”;
10
break;
11
case ‘o’:
12
cout << “Vowel\n”;
13
break;
14
case ‘u’:
15
cout << “Vowel\n”;
16
break;
17
default:
18
cout << “Consonant\n”;
19
}
Output:
Case - 1: ch = ‘a’
Output - Vowel
Case - 2: ch = ‘x’
Output - Consonant
3. Example:
1
for(int i = 1; i < 6; i++ ){
2
cout << i << " ";
3
}
Output:
1 2 3 4 5
1
while (condition)
2
statement;
3
3. Example:
1
int i = 1;
2
while (i <= 5)
3
{
4
cout << i << “ “;
5
i = i + 1;
6
}
Output:
1 2 3 4 5
3. Example:
1
int idx = 1;
2
do
3
{
4
cout << idx << “ “;
5
idx++;
6
} while (idx <= 5);
7
Output:
1 2 3 4 5