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
8th program
Page 1 of 1
8th program
Objective
Understanding the use of Switch.
Theory
A switch statement is a type of control statement that exists in most modern imperative programming languages (e.g., C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution. In some other programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement or a select statement.
A typical syntax is that the first line contains the actual word switch followed by either the name of a variable or some other expression allowed by the language's syntax. This variable or expression is usually referred to as the "control variable" of the switch statement. After this line, following lines define one or more blocks of code that represent possible branches that program execution may take.
Each block begins with a line containing the case keyword followed a value that the control variable may have. If the value of the control variable matches this value, program execution will jump to that block of code. If not, the value specified in the next block (if present) is examined and the process repeats.
Task
. Accept two numbers from the user. Then, display a menu with four options as given below:
1. Add numbers
2. Subtract second number from first
3. Multiply numbers
4. Divide first number by second number
* Prompt the user to enter a choice. Using switch to perform the appropriate calculation and display the result.
Coding:
#include<stdio.h>
void main(void)
{
float a=0.0,b=0.0;
char s;
printf("TYPE 1ST NUMBER THEN OPRETER THAN 2ND NUMBER\n");
scanf("%f %c %f",&a,&s,&b);
switch (s)
{
case'+':
printf("\n=%2.f",a+b);
break;
case'-':
printf("\n=%2.f",a-b);
break;
case'*':
printf("\n=%2.f",a*b);
break;
case'/':
printf("\n=%2.f",a/b);
break;
default:
printf("\nwrong opretor");
}
printf("\n\n");
}
Understanding the use of Switch.
Theory
A switch statement is a type of control statement that exists in most modern imperative programming languages (e.g., C, C++, C#, and Java). Its purpose is to allow the value of a variable or expression to control the flow of program execution. In some other programming languages, a statement that is syntactically different but conceptually the same as the switch statement is known as a case statement or a select statement.
A typical syntax is that the first line contains the actual word switch followed by either the name of a variable or some other expression allowed by the language's syntax. This variable or expression is usually referred to as the "control variable" of the switch statement. After this line, following lines define one or more blocks of code that represent possible branches that program execution may take.
Each block begins with a line containing the case keyword followed a value that the control variable may have. If the value of the control variable matches this value, program execution will jump to that block of code. If not, the value specified in the next block (if present) is examined and the process repeats.
Task
. Accept two numbers from the user. Then, display a menu with four options as given below:
1. Add numbers
2. Subtract second number from first
3. Multiply numbers
4. Divide first number by second number
* Prompt the user to enter a choice. Using switch to perform the appropriate calculation and display the result.
Coding:
#include<stdio.h>
void main(void)
{
float a=0.0,b=0.0;
char s;
printf("TYPE 1ST NUMBER THEN OPRETER THAN 2ND NUMBER\n");
scanf("%f %c %f",&a,&s,&b);
switch (s)
{
case'+':
printf("\n=%2.f",a+b);
break;
case'-':
printf("\n=%2.f",a-b);
break;
case'*':
printf("\n=%2.f",a*b);
break;
case'/':
printf("\n=%2.f",a/b);
break;
default:
printf("\nwrong opretor");
}
printf("\n\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