Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
3 views

C++ Course Notes _ Fundamentals of Programming 1 - Week 2

The C++ Course Notes for Week 2 cover fundamental programming concepts including conditional statements (if, if-else, nested if-else, switch) and iterative statements (for, while, do-while loops). Each topic is explained with syntax and examples to illustrate their usage. The notes also introduce conditional operators such as logical-and, logical-or, and the ternary operator.

Uploaded by

Shahukar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
3 views

C++ Course Notes _ Fundamentals of Programming 1 - Week 2

The C++ Course Notes for Week 2 cover fundamental programming concepts including conditional statements (if, if-else, nested if-else, switch) and iterative statements (for, while, do-while loops). Each topic is explained with syntax and examples to illustrate their usage. The notes also introduce conditional operators such as logical-and, logical-or, and the ternary operator.

Uploaded by

Shahukar Khan
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 11

C++ Course Notes : Fundamentals of Programming 1 - Week 2

C++ Course Notes : Fundamentals of Programming 1


- Week 2
Rahil Sehgal
Created 2 years ago • Updated 2 years ago

Hello everyone welcome to the weekly lecture notes.

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

NOTE : “and” can also be written instead of “&&” and “or”


can also be written as “||”.

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

Introduction to Iterative statements/Loops


The simple dictionary meaning of loop is a structure, series, or process,
the end of which is connected to the beginning. We have the following
types of iterative statements -
1. The while loop
2. The for loop
3. The do-while loop

The For Loop


1. The advantage of a for loop is we know exactly how many times the
loop will execute even before the actual loop starts executing.
2. Syntax:
1
for (init-statement; condition; final-expression)
2
{
3
statement
4
// logic
5
}

1. Init-statement: This statement is used to initialize or assign a


starting value to a variable which may be altered over the course
of loop (we will see this while solving examples). It is
used/referred only once at the start of the loop.
2. Condition: This condition serves as a loop control statement. The
loop block is executed until the condition evaluates to true.
3. Final-expression: It is evaluated after each iteration of the loop. It
is generally to update the values of the loop variables.

3. Example:
1
for(int i = 1; i < 6; i++ ){
2
cout << i << " ";
3
}

Output:
1 2 3 4 5

The while loop


1. A while loop is a loop that runs through its body, known as a while
statement, as long as a predetermined condition is evaluated as true.
2. Syntax:

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

The do-while loop


1. Do-while loop tests the condition at the end of each execution for the
next iteration. In other words, the loop is executed at least once
before the condition is checked. Rest everything is the same as while
loop.
2. Syntax:
1
do
2
{
3
statement;
4
} while (condition);
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

You might also like