Test 1
Test 1
Simple if statement
If…..else statement
Nested if..else statement
Else…..if ladder
Syntax
If (test expression)
Statement-block;
Statement-x;
Flow chart
No
Test Expression
Yes
Statement Block
Statement X
Example
A=10;
B=5;
if (A>B)
If the given condition is satisfied then computer will print the message
“A is the largest number” and if not simply skip this statement.
2.1.1.2. If else
Syntax
if(condition)
{
True block statements;
}
else
{
False block statements;
}
Statement X;
Flow Chart
Yes No
Condition
False block
True block statements statements
Example
2.1.1.3.Nested IF ELSE
Syntax
if(test_expression one)
{
if(test_expression two)
{
Statement - 1;
}
else
{
Statement – 2;
}
else
{
Statement – 3;
}
Statement-X;
If the test expression one is true, then check the test expression two if
it is true then statement-1 is executed; otherwise statement-2 is executed.If
the test expression one is false then statement – 3 is executed. In both the
case, the control is transferred subsequently to the statements-x.
Flow chart
Example
#include<stdio.h>
intmain()
{
int number1, number2;
printf("Enter two integers: ");
scanf("%d %d", &number1, &number2);
if (number1 >= number2)
{
if (number1 == number2)
{
printf("Result: %d = %d",number1,number2);
}
else
{
printf("Result: %d > %d", number1, number2);
2.1.1.4. ELSE IF ladder
Syntax
if (test expression1)
{
// statement(s)
}
else if(test expression2)
{
// statement(s)
}
else if (test expression3)
{
// statement(s)
}
.
.
else
{
// statement(s)
}
Flow chart
Example
#include <stdio.h>
int main()
if(number1 == number2)
printf("Result: %d = %d",number1,number2);
2.1.2. Switch
Syntax
switch (expression)
{
case value-1: block-1;
break;
case value-2: block-2;
break;
……..
Default: default-block;
break;
}
statement-x;
#include<conio.h>
void main()
char choice;
choice=gerchar();
if(choice>=“a”&& choice<=“z”)
choice=32;
switch(choice)
case ‘c’:
The goto requires a label in order to identify the place where the
branch is to be made.
A label is any valid variable name and must be followed by a colon.
The label is placed immediately before the statement when the
control is to be transferred.
There are 2 types of jump statements. They are
o Forward jump – the “goto” placed first then the label with
statements placed.
o Backward jump – the label with statements placed first then
the “goto” placed.
Syntax
Forward jump
goto label;
…………
…………
Label:
statement;
Backward jump
label:
statement;
…………
…………
goto label;
The label can be anywhere in the program before or after the gotolabel
statement, a loop will be found and some statements will be executed
repeatedly, such a jump is known as a backward jump. If the label is placed
after the goto label, some statements will be skipped and the jump is known
as a forward jump.
Example
#include <stdio.h>
#include<conio.h>
void main()
intx,y;
clrscr();
x=16;
y=12;
if(x==y)
x++;
else
2.2. Decision Making and Looping:
2.2.1. Introduction
for statement
while statement
2.2.2. For
Where
The init is executed first, and only once. This allows you to declare and
initialize any loop control variables.
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 does not execute and the
flow of control jumps to the next statement just after the 'for' loop.
After the body of the 'for' loop executes, the flow of control jumps back
up to the increment statement. This statement allows you to update
any loop control variables. This statement can be left blank, as long as
a semicolon appears after the condition.
The condition is now evaluated again. If it is true, the loop executes
and the process repeats itself (body of loop, then increment step, and
then again condition). After the condition becomes false, the 'for' loop
terminates.
Flow chart
Example
#include <stdio.h>
int main ()
int a;
Flow chart
Example
#include <stdio.h>
int main()
int n;
Syntax
#include<stdio.h>
#include<conio.h>
void main()
int digit=1;
clrscr();
while(digit<=10)
Do
{
Body of the loop;
}
while(condition);
Flow chart
Example
#include<stdio.h>
#include<conio.h>
void main()
{int count=0;
char c;
do
{c=getchar();
2.2.6. Jumps in loops
Jump Description
Statement
continue The continue statement is used for skipping part of loop's body.
break The break statement is used to stop the execution of loop and switch
case statements.
2.2.6.1. Break
In any loop “break” is used to jump out of loop skipping the code below
it without caring about the test condition. It interrupts the flow of
the program by breaking the loop and continues the execution of code which
is outside the loop.
Do
{
while (test_condition)
{
statement1;
statement1; if (condition)
if (condition ) break;
break;
statement2;
statement2;
}while
}
(test_condition);
2.2.6.2.Continue
The continue statement in C programming works somewhat like
the break statement. Instead of forcing termination, it forces the next
iteration of the loop to take place, skipping any code in between.
Syntax
continue;
Do
while (test_condition)
{
{
statement1;
statement1;
if (condition)
if (condition )
continue;
continue;
statement2;
statement2;
}while
}
(test_condition);
for (int-exp; test-exp; update-exp)
{
statement1;
if (condition)
continue;
statement2;
}
In all the above cases the compiler immediately continue the next
iteration whenever the “if” statement is true. That is it brings the control to
starting of the loop without executing the statement2.
Unit III
3.1. Arrays:
Arrays are broadly classified into three types. They are as follows
2.3.1. One-dimensional
A list of items can be stored in one variable name using only one
subscript such a variable is called as subscripted variable or one dimensional
array.
Syntax
where
Example1
int value[20];
It declares the value to be an array of integers containing 20 integer
elements.
Example2
char address[50];
Runtime initialization
Example
int number[3]={0,0,0};
The size can be omitted. In such case the compiler allocates enough
space for all initialized elements
Example
int counter[]={1,1,1,1}
Example program
#include<stdio.h>
int main ( ){
int i;
Output
10 0 30 40 50
The output of compile time initialised program will not change during
different runs of the program.
Example program
#include<stdio.h>
main ( )
int a[5],i;
scanf("%d", &a[i]);
Output
enter 5 elements 10 20 30 40 50
Syntax
Datatype array-name[row_size][colmn_size];
Where
Example
int a[3][4] = {
The nested braces, which indicate the intended row, are optional. The
following initialization is equivalent to the previous example
For example
intval = a[2][3];
The above statement will take the 4th element from the 3rd row of the
array.
Example program
#include <stdio.h>
int main ()
int i, j;
Syntax
data_typearray_name[size1][size2]....[sizeN];
Where