how basic programming concepts translate in real software?

thanks for suggestion , this is what i did and i can say it is my first program i do (if we exclude hello world) in my life without tutorials!

if anyone has suggestion for simple programs i can do for learning purpose , feel free to suggest them
Very nice start.

As for what to try programming next: how about a simple task you do frequently on your computer? Not only do you continue your learning, but you will end up with something of practical use.
 
I'll provide an example.

Code:
if ( myvalue == something ) {
  printf("Hurray!\n");
} else {
  printf("Not the same\n");
}

Notice how the printf lines within the if .. else .. are neatly indented? Doesn't change how the program works (it will if it was Python!), it's more a visual cue for the person looking at your code. It simply makes it a bit easier to read. For a single line it won't matter much, it will be more clear when the sections in between get larger.

There's also another way to do this, and can be a very hot stylistic topic:
Code:
if ( myvalue == something )
{
  printf("Hurray!\n");
}
else
{
  printf("Not the same\n");
}
Same code, different style guide. I personally use the first but I've seen style guides that want the second form. For your own personal projects it's not going to matter, as long as you stay consistent.
Thank you, SirDice ! It's nice to run into someone else who understands SANE screen real-estate usage! I've grown sooo intolerant of modern "single token per line" coding standards.
 
here comes the real software, shanke.sh (the snake game as a shell script) :)
1789149527423.png

sh:
#!/bin/sh
# shell snake game
# license LOL
clear
echo "use q/a/o/p to move up/down/left/right or run with "vi" arg for vi keys"
echo press ENTER
read -t 2 crap
TP=$(tput cm 123 123)
tPRE=${TP%%124*}
tEND=${TP##*124}
TMP=${TP#*124}
tMID=${TMP%124*}


COLS=$(tput co)
ROWS=$(tput li)


MX=$(($COLS-2))
[ $MX -gt 60 ] && MX=60
MY=$((ROWS-2))
[ $MY -gt 20 ] && MY=20
PX=$(($MX/2))
PY=$(($MY/2))
SQ="$PY,$PX"
L_KEY="o"
R_KEY="p"
U_KEY="q"
D_KEY="a"
if [ "$1" = "vi" ];then
L_KEY="h"
R_KEY="l"
U_KEY="k"
D_KEY="j"
fi
SEED=$(date +%s)
trap 'die "Script interrupted by user!"' INT
tput vi
clear

die() {
    tput vs
    print_at2 $MY 0 " "
    echo $1
    stty sane </dev/tty
    exit
}

rand() {
  SEED=$(((1103515245 * $SEED + 12345) % 2147483648));
  RANDO=$((SEED % $1));
}

gen_apple() {
while true;do
 rand $((MY-3))
 A_Y=$(($RANDO+2))
 rand $((MX-3))
 A_X=$(($RANDO+2))
 A_ALL="$A_Y,$A_X"
     OK=1
    for combo in $SQ;do
        if  [ $combo = $A_ALL ];then
            OK=0
               break
        fi
    done
   [ $OK -eq 1 ] && break
done
print_at2 $A_Y $A_X "*"
}

check_wall() {
 [ $1 -le 1 -o $1 -ge $MY ] && return 1
 [ $2 -le 1 -o $2 -ge $MX ] && return 1
 return 0
}

# 1 up 2 down 3 left 4 right
move_head() {
 DEAD=0
 OPX=$PX
 OPY=$PY
 case "$dir" in
     d) PY=$(($PY+1));;
     u) PY=$(($PY-1));;
     l) PX=$((PX-1));;
    r) PX=$((PX+1));;
    *) return;;
 esac
 print_at2 $((MY+1)) 40 "Y:$PY X:$PX  "
 check_wall $PY $PX

 if [ $? -ne 0 ];then
  DEAD=1
  HIT="the wall"
 fi
 NHEAD="$PY,$PX"
 for combo in $SQ;do
  if [ "$combo" = "$NHEAD" ];then
    DEAD=1
    HIT="yourself"
    break
  fi
 done
 SQ="$NHEAD $SQ"
}

draw_snake() {
    TAIL=${SQ##* }
    Y=${TAIL%,*}
    X=${TAIL#*,}
    if [ $A_Y -eq $PY -a $A_X -eq $PX ]; then
    # we ate an apple
        SCORE=$(($SCORE+1))
        gen_apple
        print_at2 $((MY+1)) 0 "SCORE: $SCORE"
    else
    #    delete tail
        SQ=${SQ% *}
        print_at2 $Y $X " "
    fi
    C=O
    nseg=0
    for combo in $SQ;do
          Y=${combo%,*}
          X=${combo#*,}
          print_at2 $Y $X $C
          C=o
          nseg=$(($nseg+1))
          # we really only need to draw the 1st 2 segs
          [ $nseg -ge 2 ] && break
    done

}
print_at2() {
        ZZ=$tPRE$1$tMID$2$tEND
        echo -n "$ZZ$3"
}
for i in $(jot $MX);do print_at2 0 $i -;print_at2 $MY $i -;done
for i in $(jot $MY);do print_at2 $i 0 "|";print_at2 $i $MX "|";done
print_at2 1 1 +
print_at2 1 $MX +
print_at2 $MY 1 +
print_at2 $MY $MX +
print_at2 $(($MY+2)) 10 "Left=$L_KEY Right=$R_KEY Up=$U_KEY Down=$D_KEY"
gen_apple
dir=x
stty -icanon -echo;dd ibs=1 obs=1 cbs=1 conv=unblock 2>/dev/null | \
while true;do
read -t 0 inp
sleep 0.1
if [ "$inp" = "" ];then
 NOOP=1
#echo -n "."
else
case "$inp" in
    $L_KEY) [ $dir != "r" ] && dir=l;;
    $D_KEY) [ $dir != "u" ] && dir=d;;
    $U_KEY) [ $dir != "d" ] && dir=u;;
    $R_KEY) [ $dir != "l" ] && dir=r;;
esac
#echo read $inp
inp=""
fi
move_head
draw_snake
[ $DEAD -eq 1 ] && die "You hit $HIT !"
done
 
No need to change this. Flowcharts may help, but if you do not need that help, better.
That was real long ago. I hated it because the hand drawing is way too much work and you can't get anything that's a little complex reasonably on paper. Also program structure diagrams, even worse.
 
here comes the real software, shanke.sh (the snake game as a shell script) :)
View attachment 27269
sh:
#!/bin/sh
# shell snake game
# license LOL
clear
echo "use q/a/o/p to move up/down/left/right or run with "vi" arg for vi keys"
echo press ENTER
read -t 2 crap
TP=$(tput cm 123 123)
tPRE=${TP%%124*}
tEND=${TP##*124}
TMP=${TP#*124}
tMID=${TMP%124*}


COLS=$(tput co)
ROWS=$(tput li)


MX=$(($COLS-2))
[ $MX -gt 60 ] && MX=60
MY=$((ROWS-2))
[ $MY -gt 20 ] && MY=20
PX=$(($MX/2))
PY=$(($MY/2))
SQ="$PY,$PX"
L_KEY="o"
R_KEY="p"
U_KEY="q"
D_KEY="a"
if [ "$1" = "vi" ];then
L_KEY="h"
R_KEY="l"
U_KEY="k"
D_KEY="j"
fi
SEED=$(date +%s)
trap 'die "Script interrupted by user!"' INT
tput vi
clear

die() {
    tput vs
    print_at2 $MY 0 " "
    echo $1
    stty sane </dev/tty
    exit
}

rand() {
  SEED=$(((1103515245 * $SEED + 12345) % 2147483648));
  RANDO=$((SEED % $1));
}

gen_apple() {
while true;do
 rand $((MY-3))
 A_Y=$(($RANDO+2))
 rand $((MX-3))
 A_X=$(($RANDO+2))
 A_ALL="$A_Y,$A_X"
     OK=1
    for combo in $SQ;do
        if  [ $combo = $A_ALL ];then
            OK=0
               break
        fi
    done
   [ $OK -eq 1 ] && break
done
print_at2 $A_Y $A_X "*"
}

check_wall() {
 [ $1 -le 1 -o $1 -ge $MY ] && return 1
 [ $2 -le 1 -o $2 -ge $MX ] && return 1
 return 0
}

# 1 up 2 down 3 left 4 right
move_head() {
 DEAD=0
 OPX=$PX
 OPY=$PY
 case "$dir" in
     d) PY=$(($PY+1));;
     u) PY=$(($PY-1));;
     l) PX=$((PX-1));;
    r) PX=$((PX+1));;
    *) return;;
 esac
 print_at2 $((MY+1)) 40 "Y:$PY X:$PX  "
 check_wall $PY $PX

 if [ $? -ne 0 ];then
  DEAD=1
  HIT="the wall"
 fi
 NHEAD="$PY,$PX"
 for combo in $SQ;do
  if [ "$combo" = "$NHEAD" ];then
    DEAD=1
    HIT="yourself"
    break
  fi
 done
 SQ="$NHEAD $SQ"
}

draw_snake() {
    TAIL=${SQ##* }
    Y=${TAIL%,*}
    X=${TAIL#*,}
    if [ $A_Y -eq $PY -a $A_X -eq $PX ]; then
    # we ate an apple
        SCORE=$(($SCORE+1))
        gen_apple
        print_at2 $((MY+1)) 0 "SCORE: $SCORE"
    else
    #    delete tail
        SQ=${SQ% *}
        print_at2 $Y $X " "
    fi
    C=O
    nseg=0
    for combo in $SQ;do
          Y=${combo%,*}
          X=${combo#*,}
          print_at2 $Y $X $C
          C=o
          nseg=$(($nseg+1))
          # we really only need to draw the 1st 2 segs
          [ $nseg -ge 2 ] && break
    done

}
print_at2() {
        ZZ=$tPRE$1$tMID$2$tEND
        echo -n "$ZZ$3"
}
for i in $(jot $MX);do print_at2 0 $i -;print_at2 $MY $i -;done
for i in $(jot $MY);do print_at2 $i 0 "|";print_at2 $i $MX "|";done
print_at2 1 1 +
print_at2 1 $MX +
print_at2 $MY 1 +
print_at2 $MY $MX +
print_at2 $(($MY+2)) 10 "Left=$L_KEY Right=$R_KEY Up=$U_KEY Down=$D_KEY"
gen_apple
dir=x
stty -icanon -echo;dd ibs=1 obs=1 cbs=1 conv=unblock 2>/dev/null | \
while true;do
read -t 0 inp
sleep 0.1
if [ "$inp" = "" ];then
 NOOP=1
#echo -n "."
else
case "$inp" in
    $L_KEY) [ $dir != "r" ] && dir=l;;
    $D_KEY) [ $dir != "u" ] && dir=d;;
    $U_KEY) [ $dir != "d" ] && dir=u;;
    $R_KEY) [ $dir != "l" ] && dir=r;;
esac
#echo read $inp
inp=""
fi
move_head
draw_snake
[ $DEAD -eq 1 ] && die "You hit $HIT !"
done
nice game!
 
I would advise you to stop asking questions now and just proceed.
As others have suggested, write a program in some language that prints "Hello, world!". Until you can do that you're only talking about programming and not doing.
Forget about what you said you wanted to do earlier. You don't have the knowledge or skill for that yet. Print "Hello, world!" and then come back.
And show your work!
Yep, I learned more in a few weeks of writing my own program than I did in an entire quarter of CS. I was somewhat surprised later when I went through a beginner programming book and I'd covered basically everything in the book via a couple relatively small projects.
 
IMO you should do multiple things: start with your own toy programs as well as read code of programs in /bin, /usr/bin, and common libraries in libc to see how experienced programmers wrote their code. You can also try adding features to standard programs as that will force you to learn these programs more deeply. Finally, read well regarded books to tie it all together.
 
same as title
i do not know much about programming and when i try to read book or watch videos

i find people explain how to print something , how to do simple calculation , simple if and else statments and functions

I get it.... Back in the early 1980s... during the Old Republic...

I wanted to play Star Trek. I wanted to command the Enterprise, Sector by Sector, Quadrant by Quadrant, wipe out the Klingons and Romulans and win it all for the United Federation of Planets... you know... "The good guys"

But more than that I didn't want to buy an off the shelf, shrink wrapped software package which was going to be someone else's "this is what you get" Star Trek game... I wanted to program it myself. Maybe later I can improve the Enterprise shields, phasers or photon torpedos, in the code? How about add a cloaking device.. like those Romulan War Birds had?

So I searched around and found this book -> BASIC Computer Games: Microcomputer Edition. And, you know, in the 1980s... that book (DID NOT) cost $193.00 USD.. holy cow ! But ... it does now - (Link Amazon): BASIC Computer Games: Microcomputer Edition :cool: Anyway... I guess they are literally charging $1 USD a book page !

And there it is !
-- the complete BASIC source code listing, line for line, to create a "Super Star Trek" game. So you can type in the BASIC source code into your computer, line for line, not really knowing (yet) what the BASIC code is actually doing, compile the finished source code, correct all the typos and mistakes, and then "run" the finished code and.. command the Enterprise.

My point is -> You do not have to understand (on Day 1) -- HOW the house is built... just read the "assembly instructions", follow them and get a finished product working (first). Later you can dig in to the finished/working BASIC source code and start figuring out the WHYs and HOWs the BASIC program works.

Why do you think IKEA is so popular? You go to IKEA and buy some product there made up of 10,000 different individual parts, contained within 20 to 30 different card board boxes .. you drag all of that home ... and when you are all done assembling all of those 10,000 different parts ? ... hours later ... ?

... You have a new table !
 
Back
Top