Search
Log in
Latest topics
Statistics
We have 30 registered usersThe newest registered user is deigavr
Our users have posted a total of 191 messages in 126 subjects
Who is online?
In total there is 1 user online :: 0 Registered, 0 Hidden and 1 Guest None
Most users ever online was 125 on Wed Apr 07, 2021 9:18 am
7th program
Page 1 of 1
7th program
Objective
Study the use of Branching Statements (Conditional programming).
Theory
Conditional statements and conditional expressions are features of a programming language which perform different computations or actions depending on whether a programmer-specified condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition.
If-Then(-Else)
The if-then construct (sometimes called if-then-else) is common across many programming languages. Although the syntax varies quite a bit from language to language, the basic structure looks like this:
If (condition) Then
(statements)
Else
(statements)
End If
When an interpreter finds an If, it expects a boolean condition and evaluates that condition. If the condition is true, the statement block following the Then is executed. Otherwise, the execution continues in the following block - either in the Else block (which is usually optional), or if there is no Else block, then after the End If.
After either the block after the Then or the block after the Else has been executed, control returns to the point after the End If.
Else If
By using Else If, it is possible to combine several conditions. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped. The statements of the final Else will be executed if none of the conditions are true.
if condition then
-- statements;
elseif condition then
-- more statements;
elseif condition then
-- more statements;
...
else condition then
-- other statements;
end if;
Task
1. Write a program that enter a number, find out whether that number is EVEN or ODD. Using If Else Condition.
coding
#include<stdio.h>
void main(void)
{
int w;
printf("ENTER THE NUMBER\n");
scanf("%d",&w);
if(w%2==0)
printf("THE NUMBER IS EVEN\n");
else
printf("the number is odd\n")
}
2. Write a program that uses If Else, assigns a grade based on the value of a test score, an A for a score of 80% or above, a B for a score of 70% or above, and so on.
coding
#include<stdio.h>
void main(void)
{
float q;
printf("Enter Percentage");
scanf("%f",&q);
if(q>=80)
printf("\nGRADE A+\n");
else if(q>=70)
printf("\nGRADE A\n");
else if(q>=60)
printf("\nGRADE B\n");
else if(q>=50)
printf("\nGRADE C\n");
else if(q>=40)
printf("\nGRADE D\n");
else if(q<40)
printf("\nGRADE F\n");
}
Study the use of Branching Statements (Conditional programming).
Theory
Conditional statements and conditional expressions are features of a programming language which perform different computations or actions depending on whether a programmer-specified condition evaluates to true or false. Apart from the case of branch predication, this is always achieved by selectively altering the control flow based on some condition.
If-Then(-Else)
The if-then construct (sometimes called if-then-else) is common across many programming languages. Although the syntax varies quite a bit from language to language, the basic structure looks like this:
If (condition) Then
(statements)
Else
(statements)
End If
When an interpreter finds an If, it expects a boolean condition and evaluates that condition. If the condition is true, the statement block following the Then is executed. Otherwise, the execution continues in the following block - either in the Else block (which is usually optional), or if there is no Else block, then after the End If.
After either the block after the Then or the block after the Else has been executed, control returns to the point after the End If.
Else If
By using Else If, it is possible to combine several conditions. Only the statements following the first condition that is found to be true will be executed. All other statements will be skipped. The statements of the final Else will be executed if none of the conditions are true.
if condition then
-- statements;
elseif condition then
-- more statements;
elseif condition then
-- more statements;
...
else condition then
-- other statements;
end if;
Task
1. Write a program that enter a number, find out whether that number is EVEN or ODD. Using If Else Condition.
coding
#include<stdio.h>
void main(void)
{
int w;
printf("ENTER THE NUMBER\n");
scanf("%d",&w);
if(w%2==0)
printf("THE NUMBER IS EVEN\n");
else
printf("the number is odd\n")
}
2. Write a program that uses If Else, assigns a grade based on the value of a test score, an A for a score of 80% or above, a B for a score of 70% or above, and so on.
coding
#include<stdio.h>
void main(void)
{
float q;
printf("Enter Percentage");
scanf("%f",&q);
if(q>=80)
printf("\nGRADE A+\n");
else if(q>=70)
printf("\nGRADE A\n");
else if(q>=60)
printf("\nGRADE B\n");
else if(q>=50)
printf("\nGRADE C\n");
else if(q>=40)
printf("\nGRADE D\n");
else if(q<40)
printf("\nGRADE F\n");
}
Page 1 of 1
Permissions in this forum:
You cannot reply to topics in this forum
|
|
Mon Aug 01, 2011 11:44 pm by Guest
» Netbook Brands
Mon Aug 01, 2011 5:39 am by Guest
» backlinks checker backlink service
Sun Jul 31, 2011 8:56 am by Guest
» how to buy facebook fans f4
Sat Jul 30, 2011 2:34 pm by Guest
» Alle bijzondere dingen in de zaanstreek
Sat Jul 30, 2011 8:11 am by Guest
» HERE YOU CAN POST ALL WEBSITE LINKS...
Wed Aug 26, 2009 2:16 pm by onesimpletech
» cool web site for all must check it
Mon Jul 06, 2009 2:32 pm by Snopy Cobra
» poetry spirit
Mon Jul 06, 2009 2:31 pm by Snopy Cobra
» best to learn VC++
Sun May 10, 2009 3:22 pm by Guest