unit-2 c language
unit-2 c language
Output:
Enter first number: 1.20
Enter second number: 2.45
double a, b;
printf("Enter a: ");
scanf("%lf", &a);
printf("Enter b: ");
scanf("%lf", &b);
// swapping
// a = (initial_a - initial_b)
a = a - b;
// b = (initial_a - initial_b) + initial_b = initial_a
b = a + b;
// a = initial_a - (initial_a - initial_b) = initial_b
a = b - a;
// %.2lf displays numbers up to 2 decimal places
printf("After swapping, a = %.2lf\n", a);
printf("After swapping, b = %.2lf", b);
return 0;
}
Output:
Enter a: 10.25
Enter b: -12.5
After swapping, a = -12.50
After swapping, b = 10.25
The conditional statements (also known as decision control structures) such as if, if else, switch, etc.
are used for decision-making purposes in C programs.
They are also known as Decision-Making Statements and are used to evaluate one or more
conditions and make the decision whether to execute a set of statements or not. These decision-
making statements in programming languages decide the direction of the flow of program execution.
1. if Statement
2. if-else Statement
3. Nested if Statement
4. if-else-if Ladder
5. switch Statement
6. Conditional Operator
1. if statement:
The if statement is the most simple decision-making statement. It is used to decide whether a certain
statement or block of statements will be executed or not i.e if a certain condition is true then a block
of statements is executed otherwise not.
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 block.
Flowchart of if Statement
if (i > 15) {
printf("10 is greater than 15");
}
printf("I am Not in if");
}
Output:
I am Not in if
As the condition present in the if statement is false. So, the block below the if statement is not
executed.
2. if-else statement:
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. But what if we want to do something else when the condition is false?
Here comes the C else statement. We can use the else statement with the if statement to execute a
block of code when the condition is false. The if-else statement consists of two blocks, one for false
expression and one for true expression.
Syntax of if else
if (condition)
{
// Executes this block if
// condition is true
}
else
{
// Executes this block if
// condition is false
}
Flowchart of if-else Statement
Example:
// C program to illustrate If statement
#include <stdio.h>
int main()
{
int i = 20;
if (i < 15) {
The block of code following the else statement is executed as the condition present in the if
statement is false.
else
printf("i is greater than 15");
}
return 0;
}
Output:
i is smaller than 15
i is smaller than 12 too
4. if-else-if Ladder statement:
The if else if statements are used when the user has to decide among multiple options. The C if
statements are executed from the top down. As soon as one of the conditions controlling the if is
true, the statement associated with that if is executed, and the rest of the C else-if ladder is
bypassed. If none of the conditions is true, then the final else statement will be executed. if-else-if
ladder is similar to the switch statement.
else
statement;
Flowchart of if-else-if Ladder
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}
Output:
i is 20
5. switch Statement:
The switch case statement is an alternative to the if else if ladder that can be used to execute the
conditional code based on the value of the variable specified in the switch statement. The switch
block consists of cases to be executed based on the value of the switch variable.
Syntax of switch
switch (expression) {
case value1:
statements;
case value2:
statements;
....
....
....
default:
statements;
}
Note: The switch expression should evaluate to either integer or character. It cannot evaluate any
other data type.
Flowchart of switch
The conditional operator is used to add conditional code in our program. It is similar to the if-else
statement. It is also known as the ternary operator as it works on three operands.
Loops in programming are used to repeat a block of code until the specified condition is met. A
loop statement allows programmers to execute a statement or group of statements multiple
times without repetition of code.
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
1. Entry Controlled loops: In Entry controlled loops the test condition is checked before entering
the main body of the loop. For Loop and While Loop is Entry-controlled loops.
2. Exit Controlled loops: In Exit controlled loops the test condition is evaluated at the end of the
loop body. The loop body will execute at least once, irrespective of whether the condition is
true or false. do-while Loop is Exit Controlled loop.
for loop in C programming is a repetition control structure that allows programmers to write a loop
that will be executed a specific number of times. for loop enables programmers to perform n number
of steps together in a single line.
Syntax:
Example:
In for loop, a loop variable is used to control the loop. Firstly we initialize the loop variable with some
value, then check its test condition. If the statement is true then control will move to the body and
the body of for loop will be executed. Steps will be repeated till the exit condition becomes true. If
the test condition will be false then it will stop.
Initialization Expression: In this expression, we assign a loop variable or loop counter to some
value. for example: int i=1;
Test Expression: In this expression, test conditions are performed. If the condition evaluates
to true then the loop body will be executed and then an update of the loop variable is done. If
the test expression becomes false then the control will exit from the loop. for example, i<=9;
Update Expression: After execution of the loop body loop variable is updated by some value
it could be incremented, decremented, multiplied, or divided by any value.
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:
initialization_expression;
while (test_expression)
{
// body of the while loop
update_expression;
}
do-while Loop:
The do-while loop is similar to a while loop but the only difference lies in the do-while loop test
condition which is tested at the end of the body. In the do-while loop, the loop body will execute at
least once irrespective of the test condition.
Syntax:
initialization_expression;
do
{
// body of do-while loop
update_expression;
} while (test_expression);
// Update expression
i++;
// Test expression
} while (i < 1);
return 0;
}
Output:
Hello World
Above program will evaluate (i<1) as false since i = 2. But still, as it is a do-while loop the body will be
executed once.
Loop control statements in C programming are used to change execution from its normal sequence.
Name Description
break the break statement is used to terminate the switch and loop statement. It transfers
statement the execution to the statement immediately following the loop or switch.
continue continue statement skips the remainder body and immediately resets its condition
statement before reiterating it.
goto
goto statement transfers the control to the labeled statement.
statement
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
break;
}
printf("%d\n", i);
}
return 0;
}
Output:
0
1
2
3
#include <stdio.h>
int main() {
int i;
for (i = 0; i < 10; i++) {
if (i == 4) {
continue;
}
printf("%d\n", i);
}
return 0;
}
Output:
0
1
2
3
5
6
7
8
9
INFINITE LOOP
An infinite loop is executed when the test expression never becomes false and the body of the loop is
executed repeatedly. A program is stuck in an Infinite loop when the condition is always true. Mostly
this is an error that can be resolved by using Loop Control statements.
Output
Output
Output