May 12, 2025

3.4 App Inventor: Coding Conditionals

0

Okay team, Muzukuke! We’ve talked about understanding your users and setting up your business structure. Now, let’s get back into the exciting part – the actual coding with MIT App Inventor!

Today, we’re learning about something fundamental, something that makes your apps smart: Conditionals.

Think about it: apps don’t just do the same thing every single time, right? They react! They respond to what you do, or what’s happening inside the app. Conditionals are how we give our apps the power to make decisions.

Lesson Topic: App Inventor – Coding Conditionals

Part 1: Thinking Like Your App – What are Conditions?

Imagine real life here in Jinja. You make decisions based on conditions all the time:

  • If it starts raining heavily while you’re walking home from school, then you run for shelter or pull out an umbrella (if you remembered it!).
  • If your phone battery level is below 10%, then you know you need to find a charger soon.
  • If it’s Saturday, then you know there’s no regular school timetable.
  • If you check your MTN Mobile Money balance and it’s low, then you decide maybe not to buy that extra data bundle today.

These “If… then…” scenarios are based on Conditions (is it raining? is the battery low? is it Saturday? is the balance low?). You check the condition (look outside, check your phone), determine if it’s true or false, and then take an action.

Our apps need to do the same! For example:

  • If a user types the wrong password, then the app shows an error message.
  • If a player reaches a score of 100 in a game, then the app displays “You Win!”.

Conditionals are the building blocks for this kind of decision-making logic in all programming, including App Inventor.

Part 2: The Decision Blocks in App Inventor (Conditional Blocks)

In App Inventor, you’ll find the blocks for making decisions in the Control drawer. Let’s look at the main ones:

  1. The Basic if…then Block:
    1. (Imagine the simple ‘if then’ block here)
    1. How it works: You snap a condition (something that can be true or false, like Score = 100 or IsRaining = true) into the if part.
    1. If the condition is true, the blocks you put inside the then slot will run.
    1. If the condition is false, the app just skips whatever is in the then slot and moves on.
    1. Example: If PlayerScore = 50, then set WinnerLabel.Text = “Halfway There!” (If the score isn’t 50, nothing happens here).

  1. The if…then…else Block:
    1. (Imagine the ‘if then else’ block here)
    1. How it works: This gives you two options.
    1. If the condition is true, the blocks in the then slot run.
    1. If the condition is false (the “else” situation), the blocks in the else slot run instead.
    1. Example (Password Check):
  • The if…then…else if…then…else Block:
    • (Imagine the ‘if then else if then else’ block here)How it works: This lets you check multiple conditions in order.It checks the first if condition. If it’s true, it runs the first then code and skips the rest of the entire block.If the first condition is false, it moves to the else if condition. If that one is true, it runs the second then code and skips the rest.You can add more else if parts by clicking the blue gear icon on the block!If none of the if or else if conditions are true, the code in the final else slot runs (if you have one).
    • Example (Grading):
  • VERY IMPORTANT: The order matters! In the grading example, if a student scored 90, the first condition (> 85) is true. It sets the grade to A and immediately jumps out of the block. It doesn’t even check if the score is also greater than 70 or 50. Design your logic carefully!

(Mentor Note: You might see some unrelated text about casinos in the original lesson material online. Please just ignore that part; it’s not relevant to our App Inventor coding work today!)

Part 3: Let’s Build! The Counting Game (Activity – 20 mins)

Time to practice! We’ll use a simple game called the Counting Game.

  • The Goal: Players keep adding numbers (e.g., by clicking buttons for +1, +5, +10). The aim is to reach a total score of exactly 100.
  • Winning/Losing:
    • If your total hits 100 exactly, you win!
    • If your total goes over 100, you lose!
    • If your total is less than 100, the game continues.

Your Task:

  1. Open the Starter Project: Click the link in the lesson materials (“Load App Into App Inventor”) to get the basic layout. Make your own copy.
  2. Follow the Worksheet: Open the worksheet linked in the lesson. It will guide you step-by-step on adding the conditional logic.
  3. Code the Logic: You’ll primarily be working in the event handlers for the buttons that add numbers. Inside those blocks, after adding the number to the total score variable, you need to add conditional blocks (if…else if…else) to check:
    1. if TotalScore = 100 then … (display win message)
    1. else if TotalScore > 100 then … (display lose message)
    1. else … (just update the score label on the screen, game continues)
  4. Try it First! Really attempt to build the logic yourself using the worksheet.
  5. Check the Solution (Later): If you get stuck, or after you’ve tried your best, you can click the “Uncover Solution” button in the lesson to see one possible way to do it.

Part 4: Extra Challenge!

Finished the main game? Want to add more?

  • Add a Reset Button:
    • Add a new button to the screen labelled “Reset”.
    • Code its Click event handler so that it sets the TotalScore variable back to 0.
    • Make sure it also clears any “Win” or “Lose” messages from the screen, ready for a new game!

Part 5: Thinking About Decisions (Reflection)

Think about your day. What are some other decisions you make based on conditions? The lesson gives examples like:

  • If it’s cold outside, then wear a jacket.
  • If your phone is dead, then charge it.
  • If you are sick, then go to the doctor, else go to school (or maybe stay home!).
  • If your clothes are dirty, then put them in the wash, else put them away.

Can you think of 2-3 more examples from your life here in Uganda? Share them with your team!

Part 6: Quick Review (Key Terms)

  • Conditions: A state or situation the app checks (e.g., score value, text input, checkbox checked). Usually results in true or false.
  • Conditional statements/blocks: The App Inventor blocks (if, if/else, if/else if) that let your app make decisions based on conditions.
  • if/else: The common structure: If this condition is true, do this. Else (if it’s false), do that other thing.

Part 7: More Practice

If you want to see conditionals used in another context, the lesson links to an example by Professor Dave Wolber using conditionals to start and pause a sound in a Soundboard app. Worth checking out if you have time!


Conclusion

Webale nnyo! You’ve just learned one of the most powerful concepts in programming! Conditionals are what make apps feel interactive and intelligent. Without them, apps would be very boring.

Mastering if, if/else, and if/else if blocks in App Inventor is key to building useful and engaging apps for your Technovation project.

Your Task Now: Dive into the Counting Game activity. Work with your team, follow the worksheet, and really try to build that decision-making logic. Don’t forget the challenge!

Keep experimenting, keep coding, and don’t be afraid to ask questions. You’re building amazing things! Muwereza! (Keep going!)

Leave a Reply

Your email address will not be published. Required fields are marked *