Download as pptx, pdf, or txt
Download as pptx, pdf, or txt
You are on page 1of 34

Jumping Statements

• Jump statements in C are used to interrupt the


flow of the program or escape a particular
section of the program.
• There are many more operations they can
perform within the loops, switch statements,
and functions.
• .
• The main uses of jump statements are to exit the loops
like for, while, do-while also switch case and executes the
given or next block of the code, skip the iterations of the
loop, change the control flow to specific location, etc.
• Types of Jump Statement in C
• There are 4 types of Jump statements in C language.
 Break Statement
 Continue Statement
 Goto Statement
 Return Statement.
Break Statement in C

• Break statement exits the loops like for, while,


do-while immediately, brings it out of the
loop, and starts executing the next block. It
also terminates the switch statement.
Syntax of Break Statement

• The break statement's syntax is simply to use


the break keyword.
• Syntax:
• //specific condition
break;
Example
#include<stdio.h>

void main() {

int a = 1; //initialize value to a

while (a <= 10) // run loop unless the value is 10


{
if (a == 3) // if the value is 3
break; //break the loop

printf("Print=%d \n", a);


a++;
}

printf("Outside loop"); //print statement outside the loop


}
Output
Print=1
Print=2
Outside loop
Continue Statement in C
• Continue in jump statement in C skips the specific iteration in
the loop. It is similar to the break statement, but instead of
terminating the whole loop, it skips the current iteration and
continues from the next iteration in the same loop.
• It brings the control of a program to the beginning of the loop.
Syntax of continue Statement

The syntax for the continue statement is


simple continue. It is used below the specified
continue condition in the loop.
• Syntax:
• continue;
#include<stdio.h>

int main()
{
int i;
for ( i = 1; i <= 7; i++ )
{
if( i == 3) //continue condition
{
continue; //continue statement
}

printf("Value = %d \n",i);
}
}
output
Value = 1
Value = 2
Value = 4
Value = 5
Value = 6
Value = 7
Goto Statement in C

• Goto statement is somewhat similar to the continue


statement in the jump statement in C, but the continue
statement can only be used in loops, whereas Goto can
be used anywhere in the program, but what the continue
statement does it skips the current iteration of the loop
and goes to the next iteration, but in the goto statement
we can specify where the program control should go
after skipping.
• The concept of label is used in this statement to tell the
program control where to go. The jump in the program
that goto take is within the same function.
Syntax
goto label;
....
....
label:
--code--
--code--
#include<stdio.h>

int main() {

int a;
printf("\nEnter a Positive int:");
scanf("%d", & a);

if (a % 2 == 0) //logic of even no
goto Even; //goto statement 1
else
goto Odd; //goto statement 2

Even: // label 1
printf("Number is Even\n");
exit(0);

Odd: //label2
printf("Number is Odd\n");

return 0;
}
Output
Enter a Positive int:4
Number is Even
Reasons to avoid goto statements

• If you are using goto, it is hard for the other


person to trace the flow of the program and
understand it.
• The program becomes hard to modify because
it references the different labels, so rewriting
is the only solution.
• The disadvantage of goto is that it could only
be used inside the same function.
Return Statement in C

• The return statement is a type of jump


statement in C which is used in a function to
end it or terminate it immediately with or
without value and returns the flow of program
execution to the start from where it is called.
• The function declared with void type does not
return any value.
• Syntax
return expression;
or
return;
#include <stdio.h>

#define BORN 2000

int age(int current);

int main(void)
{
int current = 2022;
printf("Age: %d", age(current));
return 0;
}

int age(int current) {


return current - BORN; //return age
}
Advantages

• You can control the program flow or alter the


follow of the program.
• Skipping the unnecessary code can be done
using jump statements.
• You can decide when to break out of the
loop using break statements.
• You get some sort of flexibility with your
code using jump statements.
Disadvantages

• Readability of the code is disturbed because


there are jumps from one part of the code to
another part.
• Debugging becomes a little difficult.
• Modification of the code becomes difficult.
• Thanks
Expressions
• Expressions are the combination of variables,
operands, and operators. The result would be
stored in the variable once the expressions are
processed based on the operator's precedence.
Example:
 c=a+b
 a-b+c
 a+b-(a*c)
Types of Expressions in C

• Arithmetic expressions
• Relational expressions
• Logical expressions
• Conditional expressions
Each type of expression takes certain types of
operands and uses a specific set of operators.
Evaluation of a particular expression produces
a specific value.
• Arithmetic Expressions
• An arithmetic expression is an expression that
consists of operands and arithmetic operators.
An arithmetic expression computes a value of
type int, float or double.
• Example
1+8*2
Relational Expressions

• A relational expression is an expression used to


compare two operands.
• It is a condition which is used to decide whether the
action should be taken or not.
• In relational expressions, a numeric value cannot be
compared with the string value.
• The result of the relational expression can be either zero
or non-zero value. Here, the zero value is equivalent to a
false and non-zero value is equivalent to true.
• Example : a>=9
Logical Expressions

• A logical expression is an expression that


computes either a zero or non-zero value.
• It is a complex test condition to take a
decision.
• Example
• ! ( x > 10 ) && ( y = = 2 )
#include <stdio.h>
int main()
{
int x = 4;
int y = 9;
if ( (x <6) || (y>10))
{
printf("Condition is true");
}
else
printf("Condition is false");
return 0;
}
Conditional Expressions

• A conditional expression is an expression that


returns 1 if the condition is true otherwise 0.
• A conditional operator is also known as a
ternary operator.
• The Syntax of Conditional operator
• Suppose exp1, exp2 and exp3 are three
expressions.
• exp1 ? exp2 : exp3
• The above expression is a conditional
expression which is evaluated on the basis of
the value of the exp1 expression. If the
condition of the expression exp1 holds true,
then the final conditional expression is
represented by exp2 otherwise represented
by exp3
• #include<stdio.h>
• #include<string.h>
• int main()
• {
• int age = 18;
• char status;
• status = (age>18) ? 'V': 'N';
• if(status == 'v')
• printf("Vote");
• else
• printf("Not Applicable");
• return 0;
• }
• Thanks

You might also like