Jump to content

C++ syntax error clarification and some stuff.

A.D.A.M

I got this mcq am wondering if i got the right idea:

3. Analyze the following code:

Code 1:

bool even;
int number;

if (number % 2 == 0)
even = true;
else
even = false;

Code 2:

bool even = (number % 2 == 0);
a. Code 2 has syntax errors.
b. Code 1 has syntax errors.
c. Both Code 1 and Code 2 have syntax errors.
d. Both Code 1 and Code 2 are correct, but Code 2 is better.

 

If i use VS2015 i can't compile without explicitly saying int number=0; (since without it num is undefined.) so answer would be "C" correct? Since 2 definitely can't run without initialisation of num. 

Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to comment
Share on other sites

Link to post
Share on other sites

-SNIP-

 

int number = 0;bool even = (number % 2 == 0) ? true : false;

 

You could do something like that. You should always initialize a variable anyways before doing anything else.

Link to comment
Share on other sites

Link to post
Share on other sites

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?

Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to comment
Share on other sites

Link to post
Share on other sites

yep. you cannot do an operation on number without defining it first.

 

its c

You can do this, just matters on the compiler. Xcode gives no errors with this program.

What are you trying to do with this program though? If you were trying to make it random then this is not the way to do it.

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
Share on other sites

Link to post
Share on other sites

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?

Switch statements are not loops so no. If you were to put it within a do while, while, or for, this would be ok although it would never enter case 7 as x does not ever equal 7.

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
Share on other sites

Link to post
Share on other sites

You can do this, just matters on the compiler. Xcode gives no errors with this program.

What are you trying to do with this program though? If you were trying to make it random then this is not the way to do it.

oh it's just a question i have to solve (spotting code errors and all that). Yeah my friends with dev C++ says it compiles fine...

Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to comment
Share on other sites

Link to post
Share on other sites

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?

 

x+3 = 6, so case 6 would be called. There is no looping in a switch.

Link to comment
Share on other sites

Link to post
Share on other sites

Switch statements are not loops so no. If you were to put it within a do while, while, or for, this would be ok although it would never enter case 7 as x does not ever equal 7.

so how does it ever cout<<y; a 2?  because there isn't a break shouldn't it like well break...

Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to comment
Share on other sites

Link to post
Share on other sites

Ah ok thanks. Another thing if you don't mind.

say we have something like this 

int x;int y;x=3;switch (x+3){    case 6:  y = 0;       case 7:  y = 1;       default: y += 1;     }

this would cause the it to run each case yeah? Until it reaches default?

so first y would be assigned 0 , then 1 ,and then y= y+1 thus 1+1 so 2?

switch just gives your 1 case. it doesnt do any more than that (loops or anything else you think it might do)

Link to comment
Share on other sites

Link to post
Share on other sites

oh it's just a question i have to solve (spotting code errors and all that). Yeah my friends with dev C++ says it compiles fine...

Ah. On Xcode it just defaults ints to 32767. 

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
Share on other sites

Link to post
Share on other sites

so how does it ever cout<<y; a 2?  because there isn't a break shouldn't it like well break...

I just ran it and yes Y comes out to 2 but I don't exactly know why. To my knowledge it should only return the value of Case 6 as it is the value that matches X.

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
Share on other sites

Link to post
Share on other sites

I just ran it and yes Y comes out to 2 but I don't exactly know why. To my knowledge it should only return the value of Case 6 as it is the value that matches X.

the way i understand is its missing the break; so it runs till the end? Since only when i do that will it give the expected result

Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to comment
Share on other sites

Link to post
Share on other sites

the way i understand is its missing the break; so it runs till the end? Since only when i do that will it give the expected result

After reading this http://stackoverflow.com/questions/252489/why-was-the-switch-statement-designed-to-need-a-break and retesting the code it is infact having a fall through occur allowing it to sort of loop through these expressions.

Main Gaming PC - i9 10850k @ 5GHz - EVGA XC Ultra 2080ti with Heatkiller 4 - Asrock Z490 Taichi - Corsair H115i - 32GB GSkill Ripjaws V 3600 CL16 OC'd to 3733 - HX850i - Samsung NVME 256GB SSD - Samsung 3.2TB PCIe 8x Enterprise NVMe - Toshiba 3TB 7200RPM HD - Lian Li Air

 

Proxmox Server - i7 8700k @ 4.5Ghz - 32GB EVGA 3000 CL15 OC'd to 3200 - Asus Strix Z370-E Gaming - Oracle F80 800GB Enterprise SSD, LSI SAS running 3 4TB and 2 6TB (Both Raid Z0), Samsung 840Pro 120GB - Phanteks Enthoo Pro

 

Super Server - i9 7980Xe @ 4.5GHz - 64GB 3200MHz Cl16 - Asrock X299 Professional - Nvidia Telsa K20 -Sandisk 512GB Enterprise SATA SSD, 128GB Seagate SATA SSD, 1.5TB WD Green (Over 9 years of power on time) - Phanteks Enthoo Pro 2

 

Laptop - 2019 Macbook Pro 16" - i7 - 16GB - 512GB - 5500M 8GB - Thermal Pads and Graphite Tape modded

 

Smart Phones - iPhone X - 64GB, AT&T, iOS 13.3 iPhone 6 : 16gb, AT&T, iOS 12 iPhone 4 : 16gb, AT&T Go Phone, iOS 7.1.1 Jailbroken. iPhone 3G : 8gb, AT&T Go Phone, iOS 4.2.1 Jailbroken.

 

Link to comment
Share on other sites

Link to post
Share on other sites

After reading this http://stackoverflow.com/questions/252489/why-was-the-switch-statement-designed-to-need-a-break and retesting the code it is infact having a fall through occur allowing it to sort of loop through these expressions.

so the previous assumption was correct?

Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to comment
Share on other sites

Link to post
Share on other sites

After reading this http://stackoverflow.com/questions/252489/why-was-the-switch-statement-designed-to-need-a-break and retesting the code it is infact having a fall through occur allowing it to sort of loop through these expressions.

 

Yes, if there's no break, a case will fall through to the next. You'll often see it used like this where more than one case runs the same code.

switch (x){    case 1:    case 2:        // Does something if x == 1 || x == 2        break;    case 3:        // Does something else if x == 3        break;    default:        // Does something else if the above cases aren't called}

Keep in mind that while fall through can be intentional, it can also be a bug because people can forget to add the break.

Link to comment
Share on other sites

Link to post
Share on other sites

Yes, if there's no break, a case will fall through to the next. You'll often see it used like this where more than one case runs the same code.

Keep in mind that while fall through can be intentional, it can also be a bug because people can forget to add the break.

So its called a fall? Good thing to know. 

Also, off topic but you proficient in jQuery.If you are I'd appreciate if you would look at my other thread.

http://linustechtips.com/main/topic/456363-need-some-help-with-jquery-and-making-chrome-extensions/#entry6124428

Intel i7 4702MQ| Nvidia GTX 850M| Kingston 16GB DDR3 1600Mhz| Acer VA70_HW (mobo)| 1TB WD Blue| MATSHITA DVD-RAM UJ8E0|1600x900 display|Win 8.1

Intel i5 4690K @Stock| Sapphire 390 Nitro| Hyper X Fury 2x4GB| MSI SLI Krait z97| Noctua Nh-U12S | 850 EVO 256GB| 2TB WD Black | CM V 850w| Enthoo Luxe

If you want to tag me or any person with periods do: @[Member='Name]

Link to comment
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×