Tuesday, November 8, 2011

Control Structures: If, Else

In a simple program each statement or expression is evaluated in sequence:
   
     username = "aedunn"
     password = "password"
     print "Welcome, aedunn!"

In this example (in Python), the variable name "username" is first assigned to the string "aedunn," then the variable name "password" is assigned to the string "password," and finally the string "Welcome, aedunn!" is printed to the screen. However, it might be desirable to be able to control whether a statement is run. This is possible through the use of control structures, which help direct the flow of program execution.

The first type of control structure I'll go over is conditional statements. These determine whether their corresponding code should execute based on a boolean condition: if the boolean condition is true, the code runs. If it is false, the code is skipped. The most common conditional statements are the if/else clauses.


If/Else
General Structure

    if (boolean statement)
        code runs

    else if (another boolean statement)
        code runs

    else
        code runs


Python


    if (username == "aedunn" and password == "password"):
        print "Welcome, aedunn!"

    elif (username == "aedunn" and password != "password"):
        print "Forgot your password?"

    elif (username != "aedunn" and password == "password"):
        print "Forgot your username?"

    else:
        print "Permission Denied!"

C++

    if (username == "aedunn" && password == "password")
    {
        cout << "Welcome, aedunn!" << endl;
    }

    else if (username == "aedunn" && password != "password")
    {
        cout "Forgot your password?" << endl;
    }

    else if (username != "aedunn" && password == "password")
    {
        cout << "Forgot your username?" << endl;
    }

    else
    {
        cout << "Permission Denied!" << endl;
    }


Java


    if (username == "aedunn" && password == "password") {
         System.out.println("Welcome, aedunn!");
    }

    else if (username == "aedunn" && password != "password") {
        System.out.println("Forgot your password?");
    }

    else if (username != "aedunn" && password == "password") {
        System.out.println("Forgot your username?");
    }

    else {
        System.out.println("Permission Denied!");
    }


  • In general, all three languages follow the same structure for if/else statements. However, Python is not as similar to C++ and Java as they are to each other
  • In Python, the colon after the boolean statement is necessary for the program to run
    • Also, the indentation is necessary as well to indicate that the code belongs to the conditional statement above it
    • Finally, in Python "else if" is shortened to "elif"
  • The only difference in if/else in C++ and Java is the print statement
    • There is NO punctuation after the boolean statement. (no semi-colon, no colon, NOTHING!)
    • Each conditional statement (if, else if, else) can control one line of code underneath it
    • If additional lines of code are required then you MUST include the brackets! Brackets are only necessary for more than one line of code, but I've included them here because it is good programming style to include them anyway (for better readability and so you don't get an error if you add another line of code and forget to add brackets).
    • Indentation is not necessary, but (once again) it improves readability.

    No comments:

    Post a Comment