0% found this document useful (0 votes)
36 views51 pages

Unit 5 (Control Statements)

Uploaded by

rishavkhadka43
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)
36 views51 pages

Unit 5 (Control Statements)

Uploaded by

rishavkhadka43
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/ 51

C Programming (CSC110)

Sulav Nepal
Email: nep.sulav@live.com
Contact No.: 9849194892
Master’s in Computer Information System (MCIS) – Pokhara University
Bachelor of Science. Computer Science & Information Technology (B.Sc. CSIT) – Tribhuwan University
Microsoft Technology Associate (MTA): Windows Server Administration Fundamentals
Microsoft Certified Technology Specialist (MCTS): Windows Server 2008 R2, Server Virtualization
Microsoft Specialist (MS): Programming in HTML5 with JavaScript and CSS3
Microsoft Students Partner (MSP) 2012 for Nepal
P
R
E Syllabus
P
A
R
 UNIT 5: Control Statement
E o Conditional Statements
D

B
Y o Decision Making and Branching (if, if else, nested if else, else if ladder,
switch statements)
S
U
L
A
o Decision Making and Looping (for, while, do while loops)
V

N o Exit function
E
P
A o Break and Continue
L

6/28/2022 6:11 AM
P
R
E
P
A
R

UNIT 5
E
D

B
Y

S CONTROL STATEMENT
U
L
A
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E Error Types in C Programming
P
A
R
 While writing C programs, errors may occur which are also known as
E bugs.
D

B
Y  Errors may occur unwillingly which may prevent the program to
S compile and run correctly as per the expectation of the programmer.
U
L
A
V
 Basically there are three types of errors in C programming:
 Runtime Errors
N
E  Compile Errors
P
A  Logical Errors
L

6/28/2022 6:11 AM
P
R
E Runtime Errors in C
P
A
R
 C runtime errors are those errors that occur during the execution of a
E C program and generally occur due to some illegal operation
D
performed in the program.
B
Y

S  Examples of some illegal operations that may produce runtime errors


U are:
L
A  Dividing a number by zero
V
 Trying to open a file which does not exist
N  Lack of free memory space
E
P
A
L

6/28/2022 6:11 AM
P
R
E Compile Errors in C
P
A
R
 Compile errors are those errors that occur at the time of compilation
E of the program.
D

B
Y  C compile errors may be further classified as:
S  Syntax Errors
U  Example: 𝑖𝑛𝑡 𝑎, 𝑏:
L
A  This will produce syntax error as the statement is terminated with ∶ rather than ;
V

N  Semantic Errors
E
P  Example: 𝑏 + 𝑐 = 𝑎;
A  Here, we are trying to assign a value of 𝑎 in the value obtained by adding 𝑏 and 𝑐
L which has no meaning in C.
 The correct statement will be: 𝑎 = 𝑏 + 𝑐; 6/28/2022 6:11 AM
P
R
E Logical Errors in C
P
A
R
 Logical errors are the errors in the output of the program.
E
D
 The presence of logical errors leads to undesired or incorrect output.
B
Y

S  These errors are caused due to error in the logic applied in the
U
L program to produce the desired output.
A
V

N  Logical errors could not be detected by the compiler, and thus,


E
P
programmers has to check the entire coding of a C program line by
A line.
L

6/28/2022 6:11 AM
P
R
E Control Statements
P
A  The statement which alter the flow of execution of the program are known
R
E as control statements.
D

B  Sometimes we have to do certain tasks depending on whether a condition is


Y true or not.
S
U
L  Similarly, it is necessary to perform repeated actions or skip some
A statements.
V

N  For these operations, control statements are needed.


E
P
A
L  There are two types of control statements:
 Decision Making (or branching) Statements
6/28/2022 6:11 AM
 Loop or Repeating Construct
P
R
E Control Statements
P
A  Decision Making (or branching) Statements
R
E
 If statement
D  If…Else statement
 Else…If statement
B
Y
 Nested If…Else statement
 Switch statement
S
U
L  Loop or Repeating Construct
A  For loop
V  While loop
N  Do…While loop
E
P
A  NOTE:
L  Branching is deciding what actions to take
 Looping is deciding how many times to take a certain action
6/28/2022 6:11 AM
P
R
E Decision Making in C
P
A  Programmer specifies one or more
R
E
conditions to be evaluated.
D

B  If the condition is determined to be true, a


Y statement is executed.

S
U  If the condition is determined to be false,
L the other statements is to be executed.
A
V
 Typical decision making structure found
N
E in most of the programming language is:
P
A
L

6/28/2022 6:11 AM
P
R
E if Statement
P
A
R
 An if statement consists of a
E Boolean expression followed by
D
one or more statements.
B
Y

S  Syntax:
U if(boolean_expression)
L
A {
V
/* statement(s) will execute if the
N Boolean expression is true */
E
P }
A
L

6/28/2022 6:11 AM
P
R
E if Statement
P
A
R
 If the Boolean expression evaluates to true, then the block of code
E inside the “if ” statement will be executed.
D

B
Y  If the Boolean expression evaluates to false, then the first set of code
S after the end of “if ” statement will be executed.
U
L
A
V
 C programming language assumes any non-zero and non-null values
as true and if it is either zero or null, then it is assumed as false
N
E value.
P
A
L

6/28/2022 6:11 AM
P
R
E Example - 1
P
A
R
#include<stdio.h>
E #include<conio.h>
D
void main()
B
Y {
S
int a=10;
U if(a<20)
L
A {
V
printf("a is less than 20\n");
N
E
}
P
A
printf("Value of a is: %d\n", a);
L getch();
} 6/28/2022 6:11 AM
P
R
E if…else Statement
P
A
R
 An if statement can be followed by an optional else statement, which
E executes when the Boolean expression is false.
D

B
Y  Syntax:
S if(boolean_expression)
U
L
{ /* statement(s) will execute if the Boolean expression is true */
A }
V
else
N
E
{ /* statement(s) will execute if the Boolean expression is false */
P }
A
L

6/28/2022 6:11 AM
P
R
E if…else Statement
P
A
R
E
D

B
Y

S
U
L
A
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E if…else Statement
P
A
R
 If the Boolean expression evaluates to true, then the if block will be
E executed, otherwise, the else block will be executed.
D

B
Y  C programming language assumes any non-zero and non-null values
S as true and if it is either zero or null, then it is assumed as false
U value.
L
A
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E Example - 2
P
A #include<stdio.h>
R #include<conio.h>
E
D void main()
{
B int a=100;
Y
if(a<20)
S {
U printf("a is less than 20\n");
L
A
}
V else
{
N
E
printf("a is not less than 20\n");
P }
A printf("Value of a is %d", a);
L
getch();
} 6/28/2022 6:11 AM
P
R
E if…else if…else Statement
P
A  An if statement can be followed by an optional else if . . . else
R
E statement, which is very useful to test various conditions.
D

B  Syntax:
Y
if(boolean_expression1)
S
U
{ /* statement(s) will execute if the Boolean expression 1 is true */
L }
A
V else if(boolean_expression2)
{ /* statement(s) will execute if the Boolean expression 2 is true */
N
E }
P else
A
L { /* statement(s) will execute if both Boolean expressions are false */
}
6/28/2022 6:11 AM
P
R
E Example - 3
P
A #include<stdio.h>
#include<conio.h>
R
void main()
E
{
D int a=100;
if(a==10)
B {
Y printf("Value of a is 10\n");
}
S else if(a==20)
U {
L printf("Value of a js 20\n");
A }
else if(a==30)
V
{
printf("Value of a is 30\n");
N }
E else
P {
A printf("None of the values are matching\n");
L }
printf("Exact value of a is %d\n",a);
getch(); 6/28/2022 6:11 AM
}
P
R
E Nested if Statement
P
A
R
 It is always legal in C programming to nest if-else statements, i.e. you
E can use one if or else if statement inside another if or else if
D
statement(s).
B
Y

S  Syntax:
U if(boolean_expression1)
L
A { /* statement(s) will execute if the Boolean expression 1 is true */
V
if(boolean_expression2)
N
E
{/* statement(s) will execute if the Boolean expression 2 is true */
P }
A
L }
6/28/2022 6:11 AM
P
R
E Example - 4
P
A #include<stdio.h>
R #include<conio.h>
E
D void main()
{
B int a=100, b=200;
Y
if(a==100)
S {
U if(b==200)
L
A
{
V printf("Value of a is 100 & b is 200\n");
}
N
E
}
P printf("Exact value of a is: %d\n",a);
A printf("Exact value of b is: %d\n",b);
L
getch();
} 6/28/2022 6:11 AM
P
R
E Loop Control Statements
P
A
R
 Loop control statements change execution from its normal sequence.
E
D
 When execution leaves a scope, all automatic objects that were
B
Y created in that scope are destroyed.
S
U
L  C supports the following control statements:
A
V  break statement
 continue statement
N
E  goto statement
P
A
L

6/28/2022 6:11 AM
P
R
E Break Statement
P
A
R
 The break statement in C programming has the following two usages:
E  When a break statement is encountered inside a loop, the loop is
D
immediately terminated and the program control resumes at the next
B statement following the loop.
Y

S
U  It can be used to terminate a case in the switch statement.
L
A
V
 Syntax:
N break;
E
P
A
L

6/28/2022 6:11 AM
P
R
E Example - 5
P
A #include<stdio.h>
R #include<conio.h>
E
D void main()
{
B int a=10;
Y
while(a<20)
S {
U printf("Value of a: %d\n",a);
L
A
a++;
V if(a>15)
{
N
E
break;
P }
A }
L
getch();
} 6/28/2022 6:11 AM
P
R
E Continue Statement
P
A  The continue statement in C programming works somewhat like the break
R
E statement.
D

B  Instead of forcing termination, it forces the next iteration of the loop to


Y take place, skipping any code in between.
S
U
L  For the for loop, continue statement causes the conditional test and
A increment portions of the loop to execute.
V

N  For the while and do…while loops, continue statement causes the program
E
P control to pass to the conditional tests.
A
L
 Syntax:
6/28/2022 6:11 AM
continue;
P
R
E Example - 6
P
A #include<stdio.h>
R
E int main()
D
{
B int i=1; //initializing a local variable
Y
//starting a loop from 1 to 10
S for(i=1;i<=10;i++)
U
L
{
A if(i==5)
V { //if value of i is equal to 5, it will continue the loop
continue;
N
E }
P printf("%d \n",i);
A } //end of for loop
L
return 0;
} 6/28/2022 6:11 AM
P
R
E break vs. continue
P
A
R
break continue
E
D The break statement is used to terminate the
The continue statement is used to bypass the
control from the switch…case structure as well in
B execution of the further statements.
the loop.
Y
When the break statement is encountered, it
When the continue statement is encountered, it
S terminates the execution of the entire loop or
bypasses single pass of the loop.
U switch case.
L
It doesn’t bypass the current loop though it
A It is used to bypass current pass through a loop.
V transfers the control out of the loop.
The loop doesn’t terminate when continue is
N The loop terminates when a break is encountered.
encountered.
E
P Syntax: break; Syntax: continue;
A
L

6/28/2022 6:11 AM
P
R
E goto Statement
P
A
R
 To alter the normal sequence of program execution by
E unconditionally transferring control to some other part of the
D
program.
B
Y

S  General expression:
U  goto label:
L
A  Here, label is an identifier used to label the target statement to which the control
V would be transferred.
N
E
P  Generally the use of goto statement is avoided as it makes program
A
L
illegible.
6/28/2022 6:11 AM
P
R
E goto Statement
P
A
R
 This statement is used in unique situations like:
E  Branching around statements or group of statements under certain
D
conditions.
B
Y
 Jumping to the end of a loop under certain conditions, thus bypassing
S
U the remainder of the loop during current pass.
L
A
V  Jumping completely out of the loop under certain conditions,
N
terminating the execution of a loop.
E
P
A
L

6/28/2022 6:11 AM
P
R
E Example - 7
P
A /* Program to calculate the sum and average of positive numbers. If the user enters a negative number, the sum and
R average are displayed */
E #include <stdio.h>
D int main()
{
B int maxInput = 100,i;
Y double number, average, sum = 0.0;
for (i = 1; i <= maxInput; ++i)
S {
U printf("%d. Enter a number: ", i);
scanf("%lf", &number);
L
if (number < 0.0)
A {
V goto jump; // go to jump if the user enters a negative number
}
N sum += number;
E }
P jump:
A average = sum / (i - 1);
L printf("Sum = %.2f\n", sum);
printf("Average = %.2f", average);
return 0;
6/28/2022 6:11 AM
}
P
R
E Switch Statement
P
A  Switch statement allows a program to select one statement for execution of a set of alternatives.
R
E  Only one of the possible statements will be executed, the remaining statements will be skipped.
D
 The multiple usage of if…else statement increases the complexity of the program, hard to read, and difficult to follow the
program.
B
Y
 Switch statement removes these disadvantages by using a simple and straight forward approach.
S
 Syntax:
U
switch(expression)
L { case caseConstant1:
A statement(s);
V break;
case caseConstant2:
N statement(s);
break;
E
.
P .
A .
L default:
statement;
} 6/28/2022 6:11 AM
P
R
E Switch Statement
P
A  The following rules apply to a switch statement:
R  The expression used in a switch statement must have an integral or enumerated type.
E
D  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.
B
Y  The caseConstant for a case must be the same data type as the variable in the switch, and it must be a
constant or a literal.
S
U  When the variable being switched on is equal to a case, the statements following that case will execute
L until a break statement is reached.
A
V  When a break statement is reached, the switch terminates, and the flow of control jumps to the next
line following the switch statement.
N
E  Not every case needs to contain a break. If no break appears, the flow of control will fall through to
P subsequent cases until a break is reached.
A
L  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. 6/28/2022 6:11 AM
P
R
E Example - 8
P
#include<stdio.h>
A
#include<conio.h>
R void main()
E {
D int choice;
LOOP:
printf("Select 1 for file, 2 for Edit, 3 for Save\n");
B printf("1==>File\n2==>Edit\n3==>Save\n");
Y scanf("%d",&choice);
switch(choice)
S {
case 1:
U
printf("\nYou have chosen File Menu Item\n");
L break;
A case 2:
V printf("\nYou have chosen Edit Menu Item\n");
break;
case 3:
N printf("\nYou have chosen Save Menu Item\n");
E break;
P default:

A printf("\nINVALID OPTION CHOSED\n");


goto LOOP;
L }
getch();
} 6/28/2022 6:11 AM
P
R
E Loop or Repeating Construct
P
A
R
 You may encounter situations, when a block of code needs to be
E executed several number of times.
D

B
Y  Programming languages provide various control structures that allow
S for more complicated execution paths.
U
L
A
V
 A loop statement allows us to execute a statement or group of
statements multiple times.
N
E
P
A
L

6/28/2022 6:11 AM
P
R
E for Loop
P
A  A for loop is a repetition control structure that allows you to efficiently write a loop that needs to execute a
R specific number of times.
E
D  The syntax for a for loop in C programming language is:
for(init ; condition ; increment)
B { statement(s);
Y }

S  The init step is executed first, and only once. This step allows you to declare and initialize any loop control
U variables.
L
A  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
V does not execute and the flow of control jumps to the next statement just after the for loop.

N  After the body of the for loop executes, the flow of control jumps back up to the increment statement. This
E statement allows you to update any loop control variables. This statement can be left blank, as long as a
P semicolon appears after the condition.
A
L  The condition is now evaluated again. If it is true, the loop executes and the process repeats itself. After the
condition becomes false, the for loop terminates.
6/28/2022 6:11 AM
P
R
E Example - 9
P
A
R
#include<stdio.h>
E
D
#include<conio.h>
B
void main()
Y {
S int a;
U
L for(a=10;a<=20;a++)
A
V {
N printf("Value of a is %d\n",a);
E
P }
A
L getch();
} 6/28/2022 6:11 AM
P
R
E while Loop
P
A  A while loop in C programming repeatedly executes a target statement as
R
E long as a given condition is true.
D

B  The syntax for a while loop in C programming language is:


Y
while(condition)
S { statement(s);
U }
L
A
V
 Statement(s) may be a single statement or a block of statements.
N
E
P  The condition may be any expression, and true is any nonzero value.
A
L
 The loop iterates while the condition is true.
6/28/2022 6:11 AM
P
R
E Example - 10
P
A
R
#include<stdio.h>
E #include<conio.h>
D
void main()
B
Y {
S
int a=10;
U while(a<20)
L
A {
V
printf("Value of a is %d\n",a);
N
E
a++;
P
A
}
L getch();
} 6/28/2022 6:11 AM
P
R
E do…while Loop
P
A  Unlike for and while loops, which test the loop condition at the top of the
R
E loop, the do…while loop in C programming checks its condition at the
D bottom of the loop.
B
Y  A do…while loop is similar to a while loop, except the fact that it is
S guaranteed to execute at least one time.
U
L
A  The syntax for a do…while loop in C programming language is:
V
do
N { statement(s);
E } while(condition);
P
A
L
 Conditional expression appears at the end of the loop, so the statement(s)
in the loop executes once before the condition is tested. 6/28/2022 6:11 AM
P
R
E Example - 11
P
A
R
#include<stdio.h>
E #include<conio.h>
D
void main()
B
Y {
S
int a=10;
U do
L
A {
V
printf("Value of a is %d\n",a);
N
E
a++;
P
A
} while(a<20);
L getch();
} 6/28/2022 6:11 AM
P
R
E for vs. while
P
A
R
for while
E
D It is definite loop It may be definite and may not be definite

B
The loop expression is scattered throughout the
The loop expression is within a one block
Y program
It contains three expressions It contains only one expression
S
U Syntax: Syntax:
L for(init; condition; increment/decrement) while(test expression)
A { statement(s); } { body of loop }
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E while vs. do…while
P
A
R
while do…while
E
D do…while loop is exit-controlled loop. i.e. the
while loop is entry-controlled loop. i.e. test
body of the loop is executed first without checking
B condition is evaluated first and body of loop is
condition and at the end of body of loop, the
Y executed only if this test is true.
condition is evaluated for repetition of next time.
S The body of the loop may not be executed at all if
U the condition is not satisfied at the very fist The body of loop is always executed at least once.
L attempt.
A
V Loop is not terminated with semicolon. Loop is terminated with semicolon.
Syntax:
N Syntax:
do{
E while(test expression)
Body of loop
P { body of loop }
A
}while(test expression);
L

6/28/2022 6:11 AM
P
R
E Nested loops in C
P
A
R
 C programming allows to use one loop inside another loop.
E
D
 The inner loop is said to be nested within the outer loop.
B
Y  Nested for loop
S  Nested while loop
U
L
 Nested do…while loop
A
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E Nested for loop
P
A
R
for(init; condition; increment)
E
D
{ for(init; condition; increment)
B
{ statement(s);
Y }
S statement(s);
U
L }
A
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E Nested for loop
P
A #include<stdio.h>
R
E #include<conio.h>
D
void main()
B {
Y
int i, j;
S
U for(i=1; i<=9; i++)
L {
A
V for(j=1; j<=i; j++)
N printf("%d",i);
E printf("\n");
P
A }
L
getch();
} 6/28/2022 6:11 AM
P
R
E Nested while loop
P
A
R
while(condition)
E
D
{ while(condition)
B
{ statement(s);
Y }
S statement(s);
U
L }
A
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E Nested while loop
P
A #include <stdio.h>
R
E
int main()
D {
int a = 1, b = 1;
B while(a <= 5)
Y
{
S b = 1;
U while(b <= 5)
L {
A printf("%d ", b);
V b++;
}
N printf("\n");
E a++;
P }
A
return 0;
L
}
6/28/2022 6:11 AM
P
R
E Nested do…while loop
P
A
R
do
E
D
{ statement(s);
B
do
Y { statement(s);
S } while(condition);
U
L }while(condition);
A
V

N
E
P
A
L

6/28/2022 6:11 AM
P
R
E Nested do while loop
P
A #include <stdio.h>
R
E int main()
D
{
B do
Y
{
S printf("I'm from outer do-while loop ");
U do
L
{
A
V printf("\nI'm from inner do-while loop ");
}
N while(1 > 10);
E
P }
A while(2 > 10);
L
return 0;
} 6/28/2022 6:11 AM
P
R
E exit function
P
A  The C library function void exit(int status) terminates the
R
E calling process immediately.
D

B #include<stdio.h>
Y
#include<conio.h>
S void main()
U
L {
A printf("Start of the program . . . \n");
V
printf("Exiting the program . . . \n");
N exit(0);
E
P printf("End of the program . . . \n");
A
L
getch();
}
6/28/2022 6:11 AM
P
R
E
P
A
R
E
D

B
Y

S
END OF UNIT FIVE
U
L
A
V

N
E
P
A
L

6/28/2022 6:11 AM

You might also like

pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy