3 Lecture
3 Lecture
rd
3 Lecture
First program in ‘C’
#include<stdio.h>
int main()
{
printf("Hello World\n");
return 0;
}
Decision making statement
• if execute a statement or not
• if-else choose to execute one of two
statements
• switch choose to execute one of a
number of statements
If statement
• The if statement allows branching (decision making)
depending upon a condition.
• Program code is executed or skipped.
• syntax
if (test expression)
{
statement-block;
}
statement- x;
If –else statement
if (test expression)
{
True-block statement ;
}
else
{
False-block statement;
}
statement-x;
If((1 || 0) && (0 || 1))
{
printf(“OK I am done”);
}
Else
{
printf(“OK I am gone”);
}
Ans:: OK I am done
nested if … else statement
if (test condition 1) else
{ {
if (test condition 2) statement 3;
{ }
statement 1; statement x;
}
else
{
statement 2;
}
}
else if ladder
if (condition 1)
statement 1;
else if (condition 2)
statement 2;
else if (condition 3)
statement 3;
else if (condition n )
statement n;
else
statement x;
Switch statement
The switch statement is a better way of
writing a program which employs an if-
else ladder. It is C’s built-in multiple
branch decision statement.
syntax :=
switch (integer expression)
{
case constant1:
statement1;
break;
case constant2:
statement2;
break;
...
default:
statement;
}
Looping statement
• while loop
• do_while loop
• for loop
• while if_break loop
While loop
• while loop will execute the give statement till the
“condition” Is true.
• The loop will terminate when the condition become
false.
Syntax:=
while ( test condition )
{
body of the loop;
}
For Example:
int cnt = 0;
int sum =0;
while(cnt <10)
{
sum = sum+5;
cnt++;
}
• Programmer is responsible for initialization and
incrimination.
Do while loop
Syntax:=
do
{
body of the loop;
}
while ( test-condition );
For loop
Syntax:=
for ( initialization ; test-condition ; increment )
{
body of the loop ;
}
while if_break loop
• Syntax
While(condition)
{
statemente 1;
if(condition)
break;
}
While(a<=10)
{
printf(“%d”,a);
a++;
if (a==10)
break;
}