C Programming Lesson 3

You might also like

Download as pdf or txt
Download as pdf or txt
You are on page 1of 32

Writing Control Structures

 KIIT 2014
Objectives
After completing this lesson, you should be able to do the following:
• Identify the uses and types of control structures
• Construct an if statement
• Use of input library function scanf()
• Use switch statements and case expressions
• Construct and identify different loop statements
• Make use of guidelines while using the conditional control
structures

 KIIT 2014
Controlling Flow of Execution
The ability to control the flow of your program, allows
you to make decisions:
• What code is to be executed
– Using Selection
• How many times that code is to be executed
– Using Iteration

 KIIT 2014
IF Statements
The if statement allows you to control if a program
enters a section of code or not based on whether a
given condition is true or false

 KIIT 2014
Decision Making
C programming language provides following types of decision
making statements.
Statement Description
if statement An if statement consists of a boolean expression followed by
one or more statements.
if...else statement An if statement can be followed by an optional else
statement, which executes when the boolean expression is
false.
nested if statements You can use one if or else if statement inside
another if or else if statement(s).
switch statement A switch statement allows a variable to be tested for equality
against a list of values.
nested switch statements You can use one switch statement inside
another switchstatement(s).

 KIIT 2014
IF Statements

Syntax:
if(boolean_expression)
{ /* statement(s) will execute if the boolean
expression is true */
}

 KIIT 2014
IF Statements

Example:
#include <stdio.h>
int main ()
{
/* local variable definition */
int myage = 31;
/* check the boolean condition using if statement */
if( myage < 11 )
{
/* if condition is true then print the following */
printf("I am a child\n" );
return 0;
}

 KIIT 2014
IF .. ELSE Statement

Syntax:
if(boolean_expression)
{
/* statement(s) will execute if the boolean expression
is true */
}
else
{
/* statement(s) will execute if the boolean expression
is false */
}

 KIIT 2014
IF .. ELSE Statement
Example:
#include <stdio.h>
int main ()
{
int myage = 31;
if( myage < 11 )
{
printf("I am a child \n" );
}
else
{
printf("I am not a child \n" );
}
return 0;
}

 KIIT 2014
Nested IF Statement

Syntax:
if(boolean_expression1)
{
/* Executes when the boolean expression 1 is true */
if(boolean_expression2)
{
/* Executes when the boolean expression 2 is true */
}
}

 KIIT 2014
Nested IF Statement
Example:
#include <stdio.h>
int main ()
{
int a = 100;
int b = 200;
if( a == 100 )
{
if( b == 200 )
{
printf("Value of a is 100 and b is 200\n" );
}
}
printf("Exact value of a is : %d\n", a );
printf("Exact value of b is : %d\n", b );
return 0;
}

 KIIT 2014
switch Statement
• A switch statement is a selection statement that lets you transfer
control to different statements within the switch body depending on the
value of the switch expression. The switch expression must evaluate to
an integral or enumeration value.
• 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.
• The constant-expression for a case must be the same data type as the
variable in the switch, and it must be a constant or a literal.
• When a break statement is reached, the switch terminates, and the flow
of control jumps to the next line following the switch statement.
• 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.

 KIIT 2014
switch Statement

Syntax:
switch(expression)
{
case constant-expression : statement(s);
break;
case constant-expression : statement(s);
break;
default : statement(s);
}

 KIIT 2014
switch Statement
Example:
#include <stdio.h>
int main ()
{
char grade = 'B';
switch(grade)
{
case 'A' : printf("Excellent!\n" );
break;
case 'B' : case 'C' : printf("Well done\n" );
break;
case 'D' : printf("You passed\n" );
break;
case 'F' : printf("Better try again\n" );
break;
default : printf("Invalid grade\n" );
}
printf("Your grade is %c\n", grade );
return 0;
}

 KIIT 2014
C library function - scanf()
The C library function scanf() reads formatted input
from stdin
#include <stdio.h>
int main()
{
char str1[20], str2[30];
printf("Enter name: ");
scanf("%s", &str1);
printf("Enter your website name: ");
scanf("%s", &str2);
printf("Entered Name: %s\n", str1);
printf("Entered Website:%s", str2);
return(0);
}

 KIIT 2014
Iterative Control: LOOP

• Loops repeat a statement or sequence


of statements multiple times.
• There are three loop types:
– do .. while loop
– for loop
– while loop

 KIIT 2014
do..while Loop

Syntax:
do
{
statement(s);
}while( condition );

 KIIT 2014
do..while Loop

Example:
#include <stdio.h>
int main ()
{
int a = 10;
do
{
printf("value of a: %d\n", a);
a = a + 1;
}while( a < 20 );
return 0;
}

 KIIT 2014
for Loop

Syntax:
for (variable initialization; condition; variable
update)
{
Code to execute while the condition is true
}

 KIIT 2014
for Loop

Example:
#include <stdio.h> int main()
{
int x;
for ( x = 0; x < 10; x++ )
{
printf( "%d\n", x );
}
}

 KIIT 2014
while Loop

Syntax:
while (condition)
{
statement(s);
}

 KIIT 2014
while Loop

Example:
#include <stdio.h>
int main ()
{
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
}
return 0;
}

 KIIT 2014
Loop Control Statement
• Change execution from its normal sequence. When
execution leaves a scope, all automatic objects that were
created in that scope are destroyed.
• C supports the following control statements

Control Statement Description


break statement Terminates the loop or switch statement and transfers
execution to the statement immediately following the
loop or switch.
continue statement Causes the loop to skip the remainder of its body and
immediately retest its condition prior to reiterating.
goto statement Transfers control to the labeled statement. Though it is
not advised to use goto statement in your program.

 KIIT 2014
break Statement
The break statement in C programming language has the
following two usages:
• When the break statement is encountered inside a loop,
the loop is immediately terminated and program control
resumes at the next statement following the loop.
• It can be used to terminate a case in
the switch statement.
If you are using nested loops (i.e., one loop inside another
loop), the break statement will stop the execution of the
innermost loop and start executing the next line of code after
the block.

 KIIT 2014
break Statement

 KIIT 2014
break Statement
Example:
#include <stdio.h>
int main ()
{
int a = 10;
while( a < 20 )
{
printf("value of a: %d\n", a);
a++;
if( a > 15)
{
break;
}
}
return 0;
}

 KIIT 2014
continue Statement
• The continue statement in C programming language works
somewhat like the break statement. Instead of forcing
termination, however, continue forces the next iteration of
the loop to take place, skipping any code in between.
• For the for loop, continue statement causes the
conditional test and increment portions of the loop to
execute. For
the while and do...while loops, continue statement
causes the program control passes to the conditional tests.

 KIIT 2014
continue Statement

 KIIT 2014
continue Statement
Example:
#include <stdio.h>
int main ()
{
int a = 10;
do
{
if( a == 15)
{
a = a + 1; continue;
}
printf("value of a: %d\n", a);
a++;
}
while( a < 20 );
return 0;
}

 KIIT 2014
goto Statement
• A goto statement in C programming language provides an
unconditional jump from the goto to a labeled statement in
the same function.

NOTE: Use of goto statement is highly discouraged in any programming language because it makes difficult to
trace the control flow of a program, making the program hard to understand and hard to modify. Any program
that uses a goto can be rewritten so that it doesn't need the goto.

 KIIT 2014
goto Statement
Example:
#include <stdio.h>
int main ()
{
int a = 10;
LOOP:do
{
if( a == 15)
{
a = a + 1;
goto LOOP;
}
printf("value of a: %d\n", a);
a++;
} while( a < 20 );
return 0;
}

 KIIT 2014
Summary
In this lesson, you should have learned how to:
• Identify the uses and types of control structures
• Construct an if statement
• Use of input library function scanf()
• Use switch statements and case expressions
• Construct and identify different loop statements
• Make use of guidelines while using the conditional control
structures
• Make use of loop control statements

 KIIT 2014

You might also like