[go: up one dir, main page]

0% found this document useful (0 votes)
53 views33 pages

PPS UNIT II Control Statements

Uploaded by

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

PPS UNIT II Control Statements

Uploaded by

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

UNIT-II

C CONTROLSTATEMENTS
Conditional Statements
Conditional Statements in C programming are used to make
decisions based on the conditions.
These statements are also known as decision making statements (or)
selection statements
There are the following conditional statement in C language.
1)If statement
2)If-else statement
3)Nested if statement
4) else-if ladder
5.Switch statement
1)If Statement:
The if statement is used to check some given condition and perform
some operations depending upon the correctness of that condition.
The single if statement in C language is used to execute the code if a
condition is true. It is also called one-way selection statement.
The syntax of the if statement is given below.
if(expression (or) condition)
{
Statement block;
} Entry
Statement x; False
Flowchart of if statement in C: condition

True

Statement -block

Statement -X
How "if" statement works..
• If the expression is evaluated to nonzero (true) then if block statement(s)
are executed.
• If the expression is evaluated to zero (false) then Control passes to the next
statement following it.
Ex:
#include <stdio.h>
int main ()
{
int a = 10;
if( a < 20 )
{
printf("a is less than 20\n" );
}
printf("value of a is : %d\n", a);
return 0;
}
2)If -else statement:
The if-else statement in C language is used to execute the code if condition is
true or false. It is also called two-way selection statement.
An if statement can be followed by an optional else statement, which executes
when the Boolean expression is false.
Syntax
The syntax of an if...else statement in C programming language is −
if(boolean_expression)
{
true block statements:
//statement(s) will execute if the boolean expression is true
}
else
{
false block statements;
//statement(s) will execute if the boolean expression is false
}
Statement –x;
conditio
n

true block statements


False block statements

Statement x
How "if..else" statement works:
 If the expression is evaluated to nonzero (true) then if block statement(s) are
executed.
 If the expression is evaluated to zero (false) then else block statement(s) are executed.
Ex:
#include<stdio.h>
void main()
{
int n;
printf("enter the number");
scanf("%d“,&n);
if(n%2==0)
{
printf("%d is an even integer.",n);
}
else
{
printf("%d is an odd number ",n);
}
}
3)Nested if statement:
The nested if...else statement is used when a program requires more than one
test expression. It is also called a multi-way selection statement.
Syntax: if( condition 1 )
{
if( condition2)
{
statement-block1;
}
else
{
statement-block 2;
}
}
else
{
statement-block 3;
}
#include<stdio.h>
void main( )
{
int a,b,c;
printf("Please Enter three numbers");
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
if(a>c)
{
printf("a is greatest");
}
else
{
printf("c is greatest");
}
}
else
{
if(b>c)
{
printf("b is greatest");
}
else
{
printf("c is greatest"); }
}
4.else- If ladder:
The if-else-if statement is used to execute one code from multiple conditions. It is also
called multipath decision statement. It is a chain of if..else statements in which
each if statement is associated with else if statement and last would be an else
statement.
• Syntax
if(condition1)
{
//statement1;
}
else if(condition2)
{
//statement2 ;
}
else if(condition3)
{
//statement3 ;
}
else
{ //statement4 ;}
#include<stdio.h>
int main()
{
int marks=83;
if(marks>75)
{
printf("First class");
}
Else if(marks>65)
{
printf("Second class");
}
else if(marks>55)
{
printf("Third class");
}
else
{
printf("Fourth class");
}}
5.Switch statement:
The switch statement is a multiway branch statement. It provides an easy way to dispatch
execution to different parts of code based on the value of the expression.
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.
Syntax:
switch( expression(or) variable)
{
case value-1:
statement Block-1;
break;
case value-2:
statement Block-2;
break;
.
.
default:
default statements;
}
How does the switch statement work?
1.The switch expression must be of an integer or character
type.
2.The case value must be an integer or character constant.
3.Case labels always end with a colon ( : ). Each of these
cases is associated with a block.
4.If there is a match, the corresponding statements after the
matching label are executed.
5.If there is no match, the default statements are executed.
6.If we do not use break, all statements after the matching
label are executed.
7.By the way, the default clause inside the switch statement
is optional.
// Program to create a simple calculator
#include <stdio.h>
int main()
{
char operator;
int n1, n2;
printf("Enter an operator (+, -, *, /): ");
scanf("%c", &operator);
printf("Enter two operands: ");
scanf("%d %d",&n1, &n2);
switch(operator)
{
case '+':
printf("%d + %d = %d",n1, n2, n1+n2);
break;
case '-':
printf("%d- %d= %d",n1, n2, n1-n2);
break;
case '*':
printf("%d* %d = %d",n1, n2, n1*n2);
break;
case '/':
printf("%d/ %d = %d",n1, n2, n1/n2);
break;
// operator doesn't match any case constant +, -, *, /
default:
printf("Error! operator is not correct");
}
return 0;
}
LOOPS
In computer programming, a loop is a sequence of instructions that is
repeated until a certain condition is reached.
(OR)
The looping can be defined as repeating the same process multiple
times until a specific condition satisfies.
Why use loops in C language?
The looping simplifies the complex problems into the easy ones. It
enables us to alter the flow of the program so that instead of writing
the same code again and again, we can repeat the same code for a
finite number of times.
Advantage of loops in C:
• It provides code reusability.
• Using loops, we do not need to write the same code again and again.
• Using loops, we can traverse over the elements of data structures
(array or linked lists).
Types of C Loops:
There are three types of loops in C language
1.For loop
2.While loop
3.do while loop
There are mainly two types of loops:
• Entry Controlled loops: In this type of loops the test
condition is tested before entering the loop body. For
Loop and While Loop are entry controlled loops.
• Exit Controlled Loops: In this type of loops the test
condition is tested or evaluated at the end of loop body.
Therefore, the loop body will execute atleast once,
irrespective of whether the test condition is true or
false. do – while loop is exit controlled loop.
1.For loop:
The for loop is used in the case where we need to execute some
part of the code until the given condition is satisfied. The for
loop is also called as a pre-tested loop.

The syntax of for loop in c language is given below:

for (initialization ; condition; inc/dec)


{
// body of the loop ;
// statements we want to execute
}
How for loop works?
• The initialization statement is executed only once.
• Then, the test expression is evaluated. If the test
expression is evaluated to false, the for loop is
terminated.
• However, if the test expression is evaluated to
true, statements inside the body of for loop are
executed, and the update expression is updated.
• Again the test expression is evaluated.
This process goes on until the test expression is
false. When the test expression is false, the loop
terminates.
Example 1: for loop
// Print numbers from 1 to 10
#include <stdio.h>
int main()
{
int i;
for (i = 1; i < 11; i++)
{
printf("%d ", i);
}
return 0;
}
Output
1 2 3 4 5 6 7 8 9 10
2.While loop:
Repeats a statement or group of statements while a given condition is true. It
tests the condition before executing the loop body.
It is also called a pre-tested loop.
The syntax of while loop in c language is given below:
Initialization;
while(condition)
{
body of the loop;
Updation;
}
How while loop works?
• The while loop evaluates the test expression inside
the parenthesis ().
• If the test expression is true, statements inside the
body of while loop are executed. Then, the test
expression is evaluated again.
• The process goes on until the test expression is
evaluated to false.
• If the test expression is false, the loop terminates
(ends).
Example 1: while loop
// Print numbers from 1 to 5
#include <stdio.h>
int main()
{
int i = 1;
while (i <= 5)
{
printf("%d", i);
i++;
}
return 0; o/p:1 2 3 4 5
}
3.do...while loop
The do..while loop is similar to the while loop with one
important difference. The body of do...while loop is executed
at least once. Only then, the test expression is evaluated.
The syntax of the do...while loop is:
Initialization;
do
{
// statements inside the body of the loop
updation;
} while (condition);
How do...while loop works?
• The body of do...while loop is executed once.
Only then, the test expression is evaluated.
• If the test expression is true, the body of the
loop is executed again and the test expression
is evaluated.
• This process goes on until the test expression
becomes false.
• If the test expression is false, the loop ends.
Example 1: do...while loop
#include <stdio.h>
int main()
{
int i= 1;
do
{
printf( “%d\n“,i );
i ++;
}
while(i <= 10);
return 0;
}
C break statement:
 The break is a keyword in C which is used to bring the program
control out of the loop. The break statement is used inside loops or
switch statement. The break statement breaks the loop one by one,
i.e., in the case of nested loops, it breaks the inner loop first and then
proceeds to outer loops. The break statement in C can be used in the
following two scenarios:
1.With switch case
2.With loop
 The Break statement in C Programming is very useful to exit from
any loop such as For Loop, While Loop, and Do While. While
executing these loops, if the C compiler finds the break statement
inside them, then the loop will stop running the statements and
immediately exit from the loop.
which is used to terminate the loop
Syntax:
break;
EX:
void main ()
{
int i;
for(i = 0; i<10; i++)
{
printf("%d ",i);
if(i == 5)
break;
}
printf("came outside of loop i = %d",i);
}
Continue statement:
 The continue statement is used inside loops. When a continue
statement is encountered inside a loop, control jumps to the
beginning of the loop for next iteration, skipping the execution of
statements inside the body of loop for the current iteration .
 The continue statement in C language is used to bring the
program control to the beginning of the loop. The continue
statement skips some lines of code inside the loop and
continues with the next iteration
Syntax:
continue;
EX:
#include<stdio.h>
int main()
{
int i;
for(i=1;i<=10;i++)
{
if(i==5)
{ //if value of i is equal to 5, it will continue the loop
continue;
}
printf("%d \n",i);
}
return 0;
}
goto statement:
 The goto statement is known as jump statement in C.
 goto statement is used to transfer the program control to a
predefined label.
 When a goto statement is encountered in a C program, the
control jumps directly to the label mentioned in the goto
statement
 The goto statement is rarely used because it makes program
confusing, less readable and complex.
Syntax:
label:
statements ; //some part of the code;
goto label;
#include <stdio.h>
int main()
{
int num, i=1;
printf("Enter the number\n”);
scanf("%d", &num);
table:
printf("%d x %d = %d\n", num,i,num*i);
i++;
if(i<=10)
goto table;
}
The goto statment can be used to repeat some part of the code
for a particular condition.

You might also like