Fundamentals of Programming - 1
Fundamentals of Programming - 1
Topics to be covered:
If statement
If-else statement
Nested if-else statement
Conditional operators
Switch statement
Introduction to Iterative statements/Loops
The for loop
The while loop
The do-while loop
If statement
An if statement is the most common conditional statement. It executes when the condition being
checked evaluates to true.
Syntax:
if(condition) {
//statements
}
Example:
Output:
If-else statement
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.
Syntax:
if(condition) {
//statements
} else {
// statements
}
Example:
Output:
CASE 1: Let value of x be 30
Value of x is greater than 20
Nested if-else
It is simply an if-else statement inside another if-else statement.
Syntax:
if (condition - 1)
{
if (condition - 2)
{
//statements
}
else
{
//statements
}
}
else
{
//statements
}
Example:
Output:
Conditional operators
These operators are used when a condition comprises of more than one Boolean expression/ condition
check.
We have following types of conditional operators - logical-and, logical-or and ternary operator.
Logical-and operator (&&): It is used when we want the condition to be true if both the
expressions are true.
Syntax:
Example:
Case - 1: val = 3
Output: No output
Case - 2: val = 11
Case - 3: val = 40
Output: No output
Logical-or operator (||) : This operator is used when any one of the Boolean expressions is
evaluated as true.
Syntax:
if(condition - 1 || condition - 2)
{
statement;
}
Example:
Case - 1: val = 3
Case - 2: val = 40
Case - 3: val = 11
Output: No output
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:
Example:
//Without ternary operator
if (val == 10)
{
cout << "The current value is 10\n";
}
else
{
cout << "The current value is not 10\n";
}
val == 10 ? cout << "The current value is 10\n" : cout << "The current value is
not 10\n";
Case - 1: val = 10
Case - 2: val = 20
NOTE : “and” can also be written instead of “&&” and “or” can also be written as
“||”.
Switch statement
Switch Statement is like an if-else ladder with multiple conditions, where we check for equality of a
variable with different values.
Syntax:
switch (expression)
{
case x:
// code
break;
case y:
// code
break;
.
.
.
default:
// code
}
Example:
switch (ch) {
case ‘a’:
cout << “Vowel\n”;
break;
case ‘e’:
cout << “Vowel\n”;
break;
case ‘i’:
cout << “Vowel\n”;
break;
case ‘o’:
cout << “Vowel\n”;
break;
case ‘u’:
cout << “Vowel\n”;
break;
default:
cout << “Consonant\n”;
}
Output:
Case - 1: ch = ‘a’
Output - Vowel
Case - 2: ch = ‘x’
Output - Consonant
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.
Condition: This condition serves as a loop control statement. The loop block is executed until the
condition evaluates to true.
Final-expression: It is evaluated after each iteration of the loop. It is generally to update the
values of the loop variables.
Example:
Output:
1 2 3 4 5
Example:
int i = 1;
while (i <= 5)
{
cout << i << “ “;
i = i + 1;
}
Output:
1 2 3 4 5
do
{
statement;
} while (condition);
Example:
int idx = 1;
do
{
cout << idx << “ “;
idx++;
} while (idx <= 5);
Output:
1 2 3 4 5