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

Unit 2

The document provides an overview of control statements in C, categorizing them into sequence, selection, looping, and unconditional statements. It details various types of selection statements like if, if-else, nested if-else, if-else ladder, and switch-case, as well as looping constructs such as while, do-while, and for loops. Additionally, it explains unconditional statements including break, continue, goto, and exit, along with their syntax and examples.

Uploaded by

kokococo285
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 views15 pages

Unit 2

The document provides an overview of control statements in C, categorizing them into sequence, selection, looping, and unconditional statements. It details various types of selection statements like if, if-else, nested if-else, if-else ladder, and switch-case, as well as looping constructs such as while, do-while, and for loops. Additionally, it explains unconditional statements including break, continue, goto, and exit, along with their syntax and examples.

Uploaded by

kokococo285
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 2

Control statements
⚫ The statement of C are broadly classified into following categories:
1. Sequence – Statements are executed one after another.
2. Selection – Selects the path of execution based on a condition.
3. Looping – Permits repetitive execution of statements till a condition is satisfied.
4. Unconditional or Jump Statements
Types of Control Statements:
Control Statements

Conditional Unconditional

Selection (decision) Looping goto break Continue exit()

if switch for do-while while


Sequence:
⚫ In sequence construct, statements are executed one by one in the order from top to bottom.
⚫ Program:
#include<stdio.h>
main( )
{
int num1,num2,prod;
printf(“Enter two numbers:”);
scanf(“%d%d”, &num1,&num2);
prod=num1 * num2;
printf(“\nThe product of %d and %d is “, num1, num2);

Selection or Decision making,(OR) Branching Statements in C

C has some kinds of statements that permit the execution of a single statement, or a block of statements, based
on the value of a conditional expression or selection among several statements based on the value of a conditional
expression or a control variable.

These are all the following conditional statements


(i) if statement
(ii) if-else statement
(iii) Nested if-else statement
(iv) else if ladder
(v) switch
(i) if statement:
It is otherwise known as One-way decisions. It is used to control the flow of execution of the statements. The
decision is based on a ‘test expression or condition’ that evaluates to either true or false.
✓ If the test condition is true, the corresponding statement is executed.
✓ If the test condition is false, control goes to the next executable statement.

Syntax: Flowchart if(condition is true)

Statement 1;

Statement n;
}

Next Statement;
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int m,n,a;
clrscr();
printf(“Enter 2 numbers:”);
scanf(“%d%d”,&m,&n); if(m>n)
{
a=m;
m=n;
n=a;
}
printf(“The interchanged values are: “%d%d”,m,n);getch();
}
(ii) if-else statement:
It is otherwise known as Two-way decisions. It is handled with if-else statements. Thedecision is based
on a ‘test expression or condition’ that evaluates to either true or false.
✓ If the test condition is true, the true block will be executed then control goes to the next executable
statement.
✓ If the test condition is false, the false block will be executed control goes to the next executable
statement.
Syntax: Flowchart

if(condition is true)
{
True block;
}

else
{
false block;
}

Next statement;

Program:
#include<stdio.h>
main()
{
int a,b;
printf(“Enter two numbers:”);
scanf(“%d%d”,&a,&b); if(a>b)
{
printf(“A is largest”);
}
else
{
printf(“B is largest”);
}

getch();
}
OUTPUT:

Enter two numbers:12 5A is


largest

(iii) Nested if-else statement


It is otherwise known as Two-way with sub-way decisions. If else statement is enclosed within another if
else structure. An if statement can be followed by an optional else if...else statement, which is very useful to test
various conditions using single if...else if statement

Syntax: Flowchart
if(condition 1)
{
if(condition 2)
{
True statement 2
}
else
{
False statement 2;
}
}

else
{
False statement 1;
}

Next Statement;
Program:
#include<stdio.h>
main()
{
int a b c;
printf(“Enter the value for A, B and C:”);
scanf(“%d%d%d”,&a,&b,&c);
if((a>b)&&(a>c))
{
printf(“A is largest”);
}
else
{
if(b>c)
{
printf(“B is largest”);
}
else
{
printf(“C is largest”);
}
}
}
Output:

Enter the value for A,B and C:12 13 5B is


largest

(iv) if – else ladder


It is otherwise known as Multi-way decisions. Each and every else block will have ifstatement. Last
else block cannot have if block. Last else will have default statement.

Syntax Flowchart

if(condition1)
statement 1;
else if(condition 2)
statement 2;
else if(condition 3)
statement 3;

else
default statement;

Example:

--------

if(per >= 60)


printf(" You got 1st Class");else
if( per >= 45)
printf(" You got 2nd Class ");else
if(per>=27)
printf("\n You got 3rd Class ");
else
printf("\n NO Class ");

(v) switch() case statement:


It is an alternative solution for else-if ladder concept. Switch statement is used to executea particular
group of statements from several groups of statements. Switch statement in C tests the value
of a variable and compares it with multiple cases. Once the case match is found, a
block of statements associated with that particular case is executed.

Each case in a block of a switch has a different name/number which is referred to


as an identifier. The value provided by the user is compared with all the cases
inside the switch block until the match is found.

If a case match is NOT found, then the default statement is executed, and the control
goes out of the switch block.

Advantages of Using Switch statement


✓ Easier to debug
✓ Faster execution potential
✓ Easier to read
✓ Easier to understand
✓ Easier to maintain

Syntax
switch( expression )
{
case value-1:
Block-1;
Break;
case value-2:
Block-2;
Break;
case value-n:
Block-n;
Break;
default:
default Block;
Break;
}
Statement-x;
How switch works
Program: // C program to print the day using switch
#include <stdio.h>
int main()
{
int day;

printf("The day with number is ");


scanf(“%d”,&day);
switch (day) {
case 1:
printf("Monday");
break;
case 2:
printf("Tuesday");
break;
case 3:
printf("Wednesday");
break;
case 4:
printf("Thursday");
break;
case 5:
printf("Friday");
break;
case 6:
printf("Saturday");
break;
case 7:
printf("Sunday");
break;
default:
printf("Invalid Input");
break;
}
return 0;
}
LOOPING STATEMENTS IN C
A loop is defined as a block of statements which are repeatedly executed for certainnumber of times.
A loop can either be a pre-test loop or be a post-test loop i.e. entry controlled loop or exit controlled loop.

The following steps are should need to construct the looping concept in a program.
➢ Initialization of a counter variable
o It is a variable used in the loop
➢ Test condition
➢ Body of the loop
o Block of statements depends on the test condition
➢ Updating the counter variable
o Example: Increment/Decrement
There are three types of looping, namely:
1. while loop
2. do-while loop
3. for loop

(i) While loop:


It is an entry control loop statement. The test condition is evaluated and if it is true, the body of the loop is
executed. The test condition is repetitively checked and if it is true the body is executed. The process of execution of
the body will continue till the test condition becomes true. The control is transferred out of the loop if the test condition
becomes false.
Syntax:

Flow Diagram of while loop

Example
Program :
….

#include<stdio.h>
main()
{

int i=1;
while(i<=10)
{
}
Output:
p intf(“%d\t”,i);i++;
r }
1 2 3 4 5 6 7 8 9 10

ii)do… while Loop

The do while loop is a post tested loop. Using the do-while loop, we can repeat the execution of several parts of the
statements. The do-while loop is mainly used in the case where we need to execute the loop at least once.
Syntax:

Flow diagram of do while loop

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

iii)for loop:
For loop is another repetitive control structure and it is used to execute set ofinstructions
repeatedly until the condition becomes false.
Syntax of for loop:

for (initialization; condition test; increment or decrement)


{
//Statements to be executed repeatedly
}
When we are working with for loop always execution process will start from the initialization block. After the
initialization block, the control will pass to the condition block. If the condition is evaluated as true then control
will pass to the statement block.
After execution of the statement block, the control will pass to the iteration block, from iteration it will pass
back to the condition. Always repetition will happen beginning condition, statement block, and iteration only.
The initialization block will be executed only once when we are entering the loop the first time.

Flow Diagram of For loop

Example of For loop


#include <stdio.h>
int main()
{
int i;
for (i=1; i<=3; i++)
{
printf("%d\n", i);
}
return 0;
}
Output:

1
2
3
UNCONDITIONAL (or) JUMP STATEMENTS IN C
(i) Break
(ii) Continue
(iii) Goto
(iv)exit

1.break statement:
1. It is used to terminate the loop. When the keyword break is used inside any‘C’
loop,control automatically transferred to first statement after the loop.
2. A break is usually associated with an if statement.
Syntax:
break;
Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i; for(i=1;i<=10;i++)
{
if(i==6) break;
printf(“%d”,i);

}
}

Output:

12345
2.Continue statement
When the statements continue is used inside any ‘C’ loop, control automatically passes to the beginning of
the loop.
It is also associated with if statement.
Syntax:
continue;

Program:
#include<stdio.h>
#include<conio.h>
main()
{
int i,n,sum==0;
for(i=1;i<=5;i++)
{
print(“Enter any number…\n”);
scanf(“%d”,&n);
if(n<0)
continue;
else
sum=sum+n;
}

printf(“sum is…%d”,sum);
}
Output:

Enter any number…10


Enter any number…15
Enter any number…25
Enter any number…10
Enter any number…50
Sum is …100

3.Goto statement
✓ It is used to transfer control unconditionally from one place to another place.
✓ A goto statement can cause program control almost anywhere in the program
unconditionally.
✓ It requires a label to identify the place to move the execution.
✓ A label is a valid variable name and must be ended with colon (:).
Syntax:
goto label;
………
………
label:

label:
…….
…….
goto label;

Program:
#include<stdio.h>
main()
{
int a,b;
printf(“Enter the numbers”);
scanf(“%%d”,&a,&b); if(a==b)
goto equal;
else
{
printf(“\n A and B are not equal”);
exit(0);
}

equal: printf(“A and B are equal”);

The exit statement

The exit function in c is defined under the stdlib. h header file is used to
terminate the function currently running. Parameters of exit() in C

The exit() function in C only takes a single parameter status which is the exit code that is
returned to the caller i.e. either the operating system or parent process. There are two valid
values we can use as the status each having a different meaning. They are as follows:
• 0 or EXIT_SUCCESS: 0 or EXIT_SUCCESS means that the program has been
successfully executed without encountering any error.
• 1 or EXIT_FAILURE: 1 or EXIT_FAILURE means that the program has encountered an
error and could be executed successfully.’
Example :
#include <stdio.h>
#include <stdlib.>
int main ()
{
printf ( " Start the execution of the program. \n");
printf (" Exit from the program. \n ");
// use exit (0) function to successfully execute the program
exit (0);
printf ( "Terminate the execution of the program.\n ");
return 0;
}

You might also like