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

0% found this document useful (0 votes)
7 views15 pages

UNIT-III Control & Decision Statements

Unit 3 control and decision making process

Uploaded by

kevalkalal22
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)
7 views15 pages

UNIT-III Control & Decision Statements

Unit 3 control and decision making process

Uploaded by

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

UNIT III

Decision Statements:
Decision making structures require that the programmer specifies one or more conditions to be
evaluated or tested by the program, along with a statement or statements to be executed if the
condition is determined to be true, and optionally, other statements to be executed if the
condition is determined to be false. Flow chart of if-else is given in below picture.

There are the following variants of if statement in C language.

o If statement
o If-else statement
o If else-if ladder
o Nested if

1. 1 if statement: The if statement is used to check some given condition and perform
some operations depending upon the correctness of that
example:
#include<stdio.h>
int main()
{
int a=2;
if(a%2==0)
{
Printf(“even”);
}
}

2 If-else Statement:
The if-else statement is used to perform two operations for a single condition. The if-else
statement is an extension to the if statement using which, we can perform two different
operations, i.e., one is for the correctness of that condition, and the other is for the incorrectness
of the condition.
Example:
#include<stdio.h>
int main()
{
int a=2;
if(a%2==0)
{
Printf(“even”);
}
else
Printf(“odd”);

3 If else-if ladder Statement

The if-else-if ladder statement is an extension to the if-else statement. It is used in the scenario
where there are multiple cases to be performed for different conditions. In if-else-if ladder
statement, if a condition is true then the statements defined in the if block will be executed,
otherwise if some other condition is true then the statements defined in the else-if block will
be executed, at the last if none of the condition is true then the statements defined in the else
block will be executed.
Example:
#include<stdio.h>
int main()
{
int number=0;
printf("enter a number:");
scanf("%d",&number);
if(number==10)
{
printf("number is equals to 10");
}
else if(number==50)
{
printf("number is equal to 50");
}
else if(number==100)
{
printf("number is equal to 100");
}
else
{
printf("number is not equal to 10, 50 or 100");
}
return 0;

4 Nested if statement:
When an if else statement is present inside the body of another “if” or “else” then this is called
nested if else.
Example:

#include <stdio.h>
int main()
{
int var1, var2;
printf("Input the value of var1:");
scanf("%d", &var1);
printf("Input the value of var2:");
scanf("%d",&var2);
if (var1 != var2)
{
printf("var1 is not equal to var2\n");
//Nested if else
if (var1 > var2)
{
printf("var1 is greater than var2\n");
}
else
{
printf("var2 is greater than var1\n");
}
}
else
{
printf("var1 is equal to var2\n");
}
return 0;
}
C – Loops
Programming languages provide various control structures that allow for more
complicated execution paths.
A loop statement allows us to execute a statement or group of statements multiple
times. Given below is the general form of a loop statement in most of the programming
languages

Sr.No. Loop Type & Description

1 while loop: Repeats a statement or group of statements while a given condition is true. It tests the
condition before executing the loop body.

2 for loop: Executes a sequence of statements multiple times and abbreviates the code that manages
the loop variable.

3
do...while loop :It is more like a while statement, except that it tests the condition at the end of the
loop body.
While Loop:
A while loop in C programming repeatedly executes a target statement as long as a given
condition is true.
Syntax:
while(condition)
{
statement(s);
}

Example:
#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* while loop execution */


while( a < 20 ) {
printf("value of a: %d\n", a);
a++;
}

return 0;
}

Output: value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19
for loop in C
A for loop is a repetition control structure that allows you to efficiently write a loop that needs
to execute a specific number of times.

Syntax :
for ( init; condition; increment )
{
statement(s);
}
Here is the flow of control in a 'for' loop −
 The init step is executed first, and only once. This step allows you to declare and
initialize any loop control variables. You are not required to put a statement here, as
long as a semicolon appears.
 Next, the condition is evaluated. If it is true, the body of the loop is executed. If it is
false, the body of the loop does not execute and the flow of control jumps to the next
statement just after the 'for' loop.
 After the body of the 'for' loop executes, the flow of control jumps back up to
the increment statement. This statement allows you to update any loop control
variables. This statement can be left blank, as long as a semicolon appears after the
condition.
 The condition is now evaluated again. If it is true, the loop executes and the process
repeats itself (body of loop, then increment step, and then again condition). After the
condition becomes false, the 'for' loop terminates.

Example:
#include <stdio.h>
int main ()
{
int a;
/* for loop execution */
for( a = 10; a < 20; a = a + 1 ){
printf("value of a: %d\n", a);
}
return 0;
}

Output: value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

do...while loop in C
A do...while loop is similar to a while loop, except the fact that it is guaranteed to execute at
least one time.
Syntax:
do
{
statement(s);
} while( condition );

Notice that the conditional expression appears at the end of the loop, so the statement(s) in the
loop executes once before the condition is tested.
If the condition is true, the flow of control jumps back up to do, and the statement(s) in the
loop executes again. This process repeats until the given condition becomes false.
Example:
#include <stdio.h>
int main ()
{

/* local variable definition */


int a = 10;

/* do loop execution */
do {
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );

return 0;
}

Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
value of a: 16
value of a: 17
value of a: 18
value of a: 19

switch-case statement
A switch statement allows a variable to be tested for equality against a list of values. Each
value is called a case, and the variable being switched on is checked for each switch case.

Syntax:
switch(expression) {

case constant-expression :
statement(s);
break; /* optional */

case constant-expression :
statement(s);
break; /* optional */

/* you can have any number of case statements */


default : /* Optional */
statement(s);
}

The following rules apply to a switch statement −

 The expression used in a switch statement must have an integral or enumerated type,
or be of a class type in which the class has a single conversion function to an integral
or enumerated type.

 You can have any number of case statements within a switch. Each case is followed by
the value to be compared to and a colon.

 The constant-expression for a case must be the same data type as the variable in the
switch, and it must be a constant or a literal.

 When the variable being switched on is equal to a case, the statements following that
case will execute until a break statement is reached.
 When a break statement is reached, the switch terminates, and the flow of control
jumps to the next line following the switch statement.

 Not every case needs to contain a break. If no break appears, the flow of control
will fall through to subsequent cases until a break is reached.

 A switch statement can have an optional default case, which must appear at the end of
the switch. The default case can be used for performing a task when none of the cases
is true. No break is needed in the default case.

Example:
#include <stdio.h>

int main () {

/* local variable definition */


Int day = 3;

switch(day) {
case 1 :
printf("Monday\n" );
break;
case 2 :
printf("Tuesday\n" );
break;

case 3 :
printf("Wednesday\n" );
break;
case 4 :
printf("Thursday\n" );
break;
case 5 :
printf("Friday\n" );
break;
case 6:
printf("Saturday\n" );
break;
case 7:
printf("Sunday\n" );
break;
default :
printf("Invalid number entered\n" );
}
return 0;
}

Output: Wednesday

break statement in C
The break statement in C programming has the following two usages −
 When a break statement is encountered inside a loop, the loop is immediately
terminated and the program control resumes at the next statement following the loop.
 It can be used to terminate a case in the switch statement
Example:
#include <stdio.h>

int main () {

/* local variable definition */


int a = 10;

/* while loop execution */


while( a < 20 ) {

printf("value of a: %d\n", a);


a++;

if( a > 15) {


/* terminate the loop using break statement */
break;
}
}

return 0;
}

Output: value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 15
continue statement in C
The continue statement in C programming works somewhat like the break statement. Instead
of forcing termination, it forces the next iteration of the loop to take place, skipping any code
in between.

For the for loop, continue statement causes the conditional test and increment portions of the
loop to execute. For the while and do...while loops, continue statement causes the program
control to pass to the conditional tests.

Example:

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

/* local variable definition */


int a = 10;

/* do loop execution */
Do
{

if( a == 15) {
/* skip the iteration */
a = a + 1;
continue;
}

printf("value of a: %d\n", a);


a++;

} while( a < 20 );

return 0;
}

Output:

value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19

goto statement in C
A goto statement in C programming provides an unconditional jump from the 'goto' to a
labeled statement in the same function.
Example:
#include <stdio.h>
int main () {

/* local variable definition */


int a = 10;

/* do loop execution */
LOOP:do {

if( a == 15) {
/* skip the iteration */
a = a + 1;
goto LOOP;
}

printf("value of a: %d\n", a);


a++;

}while( a < 20 );

return 0;
}

Output:
value of a: 10
value of a: 11
value of a: 12
value of a: 13
value of a: 14
value of a: 16
value of a: 17
value of a: 18
value of a: 19
Functions
A function is a group of statements that together perform a task. Every C program has at least
one function, which is main(), and all the most trivial programs can define additional functions.
A function declaration tells the compiler about a function's name, return type, and parameters.
A function definition provides the actual body of the function. A function can also be referred
as a method or a sub-routine or a procedure, etc.

Defining a Function
Syntax:

return_type function_name( parameter list )


{
body of the function
}

A function definition in C programming consists of a function header and a function body.


Here are all the parts of a function −
 Return Type − A function may return a value. The return_type is the data type of the
value the function returns. Some functions perform the desired operations without
returning a value. In this case, the return_type is the keyword void.
 Function Name − This is the actual name of the function. The function name and the
parameter list together constitute the function signature.
 Parameters − A parameter is like a placeholder. When a function is invoked, you pass
a value to the parameter. This value is referred to as actual parameter or argument. The
parameter list refers to the type, order, and number of the parameters of a function.
Parameters are optional; that is, a function may contain no parameters.
 Function Body − The function body contains a collection of statements that define
what the function does.
Example:
int max (int num1, int num2)
{
/* local variable declaration */
int result;
if (num1 > num2)
result = num1;
else
result = num2;
return result;
}

Function Declarations:

A function declaration tells the compiler about a function name and how to call the
function. The actual body of the function can be defined separately .

Syntax:

return_type function_name( parameter list );

Calling a Function:

Example:

#include <stdio.h>

/* function declaration */
int max(int num1, int num2);

int main () {

/* local variable definition */


int a = 100;
int b = 200;
int ret;

/* calling a function to get max value */


ret = max(a, b);

printf( "Max value is : %d\n", ret );

return 0;
}

/* function returning the max between two numbers */


int max(int num1, int num2) {

/* local variable declaration */


int result;

if (num1 > num2)


result = num1;
else
result = num2;

return result;
}

Output: Max value is : 200


While calling a function, there are two ways in which arguments can be passed to a function

Sr.No. Call Type & Description

1 Call by value

This method copies the actual value of an argument into the formal parameter
of the function. In this case, changes made to the parameter inside the function
have no effect on the argument.

2 Call by reference
This method copies the address of an argument into the formal parameter.
Inside the function, the address is used to access the actual argument used in
the call. This means that changes made to the parameter affect the argument.

You might also like