0% found this document useful (0 votes)
48 views58 pages

Unit-2.1 Control Statements

The document discusses control statements in C programming. It describes two types of control statements: conditional statements and unconditional statements. Conditional statements include if, switch, while, do-while, and for statements which allow conditional execution of code. Unconditional statements include goto, break, and continue which allow unconditional jumps in a program's flow. The document provides examples and explanations of how each control statement functions.

Uploaded by

anurag232003
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)
48 views58 pages

Unit-2.1 Control Statements

The document discusses control statements in C programming. It describes two types of control statements: conditional statements and unconditional statements. Conditional statements include if, switch, while, do-while, and for statements which allow conditional execution of code. Unconditional statements include goto, break, and continue which allow unconditional jumps in a program's flow. The document provides examples and explanations of how each control statement functions.

Uploaded by

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

A Control statement is a statement which is used to

control the flow of program execution

A Control statements are also called as Decision


Making Statements or Conditional Statement

We can use these to define the order in which the


instructions in a program must be executed.

They make it possible to make decisions, to perform tasks


repeatedly or to jump from one section of code to another
In C Programming there are majorly TWO types of
Control Statements[ Conditional Statements ]
These statements are used to decide what action to
take based on a condition

In C Programming there are TWO branching


statements
If statement is used to decide what action to take
based on a condition out of two actions

In C Programming there are four if statements


Syntax

Example
#include<stdio.h>
void main()
{
int a; F
a==10
printf(“Enter any number:”);
scanf(“%d”,&a);
if(a == 10) T
{
printf(“We are in if statement…\n”); 2 printf statements

printf(“value of ‘a’ is 10\n”);


}
printf(“We are out of if statement….”); 1 printf statement
}
Enter any number: 10
We are in if statement…
Value of a is 10
We are out of if statement….
The if-else statement is used to verify the given condition and executes only one out of
the two blocks of statements based on the condition result.
The if-else statement evaluates the specified condition. If it is TRUE, it executes a
block of statements (True block). If the condition is FALSE, it executes another block
of statements (False block).
#include<stdio.h>
void main()
{
int a; F
a==10
printf(“Enter any number:”);
scanf(“%d”,&a);
if(a == 10) T
{
printf(“Condition is TRUE…\n”); printf … T printf … F

}
else
printf… END
printf(“Condition is FALSE….”);
printf(“END”);
}
Enter any number: 10
Condition is TRUE…
END
Writing a if statement inside another if statement is called nested if
statement.
Writing a if statement inside else of an if statement is called if-else-if
statement.
What is an expression?
In any programming language, if we want to perform any calculation or
to frame any condition etc., we use a set of symbols to perform the task.
These set of symbols makes an expression.

An expression is a collection of operators and operands that


represents a specific value.
An operator is a symbol that performs tasks like arithmetic
operations, logical operations, and conditional operations, etc.

Operands are the values on which the operators perform the task.
Here operand can be a direct value or variable or address of memory
location.
Expression Types in C
In the C programming language, expressions are divided into
THREE types. They are as follows...

1. Infix Expression
2. Postfix Expression
3. Prefix Expression

The above classification is based on the operator position in


the expression.
1. Infix Expression
The expression in which the operator is used between
operands is called infix expression.
The infix expression has the following general structure.

Operand1 Operator Operand2


2.Postfix Expression
The expression in which the operator is used after operands
is called postfix expression.
The postfix expression has the following general structure.

Operand1 Operand2 Operator


3.Prefix Expression
The expression in which the operator is used before
operands is called a prefix expression.
The prefix expression has the following general structure.

Operator Operand1 Operand2


• Consider a situation in which we have many options out of which
we need to select only one option that is to be executed.

• Such kind of problems can be solved using nested if statement.


But as the number of options increases, the complexity of the
program also gets increased.

• This type of problem can be solved very easily using


a switch statement.
• Using the switch statement, one can select only one option from
more number of options very easily.

• In the switch statement, we provide a value that is to be compared


with a value associated with each option.

• Whenever the given value matches the value associated with an


option, the execution starts from that option. In the switch
statement, every option is defined as a case.
C programming language, there are circumstances
where we want to do the same thing many times

For example we want to print the same words


ten times.

we could type ten printf function, but it is easier to


use a loop. The only thing we have to do is to setup a
loop that execute the same printf function ten times
A loop is defined as a block of statements, which are
repeatedly executed for a certain number of times

Looping statements are used for running a set


of statements for any number of times

Looping statements are also called as iterative


statements
In C Programming there are mainly two
types of loops

Note

These are also called indefinite and


definite loops respectively
Unbounded looping statements are used when
we don’t know how many times the set of
statements has to be repeat
Unbounded looping statements can either be a
Pre – Test Loop or be a Post – Test Loop

In Pre – Test Loop, condition is checked before


the beginning of each iteration. If condition is
TRUE repetition is performed, if it is FALSE
repetition is not performed
Pre – Test loop is implemented using ‘while’ statement

In Post – Test Loop, first the block of statements to be


repeat is executed then condition will be tested

That means in Post – Test Loop, condition is checked


after executing the repeating statements, if the
condition is TRUE it repeat the statements again, if it is
FALSE repetition is not performed

Post – Test loop is implemented using ‘do -


while’ statement
The while statement is used to execute a single
statement or block of statements repeatedly as
long as the given condition is TRUE.

The while statement is also known as Entry


control looping statement.
The do-while statement is used to execute a
single statement or block of statements
repeatedly as long as given the condition is
TRUE.

The do-while statement is also known as the Exit


control looping statement.
The for statement is used to execute a single
statement or a block of statements repeatedly
as long as the given condition is TRUE.
The for statement has the following syntax.

Syntax for(initialization; condition; modification)


{
block of statements;
…..
…..
}
The for statement has the following execution
flow diagram.
1 2 3
for(initialization; condition; modification) 1
{
block of statements;
4
….. 2
…..
}
out of loop; 5 4

3
5
1 #include<stdio.h>
void main()
{
2 int i;
for(i=1; i<=10; i++)
{
4 printf(“MRIT\n”);

3 }
printf(“End”);
5 }
IMPORTANT POINTS TO BE REMEMBER
WHILE USING FOR LOOP STATEMENT
When we use for statement, we must follow the
following...
for is a keyword so it must be used only in lower case letters.
Every for statement must be provided with initialization,
condition, and modification (They can be empty but must be
separated with ";")
Ex: for ( ; ; ) or for ( ; condition ; modification ) or for ( ;
condition ; )
In for statement, the condition may be a direct integer value, a
variable or a condition.
The for statement can be an empty statement.
In above slides we are covered about the types of control
statements
1. if
2. switch
1. Conditional 3. while
4. do - while
5. for

1. goto
2. Unconditional 2. break
3. continue
This is used to jump from one line to any other line within
the program

Always goto statement will be associated with a ‘label’

Here ‘abc’ is a label


defined in the
program

Goto statement transfers the control from goto line to


specified label line
#include<stdio.h>
#include<conio.h>
void main()
{
int a,b,c;
char ch;
a=10;
b=20;
printf(“Enter + or - :”);
scanf(“%c”,&ch);
if(ch==‘+’)
goto add;
else
goto sub;
add: c=a+b;
sub: c=a-b;
printf(“Result = %d”,c);
}
This is used to jump from current line to the end of the block

Generally we use ‘break’ statement to terminate/skip from a


block of statements

Most common use of ‘break’ is to terminate loop, terminate


a switch case, etc.,
#include<stdio.h>
#include<conio.h>
void main() OUTPUT
{
int i=1; i=1
while(i<=10)
{
printf(“i = %d\n”,i);
break;
i++;
}
}
This is used to jump from current line to the beginning of
the loop

Generally we use ‘continue’ statement to skip the remaining


part in the loop and goes to next repetition (i.e., beginning
of the loop)
#include<stdio.h>
#include<conio.h>
void main()
{ OUTPUT
int i=1;
while(i<=10)
i=1
{ i=1
printf(“i = %d\n”,i); ……….
continue; Goes into infinite loop…..
i++;
}
}
IMPORTANT POINTS TO BE REMEMBER
When we use break, continue and goto statements, we
must follow the following…
The break is a keyword so it must be used only in lower case letters.
The break statement can not be used with if statement.
The break statement can be used only in switch case and looping
statements.
The break statement can be used with if statement, only if that if
statement is written inside the switch case or looping statements.
The continue is a keyword so it must be used only in lower case letters.
The continue statement is used only within looping statements.
The continue statement can be used with if statement, only if that if
statement is written inside the looping statements.
The goto is a keyword so it must be used only in lower case letters.
The goto statement must require a label.
The goto statement can be used with any statement like if, switch, while,
do-while, and for, etc.
There are mainly five types of errors exist in C programming:
SYNTAX ERROR

•Syntax errors are also known as the compilation errors as they


occurred at the compilation time, or we can say that the syntax
errors are thrown by the compilers.
•These errors are mainly occurred due to the mistakes while
typing or do not follow the syntax of the specified programming
language.
•These mistakes are generally made by beginners only because
they are new to the language. These errors can be easily
debugged or corrected.
•Commonly occurred syntax errors are:
•If we miss the parenthesis (}) while writing the code.
•Displaying the value of a variable without its declaration.
•If we miss the semicolon (;) at the end of the statement.
RUN-TIME ERROR

•Sometimes the errors exist during the execution-time


even after the successful compilation known as run-
time errors.
•When the program is running, and it is not able to
perform the operation is the main cause of the run-
time error.
•The division by zero is the common example of the
run-time error.
•These errors are very difficult to find, as the compiler
does not point to these errors.
LINKER ERROR
•Linker errors are mainly generated when the executable file of
the program is not created.
•This can be happened either due to the wrong function
prototyping or usage of the wrong header file.
•For example, the main.c file contains the sub() function whose
declaration and definition is done in some other file such
as func.c.
•During the compilation, the compiler finds the sub() function
in func.c file, so it generates two object files,
i.e., main.o and func.o. At the execution time, if the definition
of sub() function is not found in the func.o file, then the linker
error will be thrown.
•The most common linker error that occurs is that we
use Main() instead of main().
LOGICAL ERROR

•The logical error is an error that leads to an undesired


output.
•These errors produce the incorrect output, but they
are error-free, known as logical errors.
•These types of mistakes are mainly done by beginners.
•The occurrence of these errors mainly depends upon
the logical thinking of the developer.
•If the programmers sound logically good, then there
will be fewer chances of these errors.
SEMANTIC ERROR
•Semantic errors are the errors that occurred when the
statements are not understandable by the compiler.
•The following can be the cases for the semantic error:
•Use of a un-initialized variable.
int i;
i=i+2;
•Type compatibility
int b = “smarttechnology";
•Errors in expressions
int a, b, c;
a+b = c;
•Array index out of bound
int a[10];
a[10] = 34;
C CONSTRUCTS IN SOLVING PROBLEMS LIKE
GENERATING ARITHMETIC PROGRESSION.
• Arithmetic series is a sequence of terms in which
next term is obtained by adding common difference
to previous term.
• Let, tn be the nth term of AP, then (n+1)th term of can
be calculated as (n+1)th = tn + D
where D is the common difference (n+1)th - tn
The formula to calculate Nth term tn = a + (n – 1)d;
where, a is first term of AP and d is the common
difference.
C program to print arithmetic progression series and it's sum till N
terms
#include <stdio.h>
#include <stdlib.h>
int main() {
int first, diff, terms, value, sum=0, i;
printf("Enter the number of terms in AP series\n");
scanf("%d", &terms);
printf("Enter first term and common difference of AP series\n");
scanf("%d %d", &first, &diff);
/* print the series and add all elements to sum */
value = first;
printf("AP SERIES\n");
for(i = 0; i < terms; i++) {
printf("%d ", value);
sum += value;
value = value + diff;
}
printf("\nSum of the AP series till %d terms is %d\n", terms, sum);
getch();
return 0;
}
C CONSTRUCTS IN SOLVING PROBLEMS LIKE
GENERATING GEOMETRIC PROGRESSION.
• Write a program to read two numbers, x and n, and
then compute the sum of the geometric progression.
1+x+x2+x3+x4+……….+xn
And then, print x,n and sum.
#include <stdio.h>
#include <conio.h>
#include <math.h>
main()
{
int x,n,sum=0,i;
start:
printf("enter the values for x and n:");
scanf("%d%d",&x,&n);
if(n>0)
{
for(i=0;i<=n;i++)
{
sum = sum+pow(x,i);
}
printf("The sum of the geometric progression is:%d",sum);
}
else output
{ enter the values for x and n:4 5
printf("not a valid n:%d value",n); The sum of the geometric progression is:1365
getch();
goto start;
}}
Write a C Program to print numbers from 1 to
50?

Write a C Program to print EVEN numbers from


1 to 50?

Write a C Program to print Reverse of given


number?

Write a C Program to test a given numbers is


PRIME or not?
Write a C Program to check given number is
Palindrome or not?

Write a C Program to Fibonacci series of 10


elements?

Write a C Program to sum of all EVEN numbers


from 1 to 100?

Write a C Program to factorial of given number?

You might also like