Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
if
if-else
Else-if Ladder
Nested if statements
switch
if-then Statement
Syntax
if (condition)
{
Statement;
}
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a = 15, b = 5, c = 0;
if (a > b)
{
c = a + b;
}
printf(" The value of c is %d", c);
getch();
}
Output
The value of c is 20
If – else statement
Syntax
if (condition)
{
Statements 1;
}
else
{
Statements 2;
}
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf("Enter value for a : ");
scanf (“%d”, &a);
printf("Enter value for b : ");
scanf (“%d”, &b);
if ( a > b )
{
printf(" a is greater than b ");
}
else
{
printf(" b is greater than a ");
}
getch();
}
Output
Enter value for a : 20
Enter value for b : 10
a is greater than b
Else-if Ladder
Syntax
if (condition1)
{
Statement1;
}
else_if(condition2)
{
Statement2;
}
else
{
Statement 3;
}
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int a, b;
clrscr();
printf("Enter value for a : ");
scanf (“%d”, &a);
printf("Enter value for b : ");
scanf (“%d”, &b);
if ( a > b )
{
printf(" a is greater than b ");
}
else if ( b > a )
{
printf(" b is greater than a ");
}
else
{
printf(" Both a and b are equal ");
}
getch();
}
Output
Enter value for a : 50
Enter value for b : 50
Both a and b are equal
Nested if statements
Syntax
if (condition)
{
if (condition)
{
Statements 1;
}
else
{
Statements 2;
}
else
{
Statements 3;
}
}
Example
#include <stdio.h>
#include <conio.h>
void main()
{
char username;
int password;
clrscr();
printf("Username:");
scanf("%c",&username);
printf("Password:");
scanf("%d",&password);
if(username=='a')
{
if(password==12345)
{
printf("Login successful");
}
else
{
printf("Password is incorrect, Try again.");
}
}
else
{
printf("Username is incorrect, Try again.");
}
getch();
}
Output 1:
Username: a
Password: 12345
Login successful
Output 2:
Username: a
Password: 123456
Password is incorrect, Try again.
Output 3:
Username: a
Password: 12345
Username is incorrect, Try again.
Syntax
switch (expression)
{
case 1:
statements 1;
break;
case 2:
statements 2;
break;
case 3:
statements 3;
break;
default:
statements 0;
break; // optional
}
Example
#include <stdio.h>
#include <conio.h>
void main()
{
int choice;
clrscr();
printf("Select any option number from 1 to 4 : ");
scanf("%d",&choice);
switch(choice)
{
case 1:
printf("You have selected first option");
break;
case 2:
printf("You have selected second option");
break;
case 3:
printf("You have selected third option");
break;
case 4:
printf("You have selected fourth option");
break;
default:
printf("Invalid option selected");
break;
}
getch();
}
Output
Select any option number from 1 to 4 : 3
You have selected third option
Loop control statements
for loop
while loop
do..while
For Loop
Syntax
for ( initialization; condition; increment/decrement )
{
statement(s);
}
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int n=1, times=5;
clrscr();
for(n=1 ; n<=times ; n=n+1)
{
printf(" C for Loop : %d\n",n);
}
getch();
}
Output
C for Loop : 1
C for Loop : 2
C for Loop : 3
C for Loop : 4
C for Loop : 5
While Loop
Syntax
Assignment;
While (condition)
{
statements;
Increment or Decrements (++ or --);
}
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int n=1, times=5;
clrscr();
while(n<=times)
{
printf(" C while Loop : %d\n",n);
n++;
}
getch();
}
Output
C while Loop : 1
C while Loop : 2
C while Loop : 3
C while Loop : 4
C while Loop : 5
do-while
Syntax
do
{
Statements;
Increment or Decrements (++ or --)
}
while(condition);
Example
#include<stdio.h>
#include<conio.h>
void main()
{
int n=1, times=5;
clrscr();
do
{
printf(" C do-while Loop : %d\n",n);
n++;
}while(n<=times);
getch();
}
Output
C do-while Loop : 1
C do-while Loop : 2
C do-while Loop : 3
C do-while Loop : 4
C do-while Loop : 5
Break statement
Break statement is used to terminate the while loops, switch case
loops and for loops from the subsequent execution.
Syntax: break;
Example
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5)
{
printf("\nComing out of for loop when i = 5");
break;
}
printf("%d ",i);
}
}
Output
0 1 2 3 4
Coming out of for loop when i = 5
Continue statement
Continue statement is used to continue the next iteration of for
loop, while loop and do-while loops. So, the remaining statements
are skipped within the loop for that particular iteration.
Syntax : continue;
Example
#include <stdio.h>
int main()
{
int i;
for(i=0;i<10;i++)
{
if(i==5 || i==6)
{
printf("\nSkipping %d from display using " \"continue
statement \n",i);
continue;
}
printf("%d ",i);
}
}
Output
0 1 2 3 4
Skipping 5 from display using continue statement
Skipping 6 from display using continue statement
7 8 9