ConceptStep · learning platform for computer science

IF/ELSE: two alternative paths

C programming · Lesson

C · lesson 7 / 27

IF/ELSE: two alternative paths

Use a condition to choose exactly one of two branches.

1 · Observe and predict

The concept

With if/else exactly one branch executes and the two paths rejoin afterwards.

Mental model:

INPUT n → n % 2 == 0? → EVEN / ODD

2 · Flowchart First

Follow the same algorithm as a flowchart

Use Start and Step: when the active block changes, ConceptStep also highlights the C lines representing that same step.

Loading flowchart…
3 · Flowchart ↔ C · Run in Page

Lab 1 — Run the example program

Run C executes only the example shown on the left, using stdin on the right. The coding challenge below has a separate editor and test button.

IF/ELSE: even or odd Start the flowchart to synchronize the code
                        1#include <stdio.h>
                        2 
                        3int main(void)
                        4{
                        5    int n;
                        6    scanf("%d", &n);
                        7 
                        8    if (n % 2 == 0)
                        9    {
                        10        printf("PARI\n");
                        11    }
                        12    else
                        13    {
                        14        printf("DISPARI\n");
                        15    }
                        16 
                        17    return 0;
                        18}
Ready. Make a prediction before running.

                    
                

Lab 2 — Coding challenge: write your code — Two branches

Read an integer; print PARI or DISPARI.

This is a different program from lab 1. Complete the starter code: Check all test cases compiles this editor and tests every input, including boundary cases.

4 · Guided practice

Check your prediction

With input 12 which branch is executed?

Challenge 1 / 5 · Understanding

Progress: 0 / 5 · correct: 0

Your competency map

What you need to know for IF/ELSE: two alternative paths

ConceptStep separates what is required from what is recommended or useful as reinforcement. Only required prerequisites block your progress.

Current topic Strong skill Review due Needs reinforcement To be mastered Optional support

Relationships: solid line = required · dashed line = recommended · dotted line = reinforcement.

GUIDED LEARNING PATHS

Want to choose the next skill to develop?