Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Conditional Operator vs if...else
New Posts  All Forums:Forum Nav:

Conditional Operator vs if...else

post #1 of 10
Thread Starter 
Alright, so I'm lost. I am pretty new to coding, actually this is my very first coding class So I'm taking a Java class at the moment, and came across a weird statement in the text. it says

"We'll see that conditional expressions can be used in some situations where if...else statements cannot."

I know I'm a programming noob here, so maybe I missed something. I posted this question on my class discussion forum, and after 4 days no one has responded, not even my prof unfortunately. So that's my question, how is it possible that an if...else expression cannot be used and only the conditional operator may be used? From my understanding of it, the conditional operator is basically an if...else statement, so would it be a special circumstance where the conditional operator is not used as a basic if...else statement? But if that is the case, why would the book mention it while introducing the if...else statements? Any help/insight would be beneficial, thanks much!
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
post #2 of 10
I always consider(ed) conditional operators to be short-hand if-else. They are definitely cooler and look 10x more nerdy to use, but I fail to see something that they can do that nesting if-else statements can't. :/. What was the particular scenario?
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
post #3 of 10
Thread Starter 
see, that is part of my problem. they said that in chapter 3, we're now in chapter 7 and i still have yet to see the book mention any example of it. they just kinda threw it out there, and then left it alone. the only couple examples they showed with the conditional operator were basic if...else statements, like

System.out.println( studentGrade >= 60 ? "Passed" : "Failed");

which is a basic if...else statement.
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
post #4 of 10
Yeah, that's definitely a basic if-else. Unless they mean that you couldn't write the above statement the same way using if-else...? i.e., it would be longer? :s ...
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
The Dark Knight
(13 items)
 
Rebirth
(13 items)
 
 
CPUMotherboardGraphicsRAM
i5 2500K. 4.5Ghz (Summer Clocks) ASUS P8P67 Pro ( Rev 3.0 ) MSI 6970 Lightning (downclocked) CFX with MSI 6... 8GB G.Skill Sniper 1600Mhz 
Hard DriveOptical DriveOSMonitor
1 TB Spinpoint F3 +2TB F4(backups) Random LG drive Win7 SP1 64bit ASUS VH236H @1080p 
KeyboardPowerCase
Saitek Gaming keyboard ... >_> Corsair TX750 Rosewill Gear x3 
CPUMotherboardGraphicsRAM
C2D P8600 @ 2.4Ghz (undervolted) Crappy dell Studio XPS one 4670M (1GB) I don't remember... 
Hard DriveOptical DriveOSMonitor
Crucial M4 4x Blu-ray ROM Win7 SP1 64bit 15.6" WLED @ 1080p 
KeyboardPower
Old Saitek Gaming Keyboard 90W adapter 
  hide details  
Reply
post #5 of 10
Thread Starter 
true, but it says that it can be used in some situations where an if...else cannot be used...i think that's what is throwing me for a loop here, lol
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
post #6 of 10
That does not make sense: Conditionals are just if..else.

I would go to the TA or teacher directly during office hours.
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
Once again...
(13 items)
 
  
CPUMotherboardGraphicsRAM
i7 920 [4.28GHz, HT] Asus P6T + Broadcom NetXtreme II VisionTek HD5850 [900/1200] + Galaxy GT240 2x4GB G.Skill Ripjaw X [1632 MHz] 
Hard DriveOSMonitorKeyboard
Intel X25-M 160GB + 3xRAID0 500GB 7200.12 Window 7 Pro 64 Acer H243H + Samsung 226BW XARMOR-U9BL  
PowerCaseMouseMouse Pad
Antec Truepower New 750W Li Lian PC-V2100 [10x120mm fans] Logitech G9 X-Trac Pro 
  hide details  
Reply
post #7 of 10
Thread Starter 
Quote:
Originally Posted by DuckieHo View Post
That does not make sense: Conditionals are just if..else.

I would go to the TA or teacher directly during office hours.
haha, on-line classes ftw, no office hours

I did just get an email back from him though, his thoughts were that ..."it seems that Deitel is trying to make the distinction that syntactically one cannot use the if..else in places the Conditional is used." i guess the book just chose a really poor way to word it if that was what they meant to say...
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
First Time Build
(20 items)
 
  
CPUMotherboardGraphicsGraphics
Intel Core i7 920 MSI X58 Pro-E (MS-7522) EVGA GeForce GTX 460 EVGA GeForce GTX 460 
GraphicsRAMRAMRAM
Galaxy GeForce GTX 460 Corsair  Corsair  Corsair  
Hard DriveOptical DriveCoolingOS
Seagate Barracuda 7200.12 Sony DVD+-RW Noctua NH-D14 Windows 7 64bit 
MonitorMonitorKeyboardPower
Acer P215H Acer P221W Dynex Silverstone OP1000-E 1kW PSU 
CaseMouseMouse PadAudio
Cooler Master HAF 932 Dynex Dynex ASUS Xonar DG 
  hide details  
Reply
post #8 of 10
Yeah that is strange. The word IF it's self is basically a word for a condition. lol.
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
Dolans Mate
(15 items)
 
  
CPUMotherboardGraphicsRAM
Intel i5 760 Gigabyte P55A-UD3 Gainward GTX 560Ti Mushkin Radioactive (4GB) 
Hard DriveCoolingOSMonitor
160GB Seagate x 2 Cooler Master Hyper 212 EVO Windows 7 Ultimate 64-bit LG LED Flatron E2250V x 2 
KeyboardPowerCaseMouse Pad
Das Keyboard Utimate S  Silverstone Strider 500w NZXT Guardian 921 Razer Goliathus Fragged Alpha 
Audio
Logitech X-540s 
  hide details  
Reply
post #9 of 10
The conditional operator is NOT always replaceable by an if/else statement

Code:
int& a;
if (b == 2)
  a = x1;
else
  a = x2;
The conditional operator is an expression, whereas if-else is a statement. An expression can be used where a statement can be used, but the opposite is not true.

However, you CAN always replace the reference

Code:
int* pa;
if( b == 2 )
    pa = &x1;
else
    pa = &x2;
int& a = *pa;
And here

Code:
int* pa;
switch( b == 2 )
{
case true:
    pa = &x1;  break;
default:
    pa = &x2;
}
int& a = *pa;
But why not just use the conditional operator in this case.

Quote:
int& a = (b == 2? x1 : x2);
post #10 of 10
EDIT: Whoops, didn't read the whole thread, post above me pretty much says the same thing.


The use of the ternary operator (?) is also helpful with const variables

This code will not compile, because a const reference must be assigned to immediately
Code:
const myRef&;
bool bUseFirst = true;
if ( bUseFirst )
{
     myRef = someThing;
}
else
{
     myRef = someThingElse;
}
This will compile:
Code:
bool bUseFirst = true;

const myRef& = ( bUseFirst ? sometThing : someThingElse );
As others have said though, if does perform pretty much identically to an if...else statement.
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
Foldatron
(17 items)
 
Mat
(10 items)
 
Work iMac
(9 items)
 
CPUMotherboardGraphicsGraphics
i7 950 EVGA x58 3-way SLI EVGA GTX 660ti GTX 275 
RAMHard DriveHard DriveHard Drive
3x2GB Corsair Dominator DDR3-1600 80GB Intel X25-M SSD 2TB WD Black 150GB WD Raptor 
Hard DriveOSMonitorKeyboard
2x 150GB WD V-raptor in RAID0 Win7 Home 64-bit OEM 55" LED 120hz 1080p Vizio MS Natural Ergonomic Keyboard 4000 
PowerCase
750W PC P&C Silencer CoolerMaster 690 
CPUGraphicsRAMHard Drive
Intel Core i5 2500S AMD 6770M 8GB (2x4GB) at 1333Mhz 1TB, 7200 rpm 
Optical DriveOSMonitorKeyboard
LG 8X Dual-Layer "SuperDrive" OS X Lion 27" iMac screen Mac wireless keyboard 
Mouse
Mac wireless mouse 
CPUGraphicsRAMHard Drive
i7-2600K AMD 6970M 1GB 16GB PC3-10600 DDR3 1TB 7200rpm 
Hard DriveOptical DriveOSMonitor
256GB SSD 8x DL "SuperDrive" OS X 10.7 Lion 27" 2560x1440 iMac display 
Monitor
27" Apple thunderbolt display 
  hide details  
Reply
New Posts  All Forums:Forum Nav:
  Return Home
  Back to Forum: Coding and Programming
Overclock.net › Forums › Software, Programming and Coding › Coding and Programming › Conditional Operator vs if...else