Syntax ~ The Beauty of Digital World

05.14

What is up, guys?

With me again in this grandioso day. Have you ever heard about syntax?

Did you know what it actually is?

In programming, syntax refers to the rules that specify the correct combined sequence of symbols that can be used to form a correctly structured program using a given programming language. Programmers communicate with computers through the correctly structured syntax, semantics and grammar of a programming language.

(Definition on techopedia)

This time, I want to tell you some basic syntaxes of :

- decision-making
- loops
- arrays

This time I will use:
~ C as the programming language.
~ Dev-C++ 5.11 as the IDE.
~ TDM-GCC 4.9.2 as the compiler.

1. Decision-making


Decision-making structures require that the programmer specifies one or more conditions to be evaluated or tested by the program, along with a statement or statements to be executed if the condition is determined to be true, and optionally, other statements to be executed if the condition is determined to be false.

(Explanation on tutorialspoint)

C language provides some types of decision making statements, and today I'll tell you just two kind of them. If statement and switch statement.


a) If statement



An if statement is a programming conditional statement that, if proved true, performs a function or displays information.


Below is the syntax example :

#include<stdio.h>

int main(){
int fortuneNumber;
printf("This test will define your fortune number. Type a number please\n");
scanf("%d", &fortuneNumber);
if (fortuneNumber<=100){
printf("%d is your fortune number", fortuneNumber);
}
}

Try to compile and run the syntax, and it will displays interface like this.

I try to input the number 25 and then it will displays like this.

EXPLANATION :

The structure of an if statement works like this :

if (statement is TRUE), execute the line of the code. Like the code that I made, it means that if the number (fortuneNumber) that typed by the user less than or equal to 100, then print (display on the screen) the sentence like that in the picture.

b) Switch statement


switch statement allows a variable to be tested for equality against a list of values. Each value is called a case, and the variable being switched on is checked for each switch case.


Below is the syntax example :

#include<stdio.h>

int main(){
int presIndo;
printf("Presiden Indonesia keberapa yang ingin kalian ketahui?\n");
scanf("%d", &presIndo);
switch(presIndo){
case 1:
printf("Soekarno.");
break;
case 2:
printf("Soeharto.");
break;
case 3:
printf("Habibie.");
break;
case 4:
printf("Gusdur.");
break;
case 5:
printf("Megawati Soekarnoputri.");
break;
case 6:
printf("Susilo Bambang Yudhoyono.");
break;
case 7:
printf("Joko Widodo.");
break;
default:
printf("ERROR: Belum ada presiden.");
}
return 0;
}

Try to compile and run it. It will displays like this.

I try to input number 5 and then it will displays like this.

EXPLANATION :



The condition of a switch statement is a value. The case says that if it has the value of whatever is after that case then do whatever follows the colon. The break is used to break out of the case statements. Break is a keyword that breaks out of the code block, usually surrounded by braces, which it is in. In this case, break prevents the program from falling through and executing the code in all the other case statements. The default case is optional, but it is wise to include it as it handles any unexpected cases.



2. Loops


loop describes the process of a software program or script repeats the same instructions or processes the same information over and over until receiving the order to stop. If not handled properly a loop can cause the computer to become slower as it becomes overwhelmed with having to repeat the same steps over and over causing it to get stuck in an endless loop.


In computer programming there are many different types of loops that can be used. For now, I'll explain you about for loop and while loop.

a) For Loop


For is a loop statement in programming that performs predefined tasks while or until a predetermined condition is met.


Below is the syntax example :
#include<stdio.h>

int main(){
int num;
for(num=10;num<=100;num+=5){
printf("%d\n", num);
}
return 0;
}

I compile and run the syntax, and it will displays like this.
EXPLANATION :

The syntax for the loop is like this:

for (variable initialization; condition; variable update){
code to execute while the condition is true
}

In the example code that I wrote, I start from number 10 until the number less than or equal to 100, and every time the code repeat itself and then it added by 5 until it reached the limit you put in.

b) While Loop


while loop in C programming repeatedly executes a target statement as long as a given condition is true.


Below is the syntax example :


#include<stdio.h>

int main(){

int num=40;
while (num<=60){
printf(" %d ", num);
num+=2;
}
return 0;
}


I compile and run the code, it will displays like this.

EXPLANATION :



The syntax of while loop looks like this:



while (condition) { 

Code to execute while the condition is true 
}

It will check the condition first, and then if it's true it will displays the block statement. It will be repeated until the value is false.

3. Arrays


An array is a collection of data items, all of the same type, accessed using a common name. Array variables are declared identically to variables of their data type, except that the variable name is followed by one pair of square [ ] brackets for each dimension of the array.


Below is the syntax example of an array :


#include<stdio.h>



int main(){

char arr[4]={'a','g','i','a'};

int index;


for(index=0;index<4;index++){

printf("%c", arr[index]);
}
return 0;
}

This is the example of an array. I've run and compile the syntax, it will displays like this.
EXPLANATION :

The syntax for the array above is like this:

char examplearray[4]:

This would make a character array with 4 slots (the places in which values of an array are stored). To access a specific part element of the array, you merely put the array name and, in brackets, an index number. This corresponds to a specific element of the array. The one trick is that the first index number, and thus the first element, is zero, and the last is the number of elements minus one. The indices for a 4 element array range from 0 to 3. 


Okay, guys. So now you've learn so many things, huh?

You have learn the syntax of decision-making (if and switch statement), loops (for and while loop), and arrays.

I hope this will benefit you, and of course I hope the explanation understandable. Thank you for your time. Hasta la vista, baby~

You Might Also Like

3 komentar

  1. MestiQQ Adalah perusahaan judi online KELAS DUNIA ber-grade A

    Sudah saatnya Pencinta POKER Bergabung bersama kami dengan Pemain - Pemain RATING-A

    Hanya dengan MINIMAL DEPOSIT RP. 10.000 anda sudah bisa bermain di semua games.

    Kini terdapat 8 permainan yang hanya menggunakan 1 User ID & hanya dalam 1 website.
    ( POKER, DOMINO99, ADU-Q, BANDAR POKER, BANDARQ, CAPSA SUSUN, SAKONG ONLINE, BANDAR66 )

    PROSES DEPOSIT DAN WITHDRAWAL CEPAT Dan AMAN TIDAK LEBIH DARI 2 MENIT.

    100% tanpa robot, 100% Player VS Player.
    Live Chat Online 24 Jam Dan Dilayani Oleh Customer Service Profesional.

    Segera DAFTARKAN diri anda dan Coba keberuntungan anda bersama MestiQQ
    ** Register/Pendaftaran : WWW-MestiQQ-POKER
    Jadilah Milionare Sekarang Juga Hanya di MestiQQ ^^

    Untuk Informasi lebih lanjut silahkan Hubungi Customer Service kami :
    BBM : 2C2EC3A3
    WA: +855966531715
    SKYPE : mestiqqcom@gmail.com

    BalasHapus
  2. Mari sambut kemeriahaan Natal & Tahun baru 2020 bersama Donaco Poker
    Gratis Free chips buat member setia Donaco poker

    Hubungi Kami Secepatnya Di :
    WHATSAPP : +6281333555662

    BalasHapus
  3. mau untung banyak tanpa kerja?
    solusinya yaitu zeusbola!
    dengan deposit minimal Rp 50.000 kamu sudah bisa menjadi member kami dan bisa bermain berbagai banyak jenis game dengan 1 user id saja
    ???keuntungannya tidak main main???
    lansung gas untuk daftar ya bosquww

    terdapat beberapa jenis bank lokal yang bisa digunakan bermain di
    zeusbola seperti :






    UNTUK INFORMASI LEBIH LANJUT SILAHKAN HUBUNGI KAMI DI:
    WHATSAPP : +62 822-7710-4607
    FACEBOOK : @zeusbolame
    LINE : zeusbola
    INSTAGRAM : zeusbola.official

    BalasHapus