The execution of statements in our programs discussed so far followed an order or sequence. The first statement at the top of the program is executed, then the next statement, then the statement after that and so on, until the last is executed. This is called sequential
execution. In other words it is the execution of a sequence of instructions in the same order as they appear in the sequence. Such type of a program structure is called a sequence structure.
But what if we want the computer to execute either one statement or another depending on what has
happened before? Surely we need something that can control the flow of the instructions in our program.
Such a structure is known as a control structure.
Control Structures
Control Structures are the elements of a programming language that determine the flow of programexecution.
All high-level programming languages have control structures. They allow the programmer to express
computer instructions is a very natural way. They fall in to two distinct classes:
1) Selection (Conditional execution)
2) Repetition
Conditional execution means that an instruction executes when a specified condition occurs. Repetition
control structures use conditions to determine how many times to repeat execution of an action.
The if (keyword)
C programs use if.. else control statement to test conditions and execute one of two statements of statement blocks depending on the condition.Syntax : a) if (expression) b) if (expression)
<statement 1>; <statement 2>;
else
<statement 2>;
In a) If the (expression) is not zero when evaluated <statement 1> is executed
In b) If the (expression) is not zero when evaluated <statement 1> is executed
and if the expression is zero when evaluated <statement 2> is executed.
Termination of a Program
exit() terminates a program / terminates a calling process. Before termination it closes all files, writes buffered out put and calls any registered "exit function"./* Age check */
#include<stdio.h>
#include<conio.h>
int main()
{
int age;
printf("Enter your age : ");
scanf("%d",&age);
if (age>=18)
printf("\n You are eligible for voting \n");
}
/* Height check*/
#include<stdio.h>
#include<conio.h>
int main()
{
int height;
printf("Enter your height : ");
scanf("%d",&height);
if (height>=5)
printf("\nYou are tall \n");
else
printf("\n You are short \n");
}
/* Bonus years*/
# include<stdio.h>
# include<conio.h>
# define bonus_rate 10/100;
int main()
{
int current_year, year_join ;
float salary, bonus;
printf(“Enter current year and year of join [yyyy] : ”);
scanf(“%4d %4d”, ¤t_year, &year_join);
printf(“\n Enter salary : ”);
scanf(“%f”, &salary);
if (current_year - year_join >= 3)
{
bonus = salary * bonus_rate;
printf(“\nBonus = Rs. %.2f \n”, bonus);
}
else
printf(“No bonus \n”);
}
/* Hot, Cold or Nice*/
# include <stdio.h>
# include <conio.h>
# define max_temp 35
# define min_temp 25
int main()
{
float temp;
printf(“Enter today’s temperature : ”);
scanf(“%f”, &temp);
if (temp > max_temp)
printf(“\n Today is Hot \n”);
else if (temp < min_temp)
printf(“\n Today is Cold \n”);
else
printf(“\n The weather is nice \n”);
}
/* Final marks */
#include<stdio.h>
#include<conio.h>
#include<stdlib.h>
int main()
{
int marks;
printf("Enter your marks (0..100): ");
scanf("%d",&marks);
if (marks>100 || marks<0)
{
printf("\n Please enter correct value \n");
exit(0);
}
else if (marks = = 100 || marks = =0)
;
else if (marks % 10 >=5)
marks ++;
else
marks --;
printf("\nYour final marks %d \n",marks);
}
No comments:
Post a Comment