Unit 2 Introduction Programming
Unit 2 Introduction Programming
Simple sequential programs Conditional Statements (if, if-else, switch), Loops (for, while, dowhile) Break and
Continue
CONTROL STRUCTURES (STATEMENTS)
A statement is a part of program that can be executed. A statement specifies an action.
Statements that are written individually are called Single/Simple statements. Statements that are written
as a block are called Block/Compound statements. A block begins with an open brace {and ends with a
closing brace}.
Control statements allow programmers to control the flow of execution of their programs.
C categorizes control statements into these groups:
SELECTION STATEMENTS:
A selection statement checks the given condition and decides the execution of statements after the
success orfailure of the condition. C supports two selection control statements if and switch.
1. If statement: The if statement is a decision making statement that allows the computer to evaluate an
expression (condition) first and depending on the result of the condition i.e. true or false, it transfers the
control to a particular statement.
The if statement has the following forms
a) Simple if Statement.
b) if...else Statement.
c) Nested if Statement.
d) if..else Ladder Statement.
Simple if Statement
The simple if statement contains only one condition, if the condition is true, it will execute the statements
that are present between opening and closing braces. Otherwise it will not execute those statements.
Syntax:
if(condition)
//Single-statement
OR
if(condition)
{
//statements
}
Example:
/* A program to check whether a number is Less than 100 or not */
#include<stdio.h>
#include<conio.h>
void main()
{
int a;
clrscr();
printf("enter an integer: ");
scanf("%d",&a);
if(a<100)
printf("%d is less than 100",a);
getch();
}
Output:
Enter an integer: 99
99 is less than 100
if...else Statement
This statement is used to define two blocks of statements in order to execute only one block. If the
condition istrue, the block of if is executed; otherwise, the block of else is executed.
Syntax:
if (condition)
{
//statements
}
else
{
//statements;
}
Example:
Write a program to check whether the given number is a positive number or negative number
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter any number ");
scanf("%d",&n);
if(n>=0)
{
printf("\n %d is a positive number",n);
}
else
{
printf("\n %d is a negative number",n);
}
getch();
}
Output:
enter any number -99
-99 is a negative number
Nested if –else Statement
When an if statement is placed in another if statement or in else statement, then it is called nested if statement.
The nested if-else is used when a series of decisions are involved.
Syntax:
if (condition)
{
if(condition)
{
// Statements;
}
}
else
{
//Statements
}
Example:
Write a program to finding greatest among three numbers
#include <stdio.h>
#include <conio.h>
void main()
{
double n1, n2, n3;
clrscr();
printf("Enter three numbers:");
scanf("%lf%lf%lf",&n1,&n2,&n3);
if (n1 >= n2)
{
if (n1 >= n3)
printf("%.2lf is the largest number.", n1);
else
printf("%.2lf is the largest number.", n3);
}
else
{
if (n2 >= n3)
printf("%.2lf is the largest number.", n2);
else
printf("%.2lf is the largest number.", n3);
}
getch();
}
Output:
Enter three numbers: 1 2 3
3.00 is the largest number.
Syntax:
if(condtion1)
{
//statements1;
}
else if(condition2)
{
//statements2;
}
else if(condition3)
{
//statements3;
}
|
|
|
else if(conditionN)
{
//statementsN;
}
Else
{
//Default_Statement ;
}
In the above syntax of if-else-if, if the Condition1 is TRUE then the Statement1 will be
executed and control goes to next statement in the program following if-else-if ladder.
If Condition1 is FALSE then Condition2 will be checked,
if Condition2 is TRUE then Statement2 will be executed and control goes to next statement in the
program following if-else-if ladder.
If Condition2 is FALSE then Condition3 will be checked,
if Condition3 is TRUE then Statement3 will be executed and control goes to next statement in the
program following if-else-if ladder
Similarly, if Condition3 is FALSE then next condition will be checked and the process
continues.
If all the conditions in the if-else-if ladder are evaluated to FALSE, then Default_Statement will be
executed.
Example:
Write a program to test whether the given number is a single digit or a double
digit or a trible digit or more than three digits.
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
printf("enter any number:");
scanf("%d",&n);
if(n>=0 && n<=9)
printf("\n %d is a single digit number",n);
else if(n>=10 && n<=99)
printf("\n %d is a double digit number",n);
else if(n>=100 && n<=999)
printf("\n %d is a trible digit number",n);
Else
printf("\n %d is more than three digits",n);
getch();
}
Output:
Enter any number:10
10 is a double digit number
Switch Statement
C has a built-in multiple-branch selection statement, called switch, which successively tests the value of an
expression against a list of case values. When a match is found, the statements associated with that case are
executed. For example, if the value of the expression is equal to value1, statements after case value1: are
executed until break is encountered. If there is no match, the default statements are executed.
Syntax:
switch(expression)
{
case value1://statements;
break;
case value2://statements;
break;
|
|
|
case valueN://statements;
break;
default://statements;
}
Example:
Write a program to check whether the given character is a vowel or not using switch case
#include<stdio.h>
#include<conio.h>
void main()
{
char ch;clrscr();
printf("enter any single character:");scanf("%c",&ch);
switch(ch)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
case 'A':
case 'E':
case 'I':
case 'O':
case 'U': printf("%c is a vowel",ch);
break;
default: printf("\n %c is not a vowel",ch);
break;
}
getch();
}
LOOP/ ITERATION STATEMENTS
In C, and all other modern programming languages, iteration statements (also called loops) allow a
set ofinstructions to be repeatedly executed until a certain condition is reached.
Based on position of loop, loop statements are classified into two types:
1. entry-controlled loop (pre-test loop)- while, for
2. exit-controlled loop (post-test loop)- do while
1. While loop
While is pre tested/ entry controlled loop statement i.e. first condition is checked , if condition is true body of
loop is executed repeatedly until condition is fail.
Syntax:
initialization;
while(condition)
{
statements;
Increment / Decrement statement;
}
Initialization: In this step, we initialize the loop variable to initial value.
Conditional: This is one of the most crucial steps as it decides whether the block in the while loops code
will execute or not. The while loop body will be executed if and only the condition defined is true.
Increment/Decrement: defines how the loop control variable changes each time the loop is repeated.
Example 1:
Write a program to print first 15 natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
n=1;
while(n<=15)
{
printf("%3d",n);
n=n+1;
}
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15
Example 2:
//Displaying multiplication table for given integer
#include <stdio.h>
#include <conio.h>
void main()
{
int i,n;
clrscr();
printf("Enter integer to display multiplication table:");
scanf("%d",&n);
i=1;
while(i<=10)
{
printf("%d*%d=%d\n",n,i,n*i);
i=i+1;
}
getch();
}
Output:
Enter integer to display multiplication table: 9
9*1=9
9*2=18
9*3=27
9*4=36
9*5=45
9*6=54
9*7=63
9*8=72
9*9=81
9*10=90
2. do while loop
Do While is post tested/ exit controlled loop statement i.e first body of loop is executed & finally
condition is checked. Even though condition is false, it will execute the body of loop statements at least
once.
Syntax:
initialization;
do
{
statements;
Increment/Decrement statement;
}while(condition) ;
Example:
Write a program to print first 20 natural numbers
#include<stdio.h>
#include<conio.h>
void main()
{
int n;
clrscr();
n=1;
do
{
printf("%3d",n);
n=n+1;
} while(n<=20);
getch();
}
Output:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
Difference between while/for & do while:
In while and for, if the condition is false, it will never execute the loop statements.
But in do-while, even though condition is false, it will execute the loop statements at least once