Tutorial 12 - 1 - If For Do While

You might also like

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 21

Decision Making in C++

(if , if..else, Nested if, if-else-if , switch case)

In real life, there are times when we must make choices, and based on those
choices, we determine what to do next. Programming encounters similar scenarios
where we must make choices and then carry out the following block of code in
accordance with those choices. For instance, in C, if x happens, execute y; else,
execute z. There may be more than one condition, such as in C, where if condition
x is true, then execute p; if condition y is true, then execute q; if not, then execute
r. One of the various methods for importing several conditions is this C else-if
condition.

1.if statement
The simplest expression for making a choice is the if statement. It is used to
determine if a certain statement or block of statements will be performed or not,
i.e., whether a block of statements will be executed if a specific condition is true or
not.

Structure syntax:

if(condition)
{
// Statements to execute if
// condition is true
}
The outcome of this evaluation will be either true or false for the condition. The if
statement in C takes boolean values; if the value is true, the block of statements
below it will be executed; if not, they won't. Without the curly braces "{" and "}"
following if(condition), the if statement will automatically consider the first
sentence immediately after it to be inside its block.

Example:
2. If – else statement

A block of statements will be executed if a condition is true, and if it is false, they


won't be, according to the if statement alone. But what if the condition is false and
we want to take another action? The else statement is now being used. If the
condition is false, we may combine the else statement with the if statement to run a
block of code.

Structure syntax:

if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Example:

3. Nested-if statement

A nested if is an if statement that is the target of another if statement. Nested if


statements mean an if statement inside another if statement. Both C and C++ allow
us to nested if statements within if statements, i.e, we can place an if statement
inside another if statement.

Structure syntax:

if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
}
Example:
4. if-else-if ladder

An user can choose from a variety of alternatives here. The top-down execution of
the C++ if statements. When one of the conditions governing the if is satisfied, the
statement related to that if is performed instead of the other conditions in the C++
else-if ladder. The last else clause will be invoked if none of the requirements are
met.

Structure syntax:

if (condition)
statement;
else if (condition)
statement;
.
.
else
statement;
Example:

5. Jump Statements
a) break: This loop control statement is used to terminate the loop. As soon as
the break statement is encountered from within a loop, the loop iterations
stop there, and control returns from the loop immediately to the first
statement after the loop.
b) continues: This loop control statement is just like the break statement. The
continue statement is opposite to that of the break statement, instead of
terminating the loop, it forces to execute the next iteration of the loop.
As the name suggests the continue statement forces the loop to continue or execute
the next iteration. When the continue statement is executed in the loop, the code
inside the loop following the continue statement will be skipped and the next
iteration of the loop will begin.

c) return: The return in C or C++ returns the flow of the execution to the
function from where it is called. This statement does not mandatorily need
any conditional statements. As soon as the statement is executed, the flow of
the program stops immediately and return the control from where it was
called. The return statement may or may not return anything for a void
function, but for a non-void function, a return value is must be returned.

6. Switch statement
Switch statement consists of conditional based cases and a default case.

In a switch statement, the “case value” can be of “char” and “int” type.

Following are some of the rules while using the switch statement:
 There can be one or N numbers of cases.
 The values in the case must be unique.
 Each statement of the case can have a break statement. It is optional.

Structure syntax:

switch(expression)
{
case value1: statement_1; break;

case value2: statement_2; break;

.....
......
......
case value_n: statement_n; break;

default: default statement;

}
Example:
Exercises:

Problem 1: Given an integer N. Your task is to check if the integer is greater than,
less than or equal to 5.

If the integer is greater than 5, then print "Greater than 5" (without quotes).

If the integer is less than 5, then print "Less than 5".


If the integer is equal to 5, then print "Equal to 5".

Problem 2: Create a Calculator using the Switch statement (2 numbers are


received as user input). The calculator can do the simple operator: +, -, *, / and %
Loops in C++
(for, while, do while)

A loop is a set of instructions that are performed repeatedly in computer


programming until a specific condition is met.

An action is taken, such as receiving and changing a piece of data, and then a
condition, such as whether a counter has reached a predetermined number, is
verified.

Counter not reached: If the required number is not reached by the counter, the
following instruction in the sequence loops back to the first instruction and repeats
it.

Counter reached: If the condition has been met, the following instruction "falls
through" to the following sequential instruction or takes a different route outside of
the loop.

The two major types of loops are:

 Entry Controlled loops: In this kind of loop, the test condition is examined
before entering the loop body. Entry-controlled loops include the For Loop
and While Loop.
 Exit Controlled Loops: The test condition is tested or assessed at the
conclusion of the loop body in this type of loop. Therefore, whether the test
condition is true or false, the loop body will run at least once. Do-while
loops are loops with exit controls.

Loop Type Description


while loop First checks the condition, then executes the body.
for loop Firstly initializes, then, condition check, execute body, update.
do-while Firstly, execute the body then condition check

1. For Loop
The repetition control structure known as a "for loop" enables us to create a loop
that runs a certain number of times. The loop permits us to carry out n steps
concurrently in a single line.

Structure syntax:

for (initialization expr; test expr; update expr)


{
// body of the loop
// statements we want to execute
}
Example:
In for loop, a loop variable is used to control the loop. Initialize this loop variable
to a value, then determine if it is less than or larger than the value of the counter.
The body of the loop is run and the loop variable is updated if the statement is true.
Up to the exit condition, each step is repeated.

 Initialization Expression: We must initialize the loop counter to a certain


value in this expression. For example: int i=1;
 Test Expression: The condition must be tested in this expression. If the
condition is true, the body of the loop will be executed, and we will then
move on to updating the expression; otherwise, the for loop will end. For
example: i < 5;
 Update Expression: This expression increases or decreases the value of the
loop variable after the body of the loop has been executed. For example: i+
+;
2. While Loop
We have seen from studying the for loop that the number of iterations is known in
advance, i.e., how many times the body of the loop must be run. While loops are
utilized when we are unsure of the precise number of loop iterations in advance.
Depending on the results of the test, the loop execution is stopped.

The initialization expression, test expression, and update expression make up the
majority of a loop's syntax. The arrangement of these three statements varies
between the three looping constructs - For, while, and do while - in terms of
syntax.

initialization expression;
while (test_expression)
{
// statements

update_expression;
}

Example:
3. Do – while Loop
Do-while loops also include test conditions that determine when to end the loop's
execution. The fundamental distinction between a while loop and a do-while loop
is that a while loop tests the condition at the end of the loop body, whereas the
other two loops are entry controlled loops.

Observation: Regardless of the test condition, a do-while loop's body will run at
least once.

Structure syntax:
initialization expression;
do
{
// statements

update_expression;
} while (test_expression);

Example:

Exercise: Using 3 loop types above, write the solution for following
problems

Problem 1: Write a program in C++ to find the first 10 natural numbers.


Problem 2: Write a program in C++ to find the sum of first 10 natural numbers.

Problem 3: Write a program in C++ to find the factorial of a number.

Problem 4: Write a program in C++ to display the n terms of odd natural number
and their sum.

Sample Output:

Input number of terms: 5

The odd numbers are: 1 3 5 7 9

The Sum of odd Natural Numbers upto 5 terms: 25

Problem 5: Write a program in C++ to find power of any number using for loop.

Sample Output:

Input the base: 2

Input the exponent: 5

2 ^ 5 = 32

Problem 6: Write a program in C++ to enter any number and print all factors of
the number.

Sample Output:

Input a number: 63

The factors are: 1 3 7 9 21 63

You might also like