demo of for loop
#include<stdio.h>
int main()
{
int i;
/* in this the loop the counter is initialised also the condition is provided ie how many times
the loop should run and counter is incremented. It is used when we know how many times a loop will run */
for (i = 0; i < 10; i++)
{
printf ("Hello\n");
printf ("World\n");
}
return 0;
}
demo of while loop
#include<stdio.h>
int main()
{
int counter, val;
scanf("%d", &val);
counter = 0;
/*In this loop the counter is initialised outside before the loop, and the counter is incremented. It is used when we do not know how many times the loop will run */
while ( counter < val)
{
counter++;
printf("%d\n", counter);
}
return 0;
}
demo of if statement
#include<stdio.h>
int main()
{
int number;
/* if statement can be used to test conditions so that we can alter the flow of a program ie. if statement is true execute the instruction if not true execute the instruction */
scanf("%d",&number);
if ( number == 10 )
printf("Is equal\n");
return 0;
}
demo of switch statement
#include<stdio.h>
int main()
{
char myinput;
printf("Which option will you choose:\n");
printf("a) Program 1 \n");
printf("b) Program 2 \n");
scanf("%c", &myinput);
/* switch statement can have many conditions. Switch statement starts with a condition. If one of the variable equals the condition, the instructions are executed */
switch (myinput)
{
case 'a':
printf("Run program 1\n");
break;
case 'b':
{
printf("Run program 2\n");
printf("Please Wait\n");
break;
}
default:
printf("Invalid choice\n");
break;
}
return 0;
}
#include<stdio.h>
int main()
{
int i;
/* in this the loop the counter is initialised also the condition is provided ie how many times
the loop should run and counter is incremented. It is used when we know how many times a loop will run */
for (i = 0; i < 10; i++)
{
printf ("Hello\n");
printf ("World\n");
}
return 0;
}
demo of while loop
#include<stdio.h>
int main()
{
int counter, val;
scanf("%d", &val);
counter = 0;
/*In this loop the counter is initialised outside before the loop, and the counter is incremented. It is used when we do not know how many times the loop will run */
while ( counter < val)
{
counter++;
printf("%d\n", counter);
}
return 0;
}
demo of if statement
#include<stdio.h>
int main()
{
int number;
/* if statement can be used to test conditions so that we can alter the flow of a program ie. if statement is true execute the instruction if not true execute the instruction */
scanf("%d",&number);
if ( number == 10 )
printf("Is equal\n");
return 0;
}
demo of switch statement
#include<stdio.h>
int main()
{
char myinput;
printf("Which option will you choose:\n");
printf("a) Program 1 \n");
printf("b) Program 2 \n");
scanf("%c", &myinput);
/* switch statement can have many conditions. Switch statement starts with a condition. If one of the variable equals the condition, the instructions are executed */
switch (myinput)
{
case 'a':
printf("Run program 1\n");
break;
case 'b':
{
printf("Run program 2\n");
printf("Please Wait\n");
break;
}
default:
printf("Invalid choice\n");
break;
}
return 0;
}
No comments:
Post a Comment