473,466 Members | 1,286 Online
Bytes | Software Development & Data Engineering Community
Create Post

Home Posts Topics Members FAQ

conditional program without if else

1 New Member
a labour work in a factory if he works 40 hours he will pay 1000 for one hour, if he works more than one hour he will pay 1500 for above hours. How we do it without using if else condition in C++.
Nov 11 '17 #1
2 2035
weaknessforcats
9,208 Recognized Expert Moderator Expert
You have to use an if-else. Now you can disguise this by using code that doesn't explicitly have "if else" in it. Look up the ternary operator:

Expand|Select|Wrap|Line Numbers
  1. (x) ? a : b;
x is what you are testing for. If x is true then a. If x is false then b.

So you could say:

Expand|Select|Wrap|Line Numbers
  1. (hours > 1) ? 1500, 1000;
Then if the hours are greater than 1, the pay rate is 1500. Otherwise the pay rate is 1000.

This is still if-else. The ternary operator is obsolete except in classroom problems like this one. It comes from the belief that the less code you write, the compiler will generate fewer instructions. This was OK back in the days of very tiny computers. With todays gigabyte world you never use this.
Nov 12 '17 #2
donbock
2,426 Recognized Expert Top Contributor
You can rephrase the problem as:
  • Base pay is 1000 for each hour worked.
  • Bonus pay is additional 500 (1500-1000) for each bonus hour (above 40) worked.

You are given total_hours.
You can compute bonus_hours with only one if (no else) to trap negative bonus_hours.
You can compute total_pay from these hours without any if statements.
Nov 13 '17 #3

Sign in to post your reply or Sign up for a free account.

Similar topics

4
by: Tian | last post by:
In Windows, I have been simply using os.system() to run command line program in python. but there will be a black console window. How can I run the program without invoking that window? i guess...
5
by: PengYu.UT | last post by:
Hi, I heard that debug some C++ templates is very difficult. I'm wondering whether it is possible to compile C++ program with templates to pure C or C++ program without templates? Best...
24
by: gswork | last post by:
Let's write a c program, without knowing what it does... Some of you may recall Jim Roger's excellent series of posts (on comp.programming) exploring the implementation of common software...
92
by: Raghavendra R A V, CSS India | last post by:
hie.. Do any one knows how to write a C program without using the conditional statements if, for, while, do, switch, goto and even condotional statements ? It would be a great help for me if...
16
by: leeaby | last post by:
Hi, I will appreciate your assistance. Can we write a c code which do not contain main() I have heard that this is possible. Is it really possible? Thanks for your help in advance lee
8
by: yxq | last post by:
Hello everyone I ever found a website that provide the technology of running program without installing framework, but now i forgot the URL, does everyone know the technology and the website? ...
37
by: Spidey | last post by:
How can to write a c program without a main() so i can compile and run it
13
by: robinsonreyna | last post by:
Hi everyone Is it possible to write a program which do not have a main() function. The program should compile and run. Please give sample code to do this.
2
by: =?Utf-8?B?d2lubGlu?= | last post by:
Hello 1) I agree that useing classes is the best way to use VB.net. The question is how do you write a VB program without using classes? 2) For my own edification where can I see an actual...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers,...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...
0
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The...
0
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated ...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.