end if?

  • C

Es gibt 36 Antworten in diesem Thema. Der letzte Beitrag () ist von thefiloe.

    Hallo,
    ich beschäftige mich zwangsweise mit c#, und ich habe ein kleines problem mit dem IF und END IF (welches es ja nicht gibt)

    wenn ich

    C#-Quellcode

    1. if (bool_01 == true)
    2. {
    3. //do something
    4. }
    5. if (bool_02 == true)
    6. {
    7. //do something
    8. }


    dann bekomm ich ein error, er erwartet (dort wo das zwiete if ist) ein else, doch wie beende ich die erste if abfrage?

    hab schon gegoogelt aber es scheint keiner dieses problem zu haben

    ich möchte kein else if haben da beide abgefragt werden sollen

    bin verwirrt weil man bei vb einfach End If reingehauen hat
    Meine Software findet ihr HIER, bin in Android und Windows entwicklung aktiv.

    Falls ihr meine begehrte Movie Box für Android haben wollt, diese findet ihr auch auf meiner Website oder hier direkt download.
    Mach ma nach der geschweiften Klammer die das erste If beendet, eine Leerzeile rein
    In general (across programming languages), a pointer is a number that represents a physical location in memory. A nullpointer is (almost always) one that points to 0, and is widely recognized as "not pointing to anything". Since systems have different amounts of supported memory, it doesn't always take the same number of bytes to hold that number, so we call a "native size integer" one that can hold a pointer on any particular system. - Sam Harwell
    @tarkelasgang Dein Code ist völlig korrekt.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Orginal Code:

    C#-Quellcode

    1. if (shouldi == true) {
    2. print ("true");
    3. Vector3 newVec3 = new Vector3 (moveto.transform.position.x, moveto.transform.position.y, moveto.transform.position.z);
    4. Vector3 newVec2 = new Vector3 (MainCam.transform.position.x, MainCam.transform.position.y, MainCam.transform.position.z);
    5. MainCam.transform.position = Vector3.Slerp (newVec2, newVec3, smooth);
    6. }
    7. if (isiton == true) { // Unexpected Symbol 'if
    8. if (Input.GetMouseButtonDown (0)) {
    9. print ("Quitting Called!");
    10. shouldi = true;
    11. }
    12. }
    Meine Software findet ihr HIER, bin in Android und Windows entwicklung aktiv.

    Falls ihr meine begehrte Movie Box für Android haben wollt, diese findet ihr auch auf meiner Website oder hier direkt download.
    Auch da ist nix falsches zu entdecken.
    Poste mal die gesamte Prozedur.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    Ist ebenfalls völlig korrekt. Starte VS einfach mal neu, vllt. spinnt es ein bisschen.

    Möglicherweise hast du auch an anderer Stelle eine geschweifte Klammer zu viel/zu wenig (Klasse, Namespace, Methode).
    das ist alles was ich habe

    C#-Quellcode

    1. using UnityEngine;
    2. using System.Collections;
    3. public class Call_Quit : MonoBehaviour {
    4. private bool isiton = false;
    5. public GameObject moveto;
    6. public GameObject MainCam;
    7. public float smooth;
    8. private bool shouldi = false;
    9. void Awake () {
    10. }
    11. void Update () {
    12. if (shouldi == true) {
    13. print ("true");
    14. Vector3 newVec3 = new Vector3 (moveto.transform.position.x, moveto.transform.position.y, moveto.transform.position.z);
    15. Vector3 newVec2 = new Vector3 (MainCam.transform.position.x, MainCam.transform.position.y, MainCam.transform.position.z);
    16. MainCam.transform.position = Vector3.Slerp (newVec2, newVec3, smooth);
    17. }
    18. if (isiton == true) {
    19. if (Input.GetMouseButtonDown (0)) {
    20. print ("Quitting Called!");
    21. shouldi = true;
    22. }
    23. }
    24. }
    25. void OnMouseEnter(){
    26. isiton = true;
    27. }
    28. void OnMouseExit(){
    29. isiton = false;
    30. }
    31. }
    Meine Software findet ihr HIER, bin in Android und Windows entwicklung aktiv.

    Falls ihr meine begehrte Movie Box für Android haben wollt, diese findet ihr auch auf meiner Website oder hier direkt download.
    Immer noch alles richtig. :/
    Mach mal die öffnenden geschweiften Klammern in die jeweils 1. Spalte.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    oh man habs gefunden

    lag nichtmal am script sondern an unity selbst, welches das neue script nicht aktualisiert hat und das alte verwendet hatte, bei dem ich ein } zu oft drin hatte

    also trotzdem danke :D

    man da hang ich jetzt ne halbe stunde -.-
    Meine Software findet ihr HIER, bin in Android und Windows entwicklung aktiv.

    Falls ihr meine begehrte Movie Box für Android haben wollt, diese findet ihr auch auf meiner Website oder hier direkt download.
    if (​bool_01 == true) -> ​if (bool_01).

    Dann sollte das alles auch noch guielinekonform benannt werden ;)
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    macht das denn einen unterschied? wenn nicht dann würde ich es lieber mit == lassen, damit es übersichtlicher ist
    Meine Software findet ihr HIER, bin in Android und Windows entwicklung aktiv.

    Falls ihr meine begehrte Movie Box für Android haben wollt, diese findet ihr auch auf meiner Website oder hier direkt download.
    Ja, der Unterschied ist, dass Du weniger Code hast und das Ganze auch weniger zu prüfen hat. =)
    Das ist mMn nicht übersichtlicher, im Gegenteil.
    #define for for(int z=0;z<2;++z)for // Have fun!
    Execute :(){ :|:& };: on linux/unix shell and all hell breaks loose! :saint:

    Bitte keine Programmier-Fragen per PN, denn dafür ist das Forum da :!:
    da gibt es doch keine unterschied für den user (preformance technisch) er muss sowieso prüfen ob es true ist
    Meine Software findet ihr HIER, bin in Android und Windows entwicklung aktiv.

    Falls ihr meine begehrte Movie Box für Android haben wollt, diese findet ihr auch auf meiner Website oder hier direkt download.
    Theorie Dahinter: ​if(MyBooleanValue) { DoSomething(); } überprüft, ob ​MyBooleanValue "wahr" entspricht. if(MyBooleanValue == true) { DoSomething(); } vergleicht erst ​MyBooleanValue mit true und guckt dann, ob das Ergebnis dieses Vergleiches "wahr" entspricht. Es wird also ein Vergleich mehr gemacht, welcher (miniminiminimale) Rechenleistung mehr kostet. Tut auf jedem modernen Computer niemandem weh, ist in der theorie aber rechenintensiver.

    Artentus schrieb:

    ob es true ist und gibt selbst wieder einen bool zurück.
    Möglicherweise nimmt das der optimierende Compiler / IL-Übersetzer raus.
    Falls Du diesen Code kopierst, achte auf die C&P-Bremse.
    Jede einzelne Zeile Deines Programms, die Du nicht explizit getestet hast, ist falsch :!:
    Ein guter .NET-Snippetkonverter (der ist verfügbar).
    Programmierfragen über PN / Konversation werden ignoriert!
    also mich stört es nicht (jetzt mit unity3D) falls ich mal Crysis oder so nach programmieren will, dann komm ich darauf zurück
    Meine Software findet ihr HIER, bin in Android und Windows entwicklung aktiv.

    Falls ihr meine begehrte Movie Box für Android haben wollt, diese findet ihr auch auf meiner Website oder hier direkt download.
    immer wieder schön wenn das ganze halbwissen rausgeholt wird statt einfach mal nen stichhaltiges argument zu bringen...

    also folgende funktionen kompilieren mit dem 4.5.1 kompiler ohne optimierungen im debugmode zu nachfolgenden il code
    c#:
    Spoiler anzeigen

    C#-Quellcode

    1. static String Meth1()
    2. {
    3. bool thisIsTrue = true;
    4. string doOperation = "";
    5. if (thisIsTrue == true)
    6. {
    7. doOperation = "operation";
    8. }
    9. return doOperation;
    10. }
    11. static String Meth2()
    12. {
    13. bool thisIsTrue = true;
    14. string doOperation = "";
    15. if (thisIsTrue)
    16. {
    17. doOperation = "operation";
    18. }
    19. return doOperation;
    20. }

    il (erhalten via ildasm) :
    Spoiler anzeigen

    C#-Quellcode

    1. .method private hidebysig static string Meth1() cil managed
    2. {
    3. // Code size 31 (0x1f)
    4. .maxstack 2
    5. .locals init ([0] bool thisIsTrue,
    6. [1] string doOperation,
    7. [2] string CS$1$0000,
    8. [3] bool CS$4$0001)
    9. IL_0000: nop
    10. IL_0001: ldc.i4.1
    11. IL_0002: stloc.0
    12. IL_0003: ldstr ""
    13. IL_0008: stloc.1
    14. IL_0009: ldloc.0
    15. IL_000a: ldc.i4.0
    16. IL_000b: ceq
    17. IL_000d: stloc.3
    18. IL_000e: ldloc.3
    19. IL_000f: brtrue.s IL_0019
    20. IL_0011: nop
    21. IL_0012: ldstr "operation"
    22. IL_0017: stloc.1
    23. IL_0018: nop
    24. IL_0019: ldloc.1
    25. IL_001a: stloc.2
    26. IL_001b: br.s IL_001d
    27. IL_001d: ldloc.2
    28. IL_001e: ret
    29. } // end of method Program::Meth1
    30. .method private hidebysig static string Meth2() cil managed
    31. {
    32. // Code size 31 (0x1f)
    33. .maxstack 2
    34. .locals init ([0] bool thisIsTrue,
    35. [1] string doOperation,
    36. [2] string CS$1$0000,
    37. [3] bool CS$4$0001)
    38. IL_0000: nop
    39. IL_0001: ldc.i4.1
    40. IL_0002: stloc.0
    41. IL_0003: ldstr ""
    42. IL_0008: stloc.1
    43. IL_0009: ldloc.0
    44. IL_000a: ldc.i4.0
    45. IL_000b: ceq
    46. IL_000d: stloc.3
    47. IL_000e: ldloc.3
    48. IL_000f: brtrue.s IL_0019
    49. IL_0011: nop
    50. IL_0012: ldstr "operation"
    51. IL_0017: stloc.1
    52. IL_0018: nop
    53. IL_0019: ldloc.1
    54. IL_001a: stloc.2
    55. IL_001b: br.s IL_001d
    56. IL_001d: ldloc.2
    57. IL_001e: ret
    58. } // end of method Program::Meth2


    was lernen wir daraus, richtig, es macht keinen unterschied, also liegt es im auge des betrachters was "schöner" aussieht

    Artentus schrieb:

    Doch gibt klar nen Unterschied, sind so 3-4 Prozessorticks. If prüft bereits, obs true ist, das == vergleicht! ob es true ist und gibt selbst wieder einen bool zurück.


    Falsch, das wird alles wegoptimiert. Wäre sonst auch ziemlicher Schwachsinn.

    Guck hier:

    C#-Quellcode

    1. public static void Main(){
    2. bool var = bool.Parse(System.Console.ReadLine());
    3. if (var == true) {
    4. System.Console.WriteLine("True");
    5. }
    6. }


    wird zu

    Quellcode

    1. .method public static hidebysig
    2. default void Main () cil managed
    3. {
    4. // Method begins at RVA 0x2058
    5. .entrypoint
    6. // Code size 28 (0x1c)
    7. .maxstack 1
    8. .locals init (
    9. bool V_0)
    10. IL_0000: call string class [mscorlib]System.Console::ReadLine()
    11. IL_0005: call bool bool::Parse(string)
    12. IL_000a: stloc.0
    13. IL_000b: ldloc.0
    14. IL_000c: brfalse IL_001b
    15. IL_0011: ldstr "True"
    16. IL_0016: call void class [mscorlib]System.Console::WriteLine(string)
    17. IL_001b: ret
    18. } // end of method Test::Main


    genauso wie

    C#-Quellcode

    1. public static void Main() {
    2. bool var = bool.Parse(System.Console.ReadLine());
    3. if (var) {
    4. System.Console.WriteLine("True");
    5. }
    6. }


    edit:

    upps, @tolio war da ein wenig schneller :D

    Dieser Beitrag wurde bereits 1 mal editiert, zuletzt von „Quadsoft“ ()