Thanks to visit codestin.com
Credit goes to www.scribd.com

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

Unit 2

This document covers decision-making and branching in C programming, including the use of if-else statements, switch statements, and the goto statement. It also explains loops, detailing entry-controlled (for and while loops) and exit-controlled (do-while loops) types, along with their syntax and examples. Additionally, it discusses loop control statements such as break, continue, and the concept of infinite loops.

Uploaded by

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

Unit 2

This document covers decision-making and branching in C programming, including the use of if-else statements, switch statements, and the goto statement. It also explains loops, detailing entry-controlled (for and while loops) and exit-controlled (do-while loops) types, along with their syntax and examples. Additionally, it discusses loop control statements such as break, continue, and the concept of infinite loops.

Uploaded by

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

UNIT -2

DECISION MAKING AND BRANCHING


#include<stdio.h>
void main ()
{
int a,b,c,d;
printf("Enter the values of a,b,c,d: ");
scanf("%d%d%d%d",&a,&b,&c,&d);
if(a>b && a>c && a>d)
{
printf("%d is the largest",a);
}
else if(b>c && b>a && b>d)
{
printf("%d is the largest",b);
}
else if(c>d && c>a && c>b)
{
printf("%d is the largest",c);
}
Else
{
printf("%d is the largest",d);
}
}
Syntax of switch Statement in C

switch(expression)
{
case value1: statement_1;
break;
case value2: statement_2;
break;
.
.
.
case value_n: statement_n;
break;
default: default_statement;
}
Example:
#include <stdio.h>

int main()
{
// switch variable
int var = 1;

// switch statement
switch (var) {
case 1:
printf("Case 1 is Matched.");
break;

case 2:
printf("Case 2 is Matched.");
break;
case 3:
printf("Case 3 is Matched.");
break;

default:
printf("Default case is Matched.");
break;
}

return 0;
}
Flowchart of Switch Statement
goto Statement in C

The C goto statement is a jump statement which is sometimes also


referred to as an unconditional jump statement. The goto statement can be
used to jump from anywhere to anywhere within a function.

Syntax:
Syntax1 | Syntax2
----------------------------
goto label; | label:
. | .
. | .
. | .
label: | goto label;

In the above syntax, the first line tells the compiler to go to or jump to the
statement marked as a label. Here, the label is a user-defined identifier that
indicates the target statement. The statement immediately followed after
‘label:’ is the destination statement. The ‘label:’ can also appear before the
‘goto label;’ statement in the above syntax.

Flowchart of goto Statement in C


C – Loops
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
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
Description
Type

for loop first Initializes, then condition check, then executes the body and
Loop
Description
Type

at last, the update is done.

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

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

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)


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


Example:

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

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

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

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

Loop control statements in C programming are used to change execution


from its normal sequence.

Name Description

the break statement is used to terminate the switch and loop


break
statement. It transfers the execution to the statement
statement
immediately following the loop or switch.

continue continue statement skips the remainder body and immediately


statement resets its condition before reiterating it.

goto
goto statement transfers the control to the labeled statement.
statement

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:

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;

You might also like