0% found this document useful (0 votes)
12 views18 pages

unit-2 c language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views18 pages

unit-2 c language

Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

lOMoARcPSD|50783957

UNIT-2-CP(R23) - Notes on c programming

C Programming (Bonam Venkata Chalamayya Institute of Technology & Science)

Scan to open on Studocu

Studocu is not sponsored or endorsed by any college or university


Downloaded by Durga Prasad ([email protected])
lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

UNIT-II: Control Structures:


Simple sequential programs, Conditional Statements (if, if-else, switch), Loops (for, while, do-while)
Break and Continue.
-------------------------------------------------------------------------------------------------------------------------------------
1. SIMPLE SEQUENTIAL PROGRAMS

 Sequence of statements are written in order to accomplish a specific activity. So statements


are executed in the order they are specified in the program. This way of executing statements
sequentially is known as Sequential control statements.
 There is an advantage that is no separate control statements are needed in order to execute
the statements one after the other.
 Disadvantage is that there is no way to change the sequence. The solution for this is
branching.

Example: / * Program to swap two numbers */


#include<stdio.h>
int main() {
double first, second, temp;
printf("Enter first number: ");
scanf("%lf", &first);
printf("Enter second number: ");
scanf("%lf", &second);

// value of first is assigned to temp


temp = first;

// value of second is assigned to first


first = second;

// value of temp (initial value of first) is assigned to second


second = temp;

// %.2lf displays number up to 2 decimal points


printf("\nAfter swapping, first number = %.2lf\n", first);
printf("After swapping, second number = %.2lf", second);
return 0;
}

Output:
Enter first number: 1.20
Enter second number: 2.45

After swapping, first number = 2.45


After swapping, second number = 1.20

Example: / * Swap Numbers Without Using Temporary Variables*/


#include <stdio.h>
int main() {

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 1

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

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

2. CONDITIONAL STATEMENTS (IF, IF-ELSE, SWITCH)

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.

Following are the decision-making statements available in C

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.

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 2

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

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

// C program to illustrate If statement


#include <stdio.h>
int main()
{
int i = 10;

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

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 3

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

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) {

printf("i is smaller than 15");


}
else {
printf("i is greater than 15");
}
return 0;
}
Output :
i is greater than 15

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 4

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

The block of code following the else statement is executed as the condition present in the if
statement is false.

3. Nested if-else statement:

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


mean an if statement inside another if statement. Yes, 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.

Syntax of Nested if-else


if (condition1)
{
// Executes when condition1 is true
if (condition2)
{
// Executes when condition2 is true
}
else
{
// Executes when condition2 is false
}

Flowchart of Nested if-else

// C program to illustrate nested-if statement


#include <stdio.h>
int main()
{
int i = 10;
if (i == 10) {
// First if statement
if (i < 15)
printf("i is smaller than 15\n");
// Nested - if statement Will only be executed if statement above is true
if (i < 12)
printf("i is smaller than 12 too\n");
Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 5

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

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.

Syntax of if-else-if Ladder


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

else
statement;
Flowchart of if-else-if Ladder

// C program to illustrate nested-if statement


#include <stdio.h>
int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 6

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

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

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 7

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

// C Program to illustrate the use of switch statement


#include <stdio.h>
int main()
{
// variable to be used in switch statement
int var = 2;
// declaring switch cases
switch (var) {
case 1:
printf("Case 1 is executed");
break;
case 2:
printf("Case 2 is executed");
break;
default:
printf("Default Case is executed");
break;
}
return 0;
}
Output
Case 2 is executed
6. Conditional Operator:

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.

Syntax of Conditional Operator


(condition) ? [true_statements] : [flase_statements];
Flowchart of Conditional Operator

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 8

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

// C Program to illustrate the use of conditional operator


#include <stdio.h>
// driver code
int main()
{
int var;
int flag = 0;
// using conditional operator to assign the value to var
// according to the value of flag
var = flag == 0 ? 25 : -25;
printf("Value of var when flag is 0: %d\n", var);
// changing the value of flag
flag = 1;
// again assigning the value to var using same statement
var = flag == 0 ? 25 : -25;
printf("Value of var when flag is NOT 0: %d", var);
return 0;
}
Output
Value of var when flag is 0: 25
Value of var when flag is NOT 0: -25

3. LOOPS (FOR, WHILE, DO-WHILE)

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.

// C program to illustrate need of loops


#include <stdio.h>
int main()
{
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
printf( "Hello World\n");
return 0;
}
Output
Hello World
Hello World
Hello World
Hello World

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 9

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

There are mainly two types of loops in C Programming:

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.

Loop Type Description


first Initializes, then condition check, then executes the body and at last, the update is
for loop
done.
first Initializes, then condition checks, and then executes the body, and updating can be
while loop
inside the body.
do-while
do-while first executes the body and then the condition check is done.
loop
for 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:

for (initialize expression; test expression; update expression)


{
//

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 10

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

// body of for loop


//
}

Example:

for(int i = 0; i < n; ++i)


{
printf("Body of for loop which will execute till n");
}

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.

for loop Equivalent Flow Diagram:

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 11

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

// C program to illustrate for loop


#include <stdio.h>
// Driver code
int main()
{
int i = 0;

for (i = 1; i <= 10; i++)


{
printf( "Hello World\n");
}
return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

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;
}

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 12

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

Flow Diagram for while loop:

// C program to illustrate while loop


#include <stdio.h>
// Driver code
int main()
{
// Initialization expression
int i = 2;
// Test expression
while(i < 10)
{
// loop body
printf( "Hello World\n");
// update expression
i++;
}
return 0;
}
Output
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World
Hello World

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 13

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

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);

Flow Diagram for do-while loop:

// C program to illustrate do-while loop


#include <stdio.h>
// Driver code
int main()
{
// Initialization expression
int i = 2;
do
{
// loop body
printf( "Hello World\n");

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 14

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

// 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.

3. LOOP CONTROL STATEMENTS

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

Example: Program for break

#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

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 15

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

Example: Program for Continue

#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.

Using for loop:

// C program to demonstrate infinite


// loops using for loop
#include <stdio.h>
// Driver code
int main ()
{
int i;
// This is an infinite for loop as the condition expression is blank
for ( ; ; )
{
printf("This loop will run forever.\n");
}
return 0;
}

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 16

Downloaded by Durga Prasad ([email protected])


lOMoARcPSD|50783957

INTRODUCTION TO PROGRAMMING UNIT-2

Output

This loop will run forever.


This loop will run forever.
This loop will run forever.
...

Using While loop:

// C program to demonstrate infinite loop using while loop


#include <stdio.h>
// Driver code
int main()
{
while (1)
printf("This loop will run forever.\n");
return 0;
}

Output

This loop will run forever.


This loop will run forever.
This loop will run forever.
...

Using the do-while loop:

// C program to demonstrate infinite loop using do-while loop


#include <stdio.h>
// Driver code
int main()
{
do
{
printf("This loop will run forever.\n");
} while (1);
return 0;
}

Output

This loop will run forever.


This loop will run forever.
This loop will run forever.
...

Prepared by N.Sushuma, Assoc. Prof., Dept. of CSE, BVCITS Page 17

Downloaded by Durga Prasad ([email protected])

You might also like