Module 2
Module 2
Learn about decision type control constructs in C and the way these are
used
Learn the use of special control constructs such as goto, break, continue,
and return
Negation !
Points to Note
If an expression, involving the relational operator, is true, it is given a value
of 1. If an expression is false, it is given a value of 0. Similarly, if a numeric
expression is used as a test expression, any non-zero value (including
negative) will be considered as true, while a zero value will be considered
as false.
Syntax of if Statement
if(condition)
{
// Statements to execute if
// condition is true
}
Here, the condition after evaluation will be either true or false. C if statement
accepts boolean values – if the value is true then it will execute the block of
statements below it otherwise not.
If we do not provide the curly braces ‘{‘ and ‘}’ after if(condition) then by default if
statement will consider the first immediately below statement to be inside its
How does an if Statement Work in C?
As the condition
present in the if
statement is false. So,
the block below the if
statement is not
executed.
Example 1: C Program to check whether the number is even or odd.
Output 1
Enter an integer: -2
You entered -2.
The if statement is easy.
Output 2
Enter an integer: 5
The if statement is
easy.
2. if-else in C
The if statement alone tells us that if a condition is true it will
execute a block of statements and if the condition is false it
won’t.
Enter an integer: 5
The number is positive.
Enter an integer: -3
The number is negative.
Enter an integer: 0
The number is zero.
3. Nested if-else in C
A nested if in C is an if statement that is the target of another if
statement.
134
Output 1
No
Input 2
111
Output 2
Yes
Output
i is smaller than 15
i is smaller than 12
too
4. if-else-if Ladder in C
The if else if statements are used when the user has to decide among
multiple options.
If none of the conditions is true, then the final else statement will be
executed.
if (Condition1)
{
Statement1;
}
else if(Condition2)
{
Statement2;
}
.
.
.
else if(ConditionN)
{
StatementN;
}
else
{
Default_Statement;
}
Flowchart of if-else-if Ladder
Working of if else if ladder statement:
Putting values of a, b and c condition becomes 12>33 && 12>-17, Right? In the first part of condition, we
have 12>33. This is checking, is 12 greater than 33? Answer is NO. So, 12>33 evaluates to FALSE.
Similarly, In the second part of condition, we have 12>-17. This is checking, is 12 greater than -17?
Answer is YES. So, 12>-17 evaluates to TRUE. Results of these two conditions are combined by logical
AND opeartor. So, finally FALSE && TRUE is evaluated. Since logical AND opeartors gives FALSE when
one of the operand is FALSE, so FALSE && TRUE evaluates to FALSE.
When condition in if is FALSE then it prevents execution of printf("Largest = %d", a); and checks for
another condition. Another condition here is b>a && b>c. Which is 33>12 && 33>-17 and evaluates to
TRUE && TRUE, which in turn evaluates to TRUE. So b>a && b>c evaluates to TRUE and hence
printf("Largest = %d", b); is executed printing Largest = 33. And then execution goes to next statement
neglecting else block where it executes retun(0) which is the last statement in the program and program
execution comes to end.
Exercise<<< for IF Else Programming:
1. Write a c program to print weekday based on given
number by using ladder(if-else-if statement),
2. Write a C program to check whether a number is
negative, positive or zero.
3. Write a C program to check whether a character is
uppercase or lowercase alphabet.
4. Write a C program to find all roots of a quadratic
equation.
5. Write a C program to check whether the triangle is
equilateral, isosceles or scalene triangle.
6. Write a C program to check whether a number is even or
odd.
Switch Statement in C
prize wheel game
default:
//code to be executed if all cases are not
matched;
}
Rules of the switch case statement
Following are some of the rules that we need to follow while using the switch
statement:
● In a switch statement, the “case value” must be of “char” and “int” type.
● You can't use two case labels with the same value. Duplicate case values
would throw an error.
● You can't define ranges within a case statement, nor can you use a single
case label for more than one value. A case label can hold only one value.
● There can be one or N number of cases.
● The values in the case must be unique.
● Each statement of the case can have a break statement. It is optional.
● The default Statement is also optional.
Rules of the switch case statement
Break in switch case
NOte:
Output
Case 1 is
Matched.
Example of switch case
without break
Output
Case 2 is executed.
Case 3 is executed.Case 4 is
executed.
Output
The day with
number 2 is
Tuesday
A simple
calculator by
Switch Case:
Output
Enter first number = 6
Enter second number = 7
Choose operator to
perform operations = +
Result: 6 + 7 = 13.000000
A simple calculator by
Switch Case:
2nd Approach
Output
Loops in programming are used to repeat a block of code until the specified
condition is met.
i++ and ++i are very similar but not exactly the same. Both increment the
number, but ++i increments the number before the current expression is
evaluted, whereas i++ increments the number after the expression is
evaluated.
int i = 3;
int a = i++; // a = 3, i = 4
int b = ++a; // b = 4, a = 4
For the case of the company wanting to calculate their salary and bonus of the their
employees
Logic:: Imagine a company maintains an array salary that contains the salaries
of its employees that they should receive every month. During the festive
months of November-December, the company happens to decide to give each
employee a bonus of 15% their salary. Hence, the company will have to create
a new array updated Salary that contains the salaries to be given to their
employees that have their bonuses updated.
To do this, the tech team will use a for loop to first calculate the salary+bonus
of each employee, and then update the new array with this sum.
How this occurs, is that a loop is run on the array salary where each element of
the array is appended, the bonus is calculated using the formula (0.15 *
salary), and then is added to the original salary. This sum is then appended
into the new array updatedSalary where it is stored in the same position as the
For the case of the company wanting to calculate their salary and bonus of the their
employees
OUTPUT:
15000
30000
22500
18000
28500
Multiplication Table Up to 10 using For Loop
Forms of for loop
A) NO INITIALIZATION:
Initialization can be skipped as shown below:
int x = 10;
for( ; x < 50; x++)
B) NO UPDATION:
We can run a for loop without needing an updation in the
following way:
C) NO INITIALIZATION AND UPDATE
STATEMENT:
int num;
We can avoid both initialization and update
for (num = 50; num < 60; )
statements too!
{ int x = 20;
num++; for (; x < 40; )
} {
x++;
}
While Loop
While loop does not depend upon the number of iterations. In for loop the number of
iterations was previously known to us but in the While loop, the execution is terminated
on the basis of the test condition. If the test condition will become false then it will
break from the while loop else body will be executed.
Syntax: How while loop works?
● The while loop evaluates the testExpression
initialization_expression; inside the parentheses ().
● If testExpression is true, statements inside the
while (test_expression) body of while loop are executed. Then,
{ testExpression is evaluated again.
// body of the while ● The process goes on until testExpression is
loop evaluated to false.
● If testExpression is false, the loop terminates
update_expression; (ends).
}
Flowchart of while loop:
Syntax:
initialization_expression;
while (test_expression)
{
// body of the while
loop
update_expression;
}
Here, we have initialized i to 1.
Output
When i = 1, the test expression i <= 5 is
1
2 true. Hence, the body of the while loop is
3 executed. This prints 1 on the screen and
4 the value of i is increased to 2.
5
Now, i = 2, the test expression i <= 5 is
again true. The body of the while loop is
executed again. This prints 2 on the screen
and the value of i is increased to 3.
In the do-while loop, the loop body will execute at least once irrespective of the
test condition.
do-while Loop
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
Output
Hello World
Output
Let's suppose, you have set an alarm clock to buzz at 05:00AM in the morning
(body of the loop). It will go off at 05:00AM in the morning i.e. at least once,
and suppose the condition at the end of the loop is that if you have snoozed
your alarm or not. If you have snoozed (i.e. true), it will buzz again after a
certain time and it will not buzz if you have set the alarm off without snoozing it
(i.e. false).
Output
Uses of break in C
Explanation:
Syntax of continue in C
continue;
goto label;
.
.
label:
//code
Check if a number is odd or even using goto statement.
Output
26 is even
return expression;
Result: 8
KJSIT MUMBAI
Exercises:
1. Write a c program to print table of given number using for loop.
2. Write a C program to display a pattern like a right angle triangle with a number.
The pattern like :
1
12
123
1234
3.
Write a program in C to display n terms of natural numbers and their sum.
Test Data : 7
Expected Output :
The first 7 natural number is :
1234567
The Sum of Natural Number upto 7 terms : 28
4. Write a program in C to display the first 10 natural numbers.
Expected Output :
1 2 3 4 5 6 7 8 9 10