0% found this document useful (0 votes)
13 views17 pages

Unit II Control Structures9

Unit II covers control structures in C programming, focusing on decision-making and looping constructs. It explains various conditional statements like if, if-else, nested if, switch, and the conditional operator, along with jump statements such as break, continue, and goto. Additionally, it details loop types including while, do-while, and for loops, including their syntax and examples.

Uploaded by

ice285028
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)
13 views17 pages

Unit II Control Structures9

Unit II covers control structures in C programming, focusing on decision-making and looping constructs. It explains various conditional statements like if, if-else, nested if, switch, and the conditional operator, along with jump statements such as break, continue, and goto. Additionally, it details loop types including while, do-while, and for loops, including their syntax and examples.

Uploaded by

ice285028
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

UNIT II CONTROL STRUCTURES

Decision Making with IF, IF-ELSE,


IF ELSE, Nested IF Statement, Switch
statement, Conditional Operator, while Statement, do statement,
for statement, Nested for statements, Unconditional statements.
Control statements in C are used to direct the flow of a program's execution by specifying the
order in which the program's instructions are executed:

In programming we need to make some decisions and based on these decisions we will
execute the next block of code.

CONDITIONAL STATEMENTS

The conditional statements (also known as decision control structures) such as if, if else,
switch, etc. are used for decision-making
decision purposes in C programs.
1. if Statement
2. if-else Statement
3. Nested if Statement
1
4. if-else-if Ladder
5. switch Statement
6. Conditional Operator
7. Jump Statements:
8. break
9. continue
10. goto
11. return

1. Simple if Statement :
Simple if statements are carried out to perform some operation when the condition is only
true. If the condition of the if statement is true then the statements under the if block is
executed else the control is transferred to the statements outside the if block

Syntax: Flow Chart:

if(condition)

// Statements to execute if

// condition is true

e.g//
// 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");


}

2. If-else Statement
In some situations, you may have to execute statements based on true or false under certain
conditions, therefore; you use if-else
if else statements. If the condition is true, then if block will be
executed otherwise the else block is executed.

2
Syntax of if-else Flowchart

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

e.g

// 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;
}
3. Nested if-else
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, C allow us to nested if
statements within if statements, i.e, we can place an if statement inside another if statement.

3
if (condition1)
{
// Executes when condition1 is
true
if (condition_2)
{
// statement 1
}
else
{
// Statement 2
}
}
else {
if (condition_3)
{
// statement 3
}
else
{
// Statement 4
}
}

e.g
// C program to illustrate nested-if
nested 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");
15
// Nested - if statement Will only be executed if statement above is true
if (i < 12)
printf("i is smaller than 12 too\n");
too
else
printf("i is greater than 15");
}
else {
if (i == 20) {
// Nested - if statement Will only be executed if statement above is true
if (i < 22)
printf("i is smaller than 22 too\n");
too
else
printf("i is greater than 25");
} }
return 0;
}

4
4. if-else-if Ladder
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
else
ladder is bypassed.

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

e.g
// C program to illustrate nested-if
nested statement
#include <stdio.h>

int main()
{
int i = 20;
if (i == 10)
printf("i is 10");
else if (i == 15)
printf("i is 15");
else if (i == 20)
printf("i is 20");
else
printf("i is not present");
}

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.

5
Syntax
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.

e.g

// 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 Output:


switch (var) { Case 2 is executed
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;
}

6
// C program to illustrate using range in switch case
Ex-2:
#include <stdio.h>
int main()
{
int arr[] = { 1, 5, 15, 20 };

for (int i = 0; i < 4; i++) {


switch (arr[i]) { Output:
// range 1 to 6 1 in range 1 to 6
case 1 ... 6: 5 in range 1 to 6
printf("%d in range 1 to 6\n", arr[i]); 15 not in range
break; 20 in range 19 to 20
// range 19 to 20
case 19 ... 20:
printf("%d in range 19 to 20\n", arr[i]);
break;
default:
printf("%d not in range\n", arr[i]);
break;
} }
return 0; }

6. Conditional Operator (ternary 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] : [false_statements];

e.g
// C Program to illustrate the use of conditional operator
#include <stdio.h>
int main() {
int age;
Output:
Enter your age:20
// take input from users
printf("Enter your age: "); You can vote
scanf("%d", &age);

// ternary operator to find if a person can vote or not


(age >= 18) ? printf("You can vote") : printf("You cannot vote");
return 0;
}

7. Jump Statements in C
These statements are used in C for the unconditional flow of control throughout the functions
in a program. They support four types of 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.
7
Syntax of break

break;

B) continue
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.

Syntax of continue

continue;

// C program to explain the use of continue statement


#include <stdio.h>
int main()
{
// loop from 1 to 10
for (int i = 1; i <= 10; i++) { Output
// If i is equals to 6, 1 2 3 4 5 7 8 9 10
// continue to next iteration
// without printing
if (i == 6)
continue;
else
// otherwise print the value of i
printf("%d ", i);
} return 0; }

8
C) goto statement
The goto statement in C also referred to as the unconditional jump statement can be used to
jump from one point to another within a function.
Syntax of goto
Syntax1 | Syntax2
----------------------------
gotolabel; | label:
. | .
. | .
. | .
label: | gotolabel;

// C program to print numbers from 1 to 10 using goto// statement


#include <stdio.h>
// function to print numbers from 1 to 10
void printNumbers()
{
int n = 1;
label:
printf("%d ", n); Output
n++; 1 2 3 4 5 6 7 8 9 10
if (n <= 10)
goto label;
}
// Driver program to test above function
int main()
{
printNumbers(); return 0; }

9
Loops (iteration) in C

Loop Type Description

first Initializes, then condition check, then executes the body and at
for loop
last, the update is done.

first Initializes, then condition checks, and then executes the body,
while loop
and updating can be inside the body.

do-while loop do-while first executes the body and then the condition check is done.

1. While Loop

In this, the condition is evaluated before processing the loop’s body. Only the loop’s body is
executed if the condition is true. Then the control goes back to the beginning after completing
the loop once. The statements in the loop will be executed again, and if the condition is true
10
and checked, this process goes on until the condition becomes false. The control will go out
of the loop if the condition is false. After completion of the loop, the control will go to the
statement
ment immediately after the loop, and the body can contain more than one statement. The
curly braces are not that important if they have only one statement. If the condition is not true
in the while loop, then loop statements won’t get executed.

Syntax:

while (condition) {
statements;
}
Flowchart:

Example:

#include<stdio.h>
#include<conio.h> Output:
1
int main()
2
{ 3
int num=1; 4
while(num<=5) 5
{
printf("%d\n",num);
num++;
}
return 0;
}

2. do-while Loop

The do-while
while loop is similar to a while loop but the only difference lies in the do-while
do loop
test condition which is tested at the end of the body. In the do-while
do while loop, the loop body will
execute at least once irrespective of the test condition.

11
Syntax:

initialization_expression;
do
{
// body of do-while loop

update_expression;

} while (test_expression);

// C program to illustrate do-while


while loop
#include <stdio.h>

// Driver code
int main()
{
// Initialization expression Output:
int i = 2; Hello World
do
{
// loop body
printf( "Hello World\n");

// Update expression
i++;

// Test expression
} while (i < 1); return 0; }

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

12
Syntax:

for (initialize expression; test expression; update expression)


{
//
// 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,


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
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,
multipl or divided by any value.

e.g Output:
1 2 3 4 5
#include<stdio.h>
#include<conio.h>
int main()
{
int number;
for(number=1;number<=5;number++)
13
{
printf("%d\n",number);
}
return 0;
}

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.

Infinite for loop Infinite While loop Infinite do … while loop

for ( ; ; ) while (1) do


{ { {
.
} .} } while (1);

Nested Loop

a loop inside a loop is called Nested Loop. There can be any number of loops inside a loop.

Syntax:

Outside_loop
{
//Outside Loop Statements
Inside_loop
{
//Inside loop Statements
}
}

14
Nested for loop Nested ‘while’ loop Nested do.. while

for (initialization; condition; update) while(condition) do


{ { {
for(initialization; condition; update) while(condition) do
{ {
{
// inner loop statements. // inner loop statements.
// inner loop }while(condition);
}
statements. // outer loop statements.
// outer loop statements.
} } }while(condition);
// outer loop statements.
}

Example ( Nested for loop)

int main()
{
int n;// variable declaration
int i,j;
n=3;
// Displaying the n tables.
for(i=1;i<=n;i++) // outer loop
{
=10;j++) // inner loop
for(j=1;j<=10;j++)
{
printf("%d\t",(i*j));
t",(i*j)); // printing the value.
}
printf("\n");
}
}

15
OUTPUT:
1 2 3 4 5 6 7 8 9 10
2 4 6 8 10 12 14 16 18 20
3 6 9 12 15 18 21 24 27 30
Example : nested while loop

// C program to print pattern using nested while loops


#include <stdio.h>

int main()
{
int end = 5;

printf("Pattern Printing using Nested While loop");

int i = 1;

while (i <= end) {


printf("\n");
int j = 1;
while (j <= i) {
printf("%d ", j);
j = j + 1;
}
i = i + 1;
}
return 0;
}

OUTPUT:

Pattern Printing using Nested While loop


1
12
123
1234
12345
Example of nested do .. while loop

#include <stdio.h>

int main() {

// Declaring loop variables


int i = 0, j;
int c = 0;

// Outer loop starts


do {
j = 0;

// inner loop starts


do {
printf("%d ", c++);
j++;
16
} while (j < 3);
printf("\n");
i++;
} while (i < 3);

return 0;
}

OUTPUT:

0 1 2
3 4 5
6 7 8

while do-while

Condition is checked first then statement(s) Statement(s) is executed atleast once,


is executed. thereafter condition is checked.

It might occur statement(s) is executed zero


At least once the statement(s) is executed.
times, If condition is false.

No semicolon at the end of while. Semicolon at the end of while.


while(condition) while(condition);

Variable in condition is initialized before variable may be initialized before or within


the execution of loop. the loop.

while loop is entry controlled loop. do-while loop is exit controlled loop.

while(condition) { do {
statement(s); statement(s);
} } while(condition);

17

You might also like